CS50 Quiz 1 Cheat Sheet
CS50 Quiz 1 Cheat Sheet
CS50 Quiz 1 Cheat Sheet
Make is not a compiler Breakpoint: explicitly define stopping point, allows you to inspect memory at that point and step through lines one at a time
Multithreading: performing multiple tasks at once Floating point imprecision
Heap:unallocated memory that can be dynamically used (MALLOC)
Stack: memory set aside when program starts
Scope: use pointers for values inside functions or your functions won't return same values Stack overflow: when stack runs out of space
remember to free dynamically allocated memory
.c files do not need a main function malloc allocates memory on the heap NULL does not demarcate the end of a string
Always check for NULL, also for malloc
Preprocessing: Directives like #include are processed, then inserts contents of header files and any declarations into file being preprocessed
Segmentation fault: attempts to access memory it isn't allowed to
Compiling: Preprocessed C code is translated into assembly, possibly with optimizations
Implicit declaration: program is defined after main function, no prototype OR no header file
Assembling: Assembly instructions translated into machine code and stored in object (.o) file Undeclared identifier: variable not declared
Remember to initialize pointers Remember $_POST
Linking: object code for main is combined with object code from other files, creating executable file
HTML is a markup language
Huffman coding: using frequency of ASCII characters, make BST
Recursion adds to the stack, consumes lots of memory and can cause segfaults and stack overflow, don't forget base case, leads to elegant code
HTTP: Port 80 Email: Port 25 HTTPS: Port 443
GIF is lossless JPEG is lossy
Programs may compile backdoors
PHPSESSID: cookie, value identifies user's browser, maps to server-side file containing $_SESSION which is superglobal PHP uses [ ]
mario
for(i=0,i<n,i++)
{ for(j=0,j<i,j++)
{ print(#);}
print(\n);}
typedef struct
{ type a;
type b;} structname;
(*ptr).str OR ptr->str
queues are FIFO (first in, first out) structure, index of first element will change as dequeue
If two websites on same server, share same IP address, Host header indicates desired domain
Anonymous function/lambda function: function without a name, passed as inputs to functions to be called later
Set a cookie store key-value pair in RAM or file for subsequent retrieval
If array var has f1 = v1, f2 = v2, etc. print(json_encode($var)) = {v1:f1, etc.)
HTML = structure
CSS = style SQL injection attack put in SQL code into field, cause SQL query to run unexpected code
for(var i in milo)
foreach($milo as $i)
for(var i in milo)
foreach($milo as $key => $value)
js arrays values can even be functions
console.log(i)
print($i)
console.log(milo[i])
print($key)
js, returns keys
php, returns values
js, returns values
php, returns key
Javascript, PHP are interpreted languages
<stdio.h>
int fclose(FILE* fp);
int fprintf(FILE* fp, string format);
size_t fread(void* ptr, size_t size, size_t blocks, FILE* fp);
FILE* fopen(string filename, string mode);
int fscanf(FILE* fp, string format);
int fseek(FILE* fp, long int offset, int from_where);
size_t fwrite(void* ptr, size_t size, size_t blocks, FILE* fp);
int scanf(string format);
int sprint(string ptr, string format);
int sscanf(const char *str, const char *format)
<string.h>
char* strcat(char* dest, const char* src);
char* strncat(char* dest, const char* src, size_t n);
int strcmp(string str1, string str2);
char* strtok(char* str, const char* delim);
<stdlib.h>
int rand(void); (pseudorandom btwn zero, RAND_MAX);
<ol></ol> - ordered list
<ul></ul> - unordered list
<li></li> - list element
<table></table> - table
<tr></tr> - table row
<td></td> - table data
void insert(int n) Insert: insert node into doubly linked list
{node* ptr = malloc(sizeof(node));
if (ptr != NULL)
{ptr->n = n;
ptr->prev = NULL;
ptr->next = list;
if (list != NULL)
{list->prev = ptr;}
list = ptr;}}
void remove(int n) Remove: remove node from doubly linked list
{node* ptr = list;
while (ptr != NULL)
{if (ptr->n == n)
{if (ptr == list)
{list = ptr->next;
if (list != NULL)
{list->prev = NULL;}}
else
{ptr->prev->next = ptr->next;
if (ptr->next != NULL)
{ptr->next->prev = ptr->prev;}}
free(ptr);
return;}
ptr = ptr->next;}}
int pop(void) Pop: take int at top of stack, else return -1
{if (stack.size == 0)
return -1;
return stack.numbers[--stack.size];}
bool push(int n) Push: take int and add to stack, else return false
{if (stack.size == CAPACITY || n < 0)
return false;
stack.numbers[stack.size++] = n;
return true;}
void traverse(node* root) Traverse: traverse binary search tree
{
if (root == NULL)
{
return;
}
traverse(root->left);
printf("%i\n", root->n);
traverse(root->right);
}
bool search(int n, node* tree) Search: searches binary search tree
{if (tree == NULL)
{return false;}
else if (n < tree->n)
{return search(n, tree->left);}
else if (n > tree->n)
{return search(n, tree->right);}
else
{return true;}}
find with recursion
bool find(node* root, int i)
{node* ptr = root;
while (ptr != NULL)
{if (i < ptr->i)
ptr = ptr->left;
else if (i > ptr->i)
ptr = ptr->right;
else
return true;}
return false;}