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 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 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.


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.


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…

Programming with Scratch

The first week of CS50x is an introduction to the course and an introduction to thinking in the same way as a computer. As an introduction to programming and the logical steps that a computer takes I was introduced to an online programming environment called Scratch. Developed by MIT, it allows for users with little or no prior programming knowledge to construct simple scripts and programs using a visual interface.

Scratch works by offering the user a series of ‘puzzle pieces’ to create functions, variables and loops. Some of these pieces are shown below

Screen Shot 2017-04-23 at 21.37.01

Problem Set 0

The task set out in problem set 0 was simply to play around with scratch and get a feel for how the logic of programming works and then submit a program, the specifications for the program were:

  • Your project must have at least two sprites, at least one of which must resemble something other than a cat.
  • Your project must have at least three scripts total (i.e., not necessarily three per sprite).
  • Your project must use at least one condition, one loop, and one variable.
  • Your project must use at least one sound.

For this task I decided to create a mini game in which a character has to move across three screens to collect beach balls in order to win. This is a much simpler concept than I had originally wanted, I had originally wanted to add enemies into the game, but the scripts for these were proving a little too complicated at the time for them to be included in a time effective manor.

The main difficulty was working around the script which changes the backgrounds. This script detects when the main character sprite was at the edge of the screen, switches the background to the next one in the sequence and then moves the character to the other side of the screen to give a scrolling background effect. Actually getting the program to do this wasn’t too difficult but getting the beachballs that were placed in the game to reappear when returning to the background only if they hadn’t been collected proved to be something I couldn’t figure out.

The final version of my project is below:



I’ve started taking the online CS50x course lead by Harvard University, it is the online version of their introductory Computer Science 50, which is, apparently, their largest on-campus class.

I’ve heard quite a bit of buzz around this course online and it seems to be a solid introduction to the principles of Computer Science. I’m going to be making posts about the lectures and the projects that I have to undertake for the course, primarily as a record of any problems that I faced and how I overcame them, but also as a general ‘learning diary’.