CS50 is a renowned introductory computer science course at Harvard University, it provides students with comprehensive and immersive experience in the world of programming and computer science. This course challenges you to explore fundamentals of coding, problem-solving, and algorithmic thinking. Today I started with Week 1 of the 11 weeks. As we delve into this coding challenge, I want to emphasize the value of embracing the learning process. Instead of copying the provided code verbatim, I encourage you to approach tis as an opportunity to sharpen your problem-solving skills. True mastery comes from the journey of figuring things out, not just he end result. So let’s begin this adventure together.
CS50 provides you with Practice Problems, Labs and Problem Sets. The practice problems for this week were debug, half and prime. On the debug program, they gave you a file full of buggy code, this meant it wouldn’t compile.
// Become familiar wih C syntax
// Learn to debug buggy code
#include <cs50.h>
int main(void)
{
// Ask for your name and where live
name = get_string("What is your name? ")
location = get_string("Where do you live? ")
// Say hello
print("Hello, %i, from %i!", name, location)
}
That was the initial code, you have to go through it to make sure you declare variables correctly. Like the following code.
name = get_string("What is your name? ")
The correct way to declare a variable is by stating what type of variable will be stored in it. Some of the variables are int
which stores whole numbers, char
which stores single characters, float
which stores numbers with decimals and thanks to CS50 making easy for us strings which stores complete words or sentences.
#include <cs50.h>
int main(void)
{
// Ask for your name and where live
string name = get_string("What is your name? ")
string location = get_string("Where do you live? ")
// Say hello
print("Hello, %i, from %i!", name, location)
}
That was an easy fix. If we try to compile the program it won’t compile due to other bugs in there like the missing semi-colons. Semi-colons go on the end of line.
string name = get_string("What is your name? ");
Now that’s the correct way to write that.
The next problem that I found was the print function. In C the correct way to print or output text to the screen is using the printf()
function. Lets also make sure to add that semi-colon at the end.
printf("Hello, %i, from %i!", name, location);
While typing that out I noticed something. The print function is trying to find an int
variable. Format specifiers are used together with the printf()
function to tell the compiler what type of data the variable is storing. It is basically a placeholder for the variable value. For the string variable we must use the %s
placeholder. Let’s also add a blank line after the ! by adding \n
. The newline character \n
is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
printf("Hello, %s, from %s! \n", name, location);
If we try to compile again, well it just won’t work, we are still missing a header file. To be able to use the printf()
function we must first call the <stdio.h>
header file. <stdio.h>
is a header file library that lets us work with input and output functions, such as printf()
. Header files add functionality to C programs.
// Become familiar wih C syntax
// Learn to debug buggy code
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// Ask for your name and where live
string name = get_string("What is your name? ");
string location = get_string("Where do you live? ");
// Say hello
printf("Hello, %s, from %s! \n", name, location);
}
And that was it for the first program. Next I had to tackle the half program.
// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
return 0.0;
}
This was the starting code for the half program. What I had to do was calculate my half of the restaurant bill. This takes a couple of variables such as bill_amount
, tax_percent
, and tip_percent
. The program then asks you to enter the bill amount, tax percent, and tip percent. Now we have to calculate it using the function.
The way that I got this to work was by creating one new variable called total
, but I did make a mistake. I just went ahead and added the tax, tip and bill together.
float total = bill + tax + tip;
That did not work correctly. First of all tax and tip are percentages, not whole numbers. So I divided tax and tip by 100 to make it the decimal form of a percentage. I also had to multiply tax and tip to bill to get 5% of the bill. This came to this:
// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float total = bill + bill * tax/100 + bill * tip/100;
return total/2;
}
That one wasn’t that hard, now on to the next practice problem.
Prime was the next program to tackle, what I had to do was write a program that would ask for a range of numbers and print all the prime numbers within that range. This was the starting code provided by CS50:
#include <cs50.h>
#include <stdio.h>
bool prime(int number);
int main(void)
{
int min;
do
{
min = get_int("Minimum: ");
}
while (min < 1);
int max;
do
{
max = get_int("Maximum: ");
}
while (min >= max);
for (int i = min; i <= max; i++)
{
if (prime(i))
{
printf("%i\n", i);
}
}
}
bool prime(int number)
{
// TODO
return false;
}
I achieved this by using the modulo. The modulo operation is used to find the remainder after dividing one number by another. I played around with this for a while until I figured out that all the factors of any of the numbers would have a remainder of 0. Now all I had to do was figure out a way to return a false
value if the number had more factors than 1 and itself. The way that I managed to do this was by creating a for
loop. Loops can execute a block of code as long as a specified condition is reached. When you know exactly how many times you want to loop through a block of code, use the for
loop.
for (int i = 1; i <= number; i++)
{
if (number % i == 0 && i != number && i != 1 )
{
return = false;
}
}
return true;
This loop will start with the first number in the range provided and divide it by the variable i
. Each time the loop goes through it will add one to the variable i
which is achieved by the short hand i++
. This stops when i
has reached the number provided and it will then go to the next number in the range. It will go on until it goes through all the numbers in the range that was provided. The final code looked like this:
#include <cs50.h>
#include <stdio.h>
#include <printf.h>
bool prime(int number);
int main(void)
{
int min;
do
{
min = get_int("Minimum: ");
}
while (min < 1);
int max;
do
{
max = get_int("Maximum: ");
}
while (min >= max);
for (int i = min; i <= max; i++)
{
if (prime(i))
{
printf("%i\n", i);
}
}
}
bool prime(int number)
{
for (int i = 1; i <= number; i++)
{
if (number % i == 0 && i != number && i != 1 )
{
return false;
}
}
return true;
}
And that concludes the first practice problems for Week 1. I challenge you to try doing it yourself and see what other solutions you can come up with.