To ‘C’ or not to ‘C’?

This post is going to be all about the week 1/pset1 of the CS50 course.

Week 1 follows on from week 0 by building on the principles taught with Scratch, however it now moves these principles into the text based programming language ‘C’.

The lecture goes over basic commands and syntax as well as implementing loops. The tasks posed for this week are:

  • Say hello to the world with hello.c.
  • Calculate your water consumption with water.c.
  • Recreate one of Mario’s pyramids, in mario.c.
  • Provide a user with either cash or credit in greedy.c or credit.c.

Hello.c

Hello.c is a simple program that prints the phrase “Hello, world!”. The code is given in the instructions and this task serves to get the user used to using the CS50 IDE interface and the syntax highlighting. It also introduces the user to the clang compiler.

Water.c

Water.c asks the user to create a program that will work out the equivalent bottles of water used when showering based on how long the shower takes. It introduces simple maths into a program and works along the lines of n * 12 to determine the number of bottles.

The program prompts the user for an input and then uses a do while loop to do the calculation only when the user inputs a valid integer.

Mario.c

This task instructs the user to create a program that will take an integer from the user and then print a right angled pyramid on the screen that is as many lines tall as the user specifies. The pyramid is made from ‘#’ and ‘ ‘, and the top level has two ‘#’.

Once the user has entered a valid value (between 0 and 23) the main bulk of this code is contained within 3 for loops. These loops simply take it in turns to print the spaces and then print the #’s. The program uses simple formulas to determine the number of characters to print based on initial height entered. The code for this project can be seen here.

Greedy.c

The final task in this week’s problem set requires the user to figure out the smallest amount of coins that can be given to make up a customers change, the coins you have to choose from are $0.25, $0.10, $0.05 and $0.01.

This challenge introduces someone of the problems that are inherent in the way that a computer counts and displays numbers, namely floating point imprecision. Because a number is represented by a series of 1’s and 0’s there is a finite amount of combinations of those that are used to represent an infinite amount of numbers. The example given in the lecture is that the the sum 1 / 10 displayed to a large number of decimal places should just be represented as 0.1000000000… but it is actually shown as 0.100000000000000000555111512312578… .

Because we are working in cents it is a good idea to multiply by 100 to move from decimal values to integer values we can then use the round command to round the number to the nearest integer. Once this calculation has been completed we can use a series of if loops inside a while loop to determine whether the change can be divided into each of the respective coins from biggest to smallest, all of this is going on inside a while loop which ensures that script runs whilst a valid integer is provided.

For example, if the value of $0.44 is given then the script:

  1. Multiplies by 100 = 44
  2. Can 44 be divided by 25? Yes, once
  3. Can the remainder be divided by 10? Yes, once
  4. Can the remainder be divided by 5? Yes, once
  5. Can the remainder be divided by 1? Yes, four times
  6. Print the amount of coins = 7

That explanation is just a rough approximation, in reality the script asks if the number can be divided by 25, if so it subtracts 25 from the number and adds one to the coin count, this then repeats, if it cannot be divided by 25 it moves on to the next denomination and so on.

The code for this script can be seen here.

This week was a lot of fun and it felt really good to start getting to grips with a programming language, I’ve heard good and bad things about ‘Learn C the Hard Way’ in so much as it’s a bit of a masochistic thing to undertake but it’s incredibly thorough. I think I will stick to the basics for now and see what happens in the future…