CS50x – Week 0

Written by on August 10, 2017 in Coding, CS50

Back To Square One

To get myself back to the point I was before with coding in general I decided to start from the beginning to refresh my knowledge.

I learnt about a course which is an introduction to computer science and programming. The course is CS50x which is provided by Harvard University.

I signed up through edX, which is an online portal for a variety of courses from several well known institutions.

Week 0

The first week of materials, called ‘week 0’, actually focuses on computer systems and how computers work. Beginning with binary, the fundamental building block of everything electronic, the lecture explains what the binary system is. The binary system is essentially 0’s and 1’s or it can also be explained as a flow of electricity, it’s either off or on. 1 digit of a binary number is called a bit and 8 bits are a byte. The lecture continues to explain how these binary numbers or bits/bytes can represent different things. It can represent letters, numbers, punctuation and more through the ASCII system. With the ASCII system the binary number 0100 0001 (65 in decimal) will equate to ‘A’. Similarly ‘B’ is 0100 0010 and 66 in decimal. The lecture then shows us how we can represent colours with binary numbers using a system known as RGB, or Red, Green and Blue. The largest number we can represent with 1 byte/8 bits is 255. By assigning three values to red, green and blue we can mix them to create a wide range of colours from black (0,0,0) to white (255,255,255).

The lecture then goes onto algorithms and how computers follow a set of instructions to complete a task. They use an example of looking up someone in a phone book.
0. pick up phone book
1. open to middle of phone book
2. look at names
3. if Smith is among names
4. call Mike
5. else if Smith is earlier in book
6. open to middle of left half of book
7. go back to step 2
8. else if "Smith" is later in book
9. open to middle of right half of book
10. go back to step 2
11. else
12. quit

This type of searching technique is called a binary search, which we’ll cover in a later post. You can see how the ‘code’ follows a set of instructions until completion. Some of the words like ‘pick up’, ‘open’, ‘look’ and ‘call’ are doing something and it is better to know them as functions. The key word ‘if’ is a conditional statement and is used to check if something is true or false. The ‘go back’ key word is a loop which can repeat a set of code until a certain condition is met. We can tell how efficient an algorithm is by measuring its run time. The binary search above is faster than going to every page in the phone book and searching for the name because we’re halving the phone book every time we’re halving the number of names we have to look through. So it doesn’t matter how big the phone book is it will take a lot less steps to find the name than a traditional search e.g a Linear search.

The lecture then moves onto the main part. Programming. Code looks something like this:

#include <stdio.h>
int main(void){
    printf("Hello world");
}

This is a very simple program that prints out the words ‘Hello world’ and it only takes up a few lines of code. However to a lot of people there are all these different symbols and keywords which can be intimidating especially if the program is hundreds of lines long. So to get people introduced to programming and not make it so intimidating, some people over at MIT created a program that will allow people to visualise the code easier. This program is called Scratch, which can be found here. Scratch uses blocks which are compartmentalised portions of code. We can chain these blocks together (called scripts) to create something. The blocks themselves are split into different categories and they all have their own unique function. Some blocks will move something, or write something, or even play a sound. Some blocks are for creating loops and for conditional statements. With the amount of tools on offer you can create a wide variety of media with different levels of complexity.

Problem Set 0

The task for Problem Set 0 (pset0) is to create a program in scratch which could be anything, like interactive art, a game or an animation. The program must contain at least 2 different sprites, must play a sound, must have at least 3 scripts, and contain 1 conditional statement, 1 loop and 1 variable.

I decided to make a game which I call Cheese Chase. The aim of the game is to collect all the pieces of cheese and then then return to the starting position, all while avoiding the cat. You can play it below.

Scratch project

It took me about an hour or two to completely finish the game in scratch. I found it easy enough to do and I find that Scratch is a great tool for a beginner to learn to code.
Next week we start with C programming.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments«