gabrielleeyj
Navigate back to the homepage
About Me

Understanding CS50

Gabriel Lee
June 22nd, 2020 · 1 min read

Recently, edX was giving out free signups for HarvardX CS50. So I thought to retake and review the course once again but this time paying for the certificate. What I immediately noticed, was the improvement in walkthroughs and guides. There is even a video guide which gives you the hints on how to answer the pset statements. What I liked about CS50 was that for the C programming component, there is the CS50.h library which essentially mimics some of the functions of python.

Usually for C programming you would use

1printf("What is the input:");
2scanf(%d, variable);

But with CS50.h you can use, similar to what you would do with Python.

1int variableName = get_int("What is the input:");

Then with pset0 where you have to build a program using Scratch here is my project a memory sequence game.

1https://scratch.mit.edu/projects/319884718

Moving on we learnt about how conditional statements, data types, and how loops work in C. Out of boredom, I created a custom tax calculator which covers pretty much what pset1 taught. I won’t post the answers for pset1 as its readily available since its so widely known now, however I discourage looking for answers before you even attempt.

1#include <stdio.h>
2#include <math.h>
3
4int main(void) {
5 float amt;
6 float svc;
7 float gst;
8
9
10 // Input for Amount Spent
11 printf("Input Amount Spent:$");
12 scanf("%f", &amt);
13 printf("\n");
14
15 // Condition Check for Invalid Amount
16 while(amt < 0) {
17 printf("Invalid Amount Entered, Please Input Again: $");
18 scanf("%f", &amt);
19 }
20
21 // GST Calculation
22 gst = amt*7/100.0;
23 printf("GST : $%2.f\n", gst);
24
25 // Service Charge Calculation
26 svc = amt*10/100.0;
27 printf("Service Charge is: $%.2f\n", svc);
28
29
30 if(&amt > 0) {
31 printf("Total Spent: $%.2f\n", amt);
32 }
33
34 return 0;
35
36}

As you progress through Week0 - Week1, you learn just the basic syntax of C and how you create loops and iterate which is the key concept in every programming language. Being able to iterate through lists/arrays/maps enables you to create wonders. In pset2, CS50 covers functions/arrays/CLI. Some basic CLI commands you should remember always :

1ls - lists the current items in directory
2cd - change directory
3cd <dir name> - moves you to the directory
4mkdir - creates a folder
5rmdir - removes a folder
6rm <file name> - removes file
7cp <file/folder> - copies
8clear - clears the CLI

One quirk of C is that functions (user-defined) can be placed above the main function or below the main function.

1void printstuff (void) {
2 printf("hello, world!");
3}
4
5void main (void) {
6 printstuff();
7}

If you reverse the positions of the function printstuff(); won’t work. This is because a computer reads line-by-line from top to bottom. You can however, declare the function by telling the computer in C that there is a function and you intend to use it later.

1void printstuff (void);
2
3void main (void) {
4 printstuff();
5}
6
7void printstuff (void) {
8 printf("hello, world!");
9}

So far I’ve covered week0,week1,week2 of CS50. I’ll post again for the next few weeks.

More articles from gabrielleeyj

How I learnt to code

Before I begin, let me warn you first that this is going to be a long read. I started to code in html and css back in the 1990s, when…

June 15th, 2020 · 5 min read

Common Things in Programming

Its exactly, 3 years since I've started learning the concepts of programming. Though I'm not proficient enough to be called an expert nor do…

June 30th, 2020 · 2 min read
© 2020 gabrielleeyj
Link to $mailto:hello@gabrielleeyj.comLink to $https://github.com/kayigeLink to $https://www.linkedin.com/in/gabrielleeyj/Link to $https://www.instagram.com/gabrielleeyjLink to $https://www.facebook.com/melchsee