Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Last active October 6, 2018 20:37
Show Gist options
  • Save Theartbug/405c74ba04a2f2c611c9512bd02fd94c to your computer and use it in GitHub Desktop.
Save Theartbug/405c74ba04a2f2c611c9512bd02fd94c to your computer and use it in GitHub Desktop.
CS50 Notes

Lecture 3

location: https://cs50.harvard.edu/2018/fall/weeks/2/

Steps to compiling a program

preprocessing

  • grabs externally linked content and brings into file

compiling

  • code is compiled down into assembly, which the computer understands

assembling

  • convers assembly into binary

linking

  • all externaly linked content is converted to binary and combined

RAM

random access memory

  • temporary memory used when a program is used / active
  • uses electricity
  • copies the program off of the hard drive into the RAM during the programs use
  • variable creation in C will go into RAM
  • continuous memory, array-like, list
  • when a string variable is stored, it takes up n+1 bytes of the length of the string as the last byte contains a null-zero '\0' to denote the end of the string

function declaration

  • in C, if you'd prefer to have a function further down in the body after it is called by main, you can declare it earlier without the body:
void chart(int score); //give a prototype
void main(void) {...}

arrays

declaring:

int scores[3]; // creates 3 integer variables assigned to the name scores
for (int i = 0; i<3; i++) {
scores[i] = get_int("Score %i: ", i + 1);
}

as a parameter: void chart(int count, int scores[])

  • strings are like an array of chars
  • strlen(string) is a way to give the length of a string

const

  • C utilizes const const int COUNT = 3;

loops

  • you can declar multiple variables with a comma
for (i = 0, n = srtlen(s); i < n: i++) {}

letters

  • since letters are converted to unicode via numbers, you can change the letters with numbers! capitalization:
if (s[i] == 'a' && s[i] == 'z') { // if it is a letter
  printf("%c", s[i] - 32); // change it to a capital!
} else {
  printf("%c", s[i]); // else just print it
}
  • 32 is the difference in unicode positions between the lowercase letters and uppercase letters

manual

  • to obtain information about C functions type man FUNCTION_NAME

main() gets more interesting

  • when C runs the first function main it can accept a parameter by changing it to:
int main(int argc, string argv[]) {} // argument count, argument vector

ex:

int main(int argc, string argv[]) {
  if (argc == 2) { // if there are two arguments given
    printf("hello, %s\n", argv[1]); // print the second, since the first is the program name
  } else {
    printf("hello world\n");
  }
}
  • main can return error messages under the hood. use 0 to indicate success, and 1 to indicate error.

algorithms

O(n^2)

  • bubble sort continuously goes through the array and swaps the position of out of sort numbers, larger toward the end, smaller toward the front
  • selection sort looks for the smallest element and swaps it into the front, then continues down the line swapping along the way, will ignore already placed elements

O(log(n))

  • merge sort continuously divide the array until only two elements are reached and sort those. Place the sorted elements in a new array. Do this continuously until all elements are merged back together

Lecture 2

location: https://cs50.harvard.edu/2018/fall/weeks/1/

C

-printf() - logs to the console in a formatted way

  • printf("hello, World\n"); use the \n to tell the print to move the cursor to the next line
  • to show \n you would have to \\n
  • int counter = 0; - declares an integer variable
    • counter++ - increases counter
  • whitespace does not ultimately matter to C
  • assignment is not required at time of variable declarations
  • underscores are conventions within C to represent spaces eg: get_string()
  • file extension is .c example:
if (x < y)
{
  printf("x is less than y/n"); 
}
else if (x > y) 
{
  printf("x is greater than y/n"); 
}
else
{
  printf("x is equal to y/n"); 
}
  • for loops
for (int i = 0; i < 10; i++)
{
  printf("hmmmm printing %s\n", i);
}
  • User input
# include <stdio.h> // includes C basics like printf()
string answer = get_string("what's your name?\n");
string emotion = get_string("How are you feeling?\n");
printf("Hello, %s", answer);
printf("Hello, %S, I'm %s to see you", answer, emotion);
  • %s is a placeholder for a variable, or second parameter given to printf(). Multiple can exist with ordering.

    • different placeholder types exists for types of variables
  • Compilation

    • pass the C program as input, and get machine code (binary) as output
    • manually handle with telling the terminal to compile the C code In terminal type:
clang hello.c //will output compilation file called a.out
./a.out //will run the output
hello, world //output

clang -o hello hello.c //tells the output to be called hello
./hello //better named file to run

make hello //cs50 command shortcut to make a file called hello to an output file called hello
  • C does not contain the type string, must add it as a spec to files
#include <cs50.h> //includes the type string and some prebuilt functions created by cs50
  • C does not like unused variables, errors out a compilation

integers

  • when using integers, 2 / 10 = 0, will round decimals to integers
int main(void)
{
    int i = get_int("Integer: ");
    printf("hello, %i\n", i);
}

Floats

  • working with floats, use %f to print it in a string
  • to specify decimal places, give decimal with %f such as %.7f
float f = get_float("Float: ");
printf("hello, %f\n", f);
  • floats will become imprecise due to RAM of the computer rounding things, this becomes a problem when comparing floats, which should never be done
  • a float will take up 32 bits in a computer
  • there is a datatype called double that will increase the size of RAM / processing power, but increase the precision of float math

Functions

  • int main(void) begins the program in C, it is the default first called program
void cough(void)
  • when void is used, it does not take anything in, nor does it return anything
  • must declare functions prior to their use
void cough(int n)
  • declare variable types within function parameters

while

int n;
do
{
  n = get_int(prompt);
}
while (n < 3);
return n;
  • variables have scope within curly bois

memory

  • computers only have finite memory, there will be overflow
  • if you count too high, the higher amount will be lost
int main(void)
{
  for(int i = 1; i *=2)
  {
    printf("%i\n", i);
    sleep(1);
  }
}
  • sleep waits 1s, creates async
  • C will error out when the int gets too large, goes past the 32nd bit
  • underflow can happen as well, which means when subtracting a larger number from a smaller number, the counter can flip around to the largest represented number depending on the bit storage
00000000 - 1 => 11111111 (255!)

Lecture 1

location: https://cs50.harvard.edu/2018/fall/weeks/0/

Binary

  • This is how computers store information and speak
  • Each place is a bit
  • Values stored in 0 and 1
  • The poistion matters to what amount is stored. Add each position.
  • 32 16 8 4 2 1 are a few of the positions values example:
1 0 0 = 4
0 1 0 = 2
0 1 1 = 3
1 0 0 0 = 8
1 0 1 1 = 11
  • How to convert into text? Use numbers as well and agree on number to letter conversion. Depends on context that a machine knows to read a number into text
  • ASCII is the number to text conversion language, uses 8 bits total (256 total possible characters)
  • Unicode is a larger character language, up to 32 bits (millions of combinations)
  • RBG is a way to combine primary colors to get a specific color. A computer stores the RGB value to create an image. The KB in an image is the number of stored bites it takes to create that image.

Algorithms

  • A series of steps to solve a problem. Tried and true methods for common problems.

Programming languages

  • contain functions, booleans, loops, conditions, variables, threads, events, and more...

Scratch

  • uses puzzle-piece-like phrases to create a program
  • different colored pieces represent different programatic syntax, they clip together to form complete structure

Threads

  • multiple scripts happening in parallel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment