It’s been a week since I started Harvard’s CS50 course and I was able to go through weeks 0, 1 and 2, which contained 3 lectures and a bunch of problems to be solved. I don’t think I will be able to keep this peace, since I have a full-time job as a product manager and also the course is getting more challenging each week, but I’ll do my best to keep it at 2 lectures (weeks) per week and writing at least 1 blog post at the end of each week.
So in this blog post, I want to cover what I think about the course, what I’ve learned so far and what I find interesting about programming and C language which is what we’ve been using so far.
The course is just amazing, no doubt the best MOOC I’ve taken, and I’ve taken a lot of them. It’s really high quality, the progression of the material it’s been smooth so far and the professor and teaching assistants are really passionate about the subject and easy to understand, plus the material and supplementary technology they provide, like the coding environment and integration with github is excellent. I really recommend anyone getting serious about learning to code to start their path taking this course.
Summary of the material covered in this week:
- What is programming and algorithms
- Programming languages, machine code (binary) and compilers
- Command line
- Hello world, conditional statements, loops, data types, and operators
- Problem sets
What is programming and algorithms
I really loved the idea that programming is just a tool for solving problems, and how algorithms are sets of steps for solving them. We can think of problem-solving as the process of taking some input or details about our problem and generate some output or the solution to the problem. What’s in the middle (the black box) is the algorithm, step-by-step instructions that take those inputs and transform them into the output.
Programming languages, machine code (binary) and compilers
There are hundreds of programming languages but every single one of them has the same goal, that communicates a set of instructions from a human to a computer or machine, but to do that, there has to be a few other steps given that the computer or machine doesn’t really understand any of the programming languages, or English.
Computers only understand zeroes and ones (0 & 1), something that’s called binary. So in order for the computer to understand our instructions, there has to be something or someone that translates, from the programming language we are using, to zeroes and ones, or binary. This is where the compiler comes into action, the only purpose of the compiler is to translate the code we wrote in our chosen programming language to binary, so the computer understands what we’re telling it to do and can execute it.
I found really interesting that you could handle the computer just using “commands” and that really that’s the only way a computer executes things. When you click, select and pretty much do anything with your mouse and GUI (graphical user interface), what’s really happening behind is that these command are being executed.
ls - which stands for “list”. It lists all the folders in the directory you’re at.
cd - change directory. Changes your current directory.
pwd - present work directory - displays your current directory.
ctrl + l - cleans the terminal.
mkdir - make directory - creates a new folder.
Hello world, conditional statements, loops, data types and operators
Nothing much to say here, pretty basic stuff that’s very similar in every programming language.
Conditional statements: If, if else and switch.
Loops: While, do while and for.
Data types: Integer (int), character (char), floating (float), double (double), and void (void). I found interesting that in C, strings don’t exist, but more on that in the next post.
Operators: Sum (+), subtract (-), multiply (x), divide (/), modulus (%) which is one that I found a bit tricky, it gives us the remainder when the number on the left of the operator is divided by the number on the right.
CODE 13/4 = 3 and the remainder is 1; 13 % 4 = 1.
Logical operators: And (&&), or (||) and not (!) which gives inverts the value of its operand.
CODE X true !X False
Relational operators: Less than x < y, Less than or equal to x <= y, greater than x > y, greater than or equal to x >= y, equality x == y and inequality x != y.
This is one topic I found most interesting and never thought about it before but it’s so obvious. Basically, a library is code someone else wrote before us that we can use calling it at the beginning of our code.
For example, the most common library that we used is stdio.h, which is called as follows.
So this library enables us among others, the function “printf” which prints an outcome in the user’s screen, a function that doesn’t come with the default language C but someone else wrote before so we can use it.
Like this, there’re thousands of other libraries and useful functions we can just call at the beginning of our code and use them, without even coding them ourselves.
I won’t go into much detail about this one because I want to write a whole article about this so I’ll just say that I really got interested in the topic, I even bought a few books about algorithms. Yeah I know, I’m really nerdy but I can’t help it, I’m an engineer.
So far I’ve completed 6 problem sets, I’ll leave the link to the code below.
- Marioless: Prompts the user for a number (n) and then creates a half pyramid of height n and prints it.
- Mariomore: Prompts the user for a number (n) and then creates a pyramid, with a space between each half, of n and prints it.
- Cash: Asks the user how much change is owed and then prints the minimum number of coins with which that change can be made, minimizing the number of coins to be handled. Coins are 25 c, 10c, 5c and 1c.
- Credit: Asks the user for a credit card number, validates if the length of the number is correct, then applies Luhn’s algorithm to validate if it’s a valid credit card number and finally prints the brand of the card; VISA, MS or Amex.
- Caesar: Encrypts a message entered by the user based on a numeric key, also entered by the user.
- Vigenere: Encrypts a message entered by the user based on a key, also entered by the user but this time the key is a word.
That’s it for now, I’ll keep posting about things I learn and find interesting. If you want to check the code I wrote for each problem set and future ones, you can find them on my github profile, under CS50 Psets repository.
Connect with me on Twitter! :)