Table of Contents
CS50 is an online course on Computer Science taught at Harvard University since 2015.
I came to know about CS50 by stumbling upon Mark Zuckerberg’s video on YouTube about Facebook and Computer Science.
I immediately subscribed to the CS50 YouTube channel with a motive to revisit in the future for more such content.
It turns out, CS50 channel is full of knowledge.
On 4th September 2020, I decided to start watching Computer Science Lectures provided as a playlist in the CS50 channel.
I will try to summarize each lecture while learning. CS50 has already created detailed notes for each course.
I am doing this mostly to keep a journal to revisit for insights.
Lecture 1 – Introduction to Computer Science & Scratch
Computer Science is driven by problem-solving.
To solve a problem, we take inputs, perform a set of instructions on it to get an output.
Just like we understand English language, Computer has it’s own language called Binary Language.
Binary language consists of 0 & 1 only. Computer only understands Binary language (more on this later).
So if we are able to read a number 751, then for Computer to understand and interpret this number it checks for the related 0s & 1s that denote this number in it’s system.
751 is a number where 1 is at ones, 5 is at tens and 7 is at hundreds place.
So, we represent each digit as power of 10. Since their are 10 possible number at each place starting from 0 to 9.
751 = 7×100 + 5×10 + 1×1
= 7 x 10^2 + 5 x 10^1 + 1 x 10^0
Computer only understands 0 and 1
Why?
Because Computer system works on Electricity which is either ON or OFF.
ON is represented by 1 and OFF is represented by 0.
Just like we take numbers as power of 10, Computer take 0 and 1 as power of 2.
So, if I see a number 3 then Computer interpret it as 011.
3 = 0 x 2^2 + 1 x 2^1 + 1 x 2^0
^ means raise to the power.
Similarly the number 8 will be seen as 1000 by Computer.
Computer is a Set of Switches
Computer System consists of many transistors which are basically a switch. These switches are turned ON and OFF based on the input.
Every output we see on Computer screen is represented by 0 and 1.
Alphabet, symbol, color, image, video, etc are mapped to a number which is decided by ASCII & UNICODE. These are standards decided in the early days to represent each and every element that can be interpreted by Computer to show output.
So if their is an emoji shown on the Computer screen, it is represented by some predefined number.
But as we know every color on Computer screen is a result of combination of color Red, Green and Blue. So, the concentration of each RGB color we take, makes up to a single color that covers a pixel on the screen.
So for a Computer, Emoji is-
Emoji = Combination of 0 and 1 { Combination of predefined number [ Combination of color in each pixel ( Combination of RGB Colors ) ] }
Computer Algorithm
To perform a tasks we require elements. These elements are the input we provide.
A set of instructions are then performed on that input.
That gives us an output.
These “Set of Instructions” are called as algorithm.
There are many ways to do a task. But with a Computer algorithm, we generally focus on the most efficient way possible.
Efficient Code = Less Space + Less Time for Output
For searching a name in the phone directory, the most efficient way is to look from the middle. Decide whether the name is present in the left or right side. Then again look in the middle of that chosen part.
The algorithm can be represented by pseudo-code, which is a step-by-step instruction in an informal Human language.
A Pseudo-code consists of functions, conditions, boolean expressions, loops, etc.
Scratch – A Graphical Programming Language
Scratch is graphical programming language that offers drag and drop functionality to create a interactive program like stories, games, animations.
Although this is targeted for Children, this is definitely a useful tool for waking up or nourishing the algorithmic mind inside you.
Everything is self explanatory in Scratch interface.
C Programming
This is the second lecture of Computer Science.
David J. Malan (Gordon McKay Professor of the Practice of Computer Science Harvard University) started with a basic comparison between Scratch and C Programming.
We used CS50 Sandbox which is a temporary programming environment for students and teachers.
CS50 has its own library that contains functions.
This is the basic structure of C program for printing Hello world.
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
stdio.h – A predefined file in C libraries for Standard Input Output functions.
int main(void) – int is the type of output, main is the function and void is just empty parameters.
printf – Print Format is used to format the string in different ways and print it out. This function is predefined under stdio.h file
The CS50 sandbox comes with a C compiler denoted by clang.
clang is used to convert programming language to machine language. That is, it converts the source code to the binary code that the machine can understand.
We can then call this machine code to get our output.
This is how it works –
$ clang hello.c
$ ./a.out
$ hello, world
This CS50 sandbox environment works with all Linux commands.
Instead of a.out, we can name our machine code file manually.
$ clang -o hello hello.c
Here hello is the name of file containing the machine code.
To make it more quick, you can use this command –
$ make hello
This searches for filename and creates the machine code file with exact same name.
David J. Malan also covered other C programs, for example, if-else condition, for loop, nested for loop, while & do-while loop.
CS50 have their own library file named cs50.h that has lot’s of useful functions like get_int, get_string, get_float, etc.
We also learnt about how to create our own function.
The general format for creating a function and using it is-
#include <cs50.h>
#include <stdio.h>
void OurFunction(void);
int main(void)
{
OurFunction();
}
void OurFunction(void);
{
printf("Hello world");
}
Memory & Overflow
Computer storage is finite. A program can occupy space in RAM while running but to a certain extent. So int, float, char have a certain limit to the amount of bit.
If we ask the Computer to give an infinite value of value above the limit then it will give an overflow value of round off.
Data Types
int – 4 bytes
float – 4 bytes
char – 1 byte
double – 8 bytes
unsigned int – doubles the positive range with no negative values
void – it means the function does not return any value, for example, printf. It also means the function does not take any parameter.
bool – take boolean values TRUE or FALSE. This is defined under cs50.h library file.
string – combination of characters.
Operators
Assignment, Arithmetic, Boolean are the examples of operators in C.
Assignment operator is =
Arithmetic operators are + – * / %
x = x + 1, x += 1, x++ all are same thing.
Boolean consists of two operators- Logical and relational.
In boolean expression, every non-zero number is TRUE. Zero is False.
Logical operators are && ||
&& is logical AND where both values must be true to yield true output. If either one is false then output will be false.
|| is logical OR where at least one variable needs to be true to give true output.
Relational operators are > < >= <=
Conditional Statements
if, else if, else is an example for conditional statement.
Switch case is another example.
General format for switch case –
int x;
switch(x)
{
case 1:
printf("one");
break;
case 1:
printf("one");
break;
default:
printf("nothing");
}
If user enters 1 then the case 1 will be processed.
break is used to terminate the switch function.
Ternary operator is ?:
expr ? 5 : 6
expr is expression. If the condition is true then 5 else 6.
Leave a Reply