C requires that you state the data type of a variable on declaration. int is the first data type that stores integers. Integers always take up 4 bytes of memory (32 bits) which limits how much it can store. The range is -2^31 to 2^31-1(cause zero needs a place). unsigned int can be used to double the range of positive. char short for character they always take up 1 byte of memory (8 bits). float are real numbers, these are 4 bytes of memory for each. double addresses float shortfall in that hteyh have 8 bytes of memory or 64 bits. They are able to display more precision for real numbers. void - type not a data type this is more like a placeholder used for functions without returns. Those are the 5 main types that will be in C, there are two data types with cs50 library which is bool or boolean which includes true or false not a default in C. string is also provided by the library but not a default data type for C. This is to store a series of characters. Later we will encounter structures and typedefs that can allow you to create types or combine types. To declare a variable you need to give a data type and a name (ex. int number;) You can also name multiple variables in one declaration like int height, width; to declare both height and width. You can declare a variable like int number; number = 17; this declares/assigns. You can also initialize int number = 17;.
We can use +, -, *, / as expected. We can also use the modulos % which gives the remainder. In assignment the one on the right goes to the one on the left. Modulos is useful for random number generators to limit the range of values. x = x * 5; and x *= 5; also x--; x++;. Boolean expressions used for comparing values typically for conditional or a loop. Every nonzero is true, zero is false. Logical expression && is only true if both are true. Logical || is true if at least one operand is true. The last logical operator is ! or NOT. Relational operators <,>,<=,>=. Equality == and Inequality !=.
Also known as a conditional branch. if is a common conditional that will have a boolean expression the code will execute, otherwise that code is skipped. You can also add an else block after the if, in order to have something execute when the condition is false. You can also put an else if statement after the if statement to have it test for another if conditional before going to the next branch/else. Also talks about the switch statement, which can be used to decide a
group of conditionals at once. switch(x) { case 1: printf("One!\n"); break; case 2: printf("Two!\n"); break; } You can also use a ternary operator for conditionals int x = (expr) ? 5 : 6; this will make the value of x 5 if the expression is true, 6 if false.
The while (true) {} block is similar to the forever block from scratch. You can break out of the loop with a break command ctrl + c will kill the program if infinite loop is taking over. You can also replace the condition of a while loop with a real boolean expression. There is also the do {} while loop which always executes the code one time, then it is pretty much the same as a while loop. The last kind of loop is the for loop for (int i = 0; i < 10; i++)
{ printf("Counting to 10 from %i", i); } While loop is often good in a game since you may never know when a certain condition like the user dying will occur. The do while loop is great when asking for input since its always going to gain input and then it may or may not accept that input. The for loop is great when you know how many times to run it or have a specific condition that can set that easily. You can also use all loops interchangeably.
Linux commands work within the IDE and on any UNIX based system (linux, mac). Windows has command prompt but some commands are different because it is not unix based. The first command used a lot is the ls command which gives a readout of all files in the directory. The next command used a lot is cd which allows navigation between directories. pwd gives the present working directory. You can press ctrl L to clear the screen in a command prompt. If you want to get back to the home directory just type cd. There is also the mkdir which is make directory. cp source destination - copies a file to a new location, if you try to copy a directory with stuff inside of it it will throw an error. If you want to copy the folder with all the files recursively you need to use cp -r source destination. the rm command removes a file. If you want to get rid of the confirmation you can use -f. if you want to delete a whole directory you need to do it recursively with -r. You can also combine the two flags together. The last command that is useful is mv which is move command. This can also be used to rename files. Other interesting commands chmod, ln, touch, rmdir, man, diff, sudo, clear, telnet.
The first number mentioned is 23 which is used as the potential max height of the mario pyramid. This was set because it's the maximum height of the command line and it will look awkward outside of that. Magic numbers are numbers that aren't fully explained that are hardcoded. You can fix this problem by using a variable that gives it a name to describe the purpose of the number. C provides a preprocessor directive for creating symbolic constants. At the time your program is compiled you can use #define to go through the code and find and replace but it does not require a semicolon. #define is often used with caps. The better solution to decksize would be to use #define DECKSIZE 52 at the top so that you can use DECKSIZE. This can't be easily manipulated like a variable and is often in caps so it is not confused with a variable.
Most programming languages allow you to make functions. These are sometimes called procedures, methods, or subroutines. Function is a blackbox (if we did not write the function ourselves, we do not need to know about the underlying implementation) that has 0+ inputs and 1 output. Black box really just means you don't really care how it's written,just that the output is expected. We use functions for various reasons: organization - break into subparts, simplification - smaller components, reusability - functions are easily recycled. Function declarations - the first step gives a compiler a heads-up that a user is writing a function. Declarations should go at the top of your code. The declarations follow the same format : return-type name(argument-list); return-type is the kind of variable that will be outputted. Example int add_two_ints(int a, int b); this gives a good explantion of the function. Function definition is almost the same as declaration, with a ; ex. float mult_two_reals(float x, float y); To call a function you just need to pass it the appropriate arguments and assign its return value to an appropriate place.
Scope defines where a variable can be used. Local means it is only available from where it was accessed. Global means it can be accessed from anywhere. Local variables are passed by value which means that when it is passed its a copy. This is so that the original variable isn't replaced. If we had something like int foo = 4; foo = triple(foo); this would actually replace the variable foo wiht 12 instead of just triple(foo) which would give us 12 but in a
one time location.
Using the debug50 using the step over button after adding a red breakpoint spot to see the values.
When using debug50 its away to step into a function. Which is the down arrow button.
Arrays are a fundamental data structure they can hold many values of the same type in consecutive memory locations. The values in an array have to be the same size and can be accessed by index from 0. C will not prevent you from going out of bounds of your array this will result in a segmentation fault typically. To initialize an array with values you can do bool truthtable[3] = {false, true, true} . Arrays can have more than one dimension like bool battleship[10][10] which is technically still just a 100 space one dimensional array. You can not assign an array to an array you have to loop over each element. Arrays are passed by reference ratherthan value. This means the actual array is passed when it is called.
Most of the time the program starts with int main(void). If we want to collect data at the command line we have to declary int main(int argc, string argv[]). argc stands for argument count integer type that stores the number of command line arguments passed. argv is the argument vector, which stores each word the user input as a string in the array. These are always stored as strings.
Algorithm is step by step instructions to complete a task. Linear search iterates over the entire area searching for a specified element. When you find what you want you stop, otherwise keep going. Worst case scenario is you look through the array of n elements. Best case is that what we are searching for is in the first value searched. So O(n)-omega(1).
Divide and conquer, reducing the search by half each time trying to find the target. An essential part of this is to sort the array first. In pseudocode: Repeat until sub(array) is the size 0: | calculate the middle of current array | if target at the middle stop | if target is less go to the left of middle | if target is more go to the right. Worst case log n, omega 1.
Bubble sort moves higher values to the right, lower values to the left. Set swap counter to a non zero value | repaet until swap counter is 0 | reset swap counter to 0 | look at adjacent pair | if elements out of order swap them. Bubble sort Big O of n^2, but omega of n.
Finds the smallest unsorted element and adds it to the end of the sorted list. Repeat until no unsorted elements remain | search the unsorted part of the data to find the smallest value | swap the smallest found value with the first element of the unsorted part. Big O of n^2, omega is also n^2.
When you call a function memory is set aside to execute the function. We call these chunks of memory stack frames or function frames. More than one functions stack frame may exist in memory at one time. These frames are arranged in a stack, the most recently called function is at the top of the stack. When a new function is called, that frame is pushed to the top of the stack. When afunction finishes it is popped off the stack.
Function that calls itself until it reaches base case. Consists of a base case which terminates, and a recursive case which recalls the function. Fibonacci requires multiple base cases. The collatz conjecture requires multiple recursive cases.
In merge sort to sort the array it is sorted into a smaller arrays, then merged in order leveraging recursion. Sort the left half of the array | Sort the right half | merge together.