CS50 2019
Lecture 0 - Computational Thinking, Scratch
input -> [ black box ] -> output
How to represent input/output information?
- represent number
- represent characters/letters:
- ASCII (8 bits)
A:65(01000001)
-
- Unicode(a superset of ASCII, 8/16/24/32 bits)
emoji 😂 :128514(11111011000000010)
- represent colors
- RGB: every dot in an image is stored using three value
a shade of yellow: 72 73 33
every dot in an image is used three value
-
-
- animated GIF/videos
-
multiple images in the same file, one image fly past after another to create an illusion of moving
- represent music
maybe the note, the duration, the value
input -> [ algorithms ] -> output
The algorithm is a step-by-step process for solving a problem.
- functions: verbs or actions to tell computers what to do
- conditions
- Boolean expressions: one or zero questions
- loops
- variables
- threads
- events
Lecture 1 - C
20200610
source code(e.g. C) -> [ compiler ] -> machine code (01)
#include <stdio.h> int main(void) { printf("hello world\n"); }
run the code:
in the Terminal:
$ clang hello.c $ ./a.out
or assign a name to the compiled file
$ clang -o hello hello.c
$ ./hello
-o: output
List
a list of all the files in the current folder/directory
$ ls a.out* hello* hello.c
*: machine code
without*: source code
Remove
$ rm a.out rm: remove regular file 'a.out'? y
y: yes
#include <cs50.h> #include <stdio.h> int main(void) { string anwser = get_string("what's your name?\n"); printf("hello,%s\n", anwser); }
Terminal:
$ clang -o string string.c -lcs50 $ ./string what's your name? David hello,David
Or
$ make string clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow string.c -lcrypt -lcs50 -lm -o string $ ./string what's your name? David hello,David
int counter = 0; counter = counter+1;
counter += 1;
if(x<y) { printf("x is less than y\n"); } else { printf("x is not less than y\n"); }
; only after an action.
int i = 0; while(i<50) { printf("hello,world!\n"); i++ ; }
| Datatype | Prompt Function | Placeholder |
|
bool |
||
| char | get_char | %c |
| double(decimal with more digits) | get_double | %f |
| float(decimal) | get_float | %f |
| int(<4 billion) | get_int | %i |
| long(more than 4 billion) | get_long | %li |
| string | get_string | %s |
#include <cs50.h> #include <stdio.h> void cough(void);//prototype int main(void) { cough(); } void cough(void) { printf("cough!"); }
Lecture 2 - Arrays
20200615
Compile:
source code
⬇️
preprocessing
compiling
assembling
linking
⬇️
machine code
Bug
syntactically error
logical error
Byte
| bool | 1 byte |
| char | 1 |
| int | 4 |
| float | 4 |
| long | 8 |
| double | 8 |
| string | ? |
#include <stdio.h> int main(void) { char c1 = 'H'; char c2 = 'I'; char c3 = '!'; printf("%i %i %i \n", c1,c2,c3); } /* $ ./hi 72 73 33 */
int n1 = 72; printf("%c\n", n1); //H
Array
#include <stdio.h> #include <cs50.h> //global variable(outside of functions) const int N = 3; //avoid accidentally change; int main(void) { int score[N]; score[0] = 72; score[1] = 73; score[2] = 33; printf("Average: %i\n", (score[0]+score[1]+score[2]) / 3); }
averages
#include <stdio.h> #include <cs50.h> float average(int length, int array[]); int main(void) { int n = get_int("Number of scores:"); int scores[n]; for (int i = 0; i < n; i++) { scores[i] = get_int("Socre %i:", i+1); } printf("Average: %f\n",average(n,scores)); } float average(int length, int array[]) { int sum = 0; for (int i = 0; i < length; i++) { sum += array[i]; } return (float)sum / (float)length; }
In C, the array doesn't remember its length.
\0 terminating character of a string;
command line arguement;
int main(int argc, string argv[])
plaintext & key --> [ ] --> ciphertext
----

浙公网安备 33010602011771号