I have learnt about libraries, compilers, switch statements, and typecasting. When we are programming, we are giving instructions to the computer to carry out tasks. However, the programming language like C is considered a high level language. High level language is easy to read (resembles english), while low level language is cryptic. Computers only read in binary (0s and 1s), so we have to change the high level language code that we wrote, to a low level machine code that the computer can understand. The step is carried out by a compiler.
Compiler carries out 4 functions, namely, preprocessing, compiling, assembling, and linking. Preprocessing reads the top part of your C code, that is the “#include ” line. It takes the codes prewritten in the something.h library to your code. Next, compiling C code turns the C code to assembly language. Then, the assembly language is assmbled into object codes. Linker links the object codes into machine code (0s and 1s) that the computer can read.
Besides learning about theory, CS50x also taught me how to make some useful simple programs. You have to include the cs50.h header for the programs to work. cs50.h is available when you take the class.
Program 1 (Hello.c)
Print Hello, world.
1 2 3 4 5 6 |
#include int main(void) { printf("hello, world\n"); } |
Program 2 (mario.c)
Print the mario stair.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main(void) { int height = 0; do { printf("Height: "); height = GetInt(); } while (height < 0 || height > 23); for (int i = 0; i < height; i++) { for (int j = 0; j < (height - i - 1); j++) { printf(" "); } for (int k = 0; k < (i + 2); k++) { printf("#"); } printf("\n"); } } |
Program 3 (greedy.c)
Convert your money to quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). Rule: Use the least number of coins.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h> int main(void) { float dollars; int cents; int count = 0; do { printf("O hai! How much change is owed?\n"); dollars = GetFloat(); cents = round(dollars*100); } while (dollars < 0); while (cents / 25 > 0) { count++; cents -= 25; } while (cents / 10 > 0) { count++; cents -= 10; } while (cents / 5 > 0) { count++; cents -= 5; } while (cents / 1 > 0) { count++; cents -= 1; } printf("%i\n", count); } |
Program 4 (credit.c)
Check if number is valid credit card number, and list out the type of credit card.
I use the numbers from following link to test my program.
https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
This program can only work for American Express, MasterCard, and Visa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
#include <cs50.h> #include <stdio.h> int main(void) { long long cc; int evenDigit; int oddDigit; int total = 0; bool valid = 0; long long creditCheck; // get user credit card number printf("Number: "); do { cc = GetLongLong(); } while (cc < 0); creditCheck = cc; // luhn's algorithm while (cc > 0) { evenDigit = (cc % 100) / 10; oddDigit = (cc % 10); cc /= 100; // add digits of multiplication of evenDigit by 2 evenDigit *= 2; if (evenDigit / 10 == 1) { total += 1; total += evenDigit % 10; } else { total += evenDigit; } // add oddDigit total += oddDigit; } // check if the credit card is valid if (total % 10 == 0) { valid = 1; } else { printf("INVALID\n"); } // check type of credit card if (valid == 1) { if (creditCheck / 1000000000000000 >= 1) { switch (creditCheck / 100000000000000) { case 51: case 52: case 53: case 54: case 55: printf("MASTERCARD\n"); break; case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: printf("VISA\n"); break; } } else { if (creditCheck / 100000000000000 > 1) { switch (creditCheck / 10000000000000) { case 34: case 37: printf("AMEX\n"); break; } } else { if (creditCheck / 1000000000000 == 4) { printf("VISA\n"); } } } } } |
Program 5 (mario2.c)
Advance version of mario stair program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <cs50.h> #include <stdio.h> int main(void) { int height; // get height from user do { printf("Height: "); height = GetInt(); } while (height < 0 || height > 23); // print half pyramid for (int i = 1; i <= height; i++) { for (int j = 1; j <= height - i; j++) { printf(" "); } for (int k = 1; k <= i; k++) { printf("#"); } printf(" "); for (int j = 1; j <= i; j++) { printf("#"); } printf("\n"); } } |
My codes: https://github.com/shaunlgs/CS50x
Other posts in the series: Harvard CS50x 2014
did you notice the translational errors in your posted code, where ‘>’ & ‘<' are transposed into "<" and ">"