Episode 2 : C, The Mother Language

Episode 2 : C, The Mother Language

CS50 Week 1: C

After 2 months of learning Python, I wasn't prepared for how difficult programming in C would be! I guess the point of starting with C is to make you understand fundamentals of coding with a low level language and increase your appreciation of what higher level languages can do.

Here is how you display a "Hello World!" message on the screen in Python:

print("Hello World!")

And here is how you do the same thing in C:

#include <stdio.h>

int main(void)
{
    printf("Hello World!");
}

Crazy, right? And that's just a tip of the iceberg. This notwithstanding C is a very powerful and useful language since most modern Operating Systems are actually written in C. Here's a fun fact that makes sense but is funny the first time you hear it: The most common Python Interpreter is written in C.

The lecture covered other basic topics like data types, operators, conditional statements and loops. I was already familiar with these concepts only that the syntax in C is different so let me just move on and talk about 2 of the 3 coding problems on the problem set.

Greedy Algorithms

A greedy algorithm is one which goes through a problem in stages making the most optimal choice at every step of the problem. In this particular code problem, you are meant to complete a program that tells you the least number of coins (or notes) a cashier should give to a buyer depending on the change owed.

For Example: Muthoki goes to the supermarket and buys a small yogurt for her fiancé at KSh 52. She gives Wafula the cashier a Ksh 100 note. How much change should Wafula give to Muthoki? KSh 48 right? Considering the currency denominations we have in Kenya, your program should be able to figure out the least number of coins Muthoki should receive as change. In this case the answer is 5 coins (1 x KSh 40) + (1 x Ksh 5) + (3 x ksh 1).

Luhn's Algorithm

This was an optional code problem to submit if you're feeling more comfortable and I thought I was, so I gave it a shot. I'm embarrassed to admit it but the problem remains incomplete and 'unsubmitted' to this day😅. I struggled to implement the modulo operator (%) at the time and I haven't gone back to give it another try.

(# The mod operator (%) is an arithmetic operator that gives you the remainder when you divide two integers (E.g. 10 % 3 = 1). Pre-coding I had no idea that the remainder of a division could be so useful.)

The algorithm itself was invented by Hans Peter Luhn of IBM and is widely used in verifying the validity of credit cards, IDs etc. I was planning to give a breakdown of the algorithm, but on 2nd thought, just read about it here if you'd like: geeksforgeeks.org/luhn-algorithm

That's it for Week 1. Adios.