COP 3223 Final Review Jennifer Brown December 2, 2018 1 Introduction 1.1 Variables I. How can we store data in a program? A. Which of these are valid names for variables? i. 9length ii. hello iii. IamASuperCoolName iv. int v. a new int vi. str1 II. Which way does assignment go? A. Which of these are valid if I have an int n; in main? i. 3 = n; ii. n = 5; 1.2 Arithmetic I. What order of operations does the C compiler follow? A. Is it the same or different than PEMDAS? B. If we have an int n; in main, what goes into n? i. n = 3 + 4 / 2 * 7; ii. n = (3 + 4) / 2 * 7 iii. n = 4 * (1-8); C. Is this valid? i. n = 4(1 + 2); ii. n = (3-2)(4 + 5); 1
1.3 Precedence I. Which operation executes first? A. i++; B. ++i; II. What does the?: tertiary operator do? A. What will these return? i. int a = 3, b = 1; (a > b)? a : b; ii. int a = 3, b = 5; (a > b)? a : b; B. Are there any other tertiary operators? III. Which as higher precedence? A. Does the + execute before the =? i. int b = 3 + 4; B. Additionally the + and = operators are... i. Unary? ii. Binary? IV. Lets get fancy! What does the % operator do? A. How can we use mod to determine an even or odd number? B. How can we use mod to restrict range? C. What does mod do when we have rand()? 1.4 Data Types I. What is a data type? A. What type of data do these create? i. int ii. float iii. double iv. int * v. void 2
1.5 Conversion Specifiers I. What is a conversion specifier (format code etc)? A. What do the following codes display in a printf? i. %d ii. %f iii. %c iv. %lf v. %p 2 Functions 2.1 Main I. Let s break down the main function! A. int main(void) { return 0; } i. What data type does main return? ii. What parameters does main take in? iii. What does main return? iv. Can I have no main function? v. Can I have 2 main functions? II. What is this clever little void we keep seeing? III. What is the difference between pass by value and pass by reference? 2.2 Writing Functions and Function Prototypes I. How do we build our own functions? A. What do we need? i. Data type ii. Name iii. Parameters iv. Return II. Let s look at the following function prototypes A. void swap(int *a, int *b); B. int max int(int x, int y); i. What parameters do they take in? ii. What do they return? III. Let s finish up the max function! A. The function max will find the biggest number and return that value 3
3 Conditionals 3.1 Decisions I. What keyword do we use when making decisions? A. When do we execute a decisions? Will these execute? i. int a = 3, b = 1; if (a > b) ii. int a = 3, b = 1; if (a < b) iii. if (1) iv. if (0) v. if (a = 1) a. Oh no! Did we do this right? B. What comparisons can we make? i. >=, <= ii. ==,!= iii. <, > 3.2 Boolean Logic I. We saw some tricky numbers in our decisions! Let s review Boolean logic, what does the compiler see as true and as false when we get sneaky numbers? II. What if we want to compare more than one thing at a time? A. What do these operators do? i. && ii. B. If we have a and the first comparison is true, do we check the second one? Does it matter? i. Likewise, if we have a &&, and the first comparison is false, do we compare the second? Does it matter? ii. What is this called? 3.3 If/Else if/else I. What keyword do we use if we want to execute when the condition is false? A. Let s write a nested if/if else/else that determines the age of the user, and then tells them if they can vote, drink alcohol, both, or neither. We fancy! 4
4 Loops When we want to make a decision over and over again, we use loops. What are different types of loops we have? 4.1 While Loop I. How does the while loop work? A. What happens in these loops? 4.2 For Loop i. int i = 3; while (i!= -1) printf( %d, i ; ii. while (i!= 1); { printf( Hurray!); i = 1; } a. Oh no! This looks like it would work, but it doesn t! What happened? I. How does the for loop work? A. What happens in these loops? i. for (i = 0; i < 5; i++) printf( %d, i); ii. for(;;) a. This looks a little funky, is this still valid? b. What is the minimum thing that for loops require? 4.3 Do While Loop I. How does the do while loop work? A. What happens in these loops? i. do printf( Yasssss ); while (0); a. Oh lookie, we have some tricksy numbers again in our loops. What do we do? b. Does this execute at least once? c. How is it different than a while loop? 5
5 Break/Continue 5.1 Break I. What do we do when we want to leave a loop early? A. Let s write loop that adds to a total as long as the numbers are even 5.2 Continue I. What do we do when we want to skip a certain case and continue looping? A. Let s write a loop that keeps looping as long as the user doesn t put in negative numbers, but skips odds. 6 Switch 6.1 Decisions i. Depending on your logic, continue can very easily cause nasty infinite loops. Let s fix the following code: a. int i = 0; while (i < 7) if (i == 2) continue; i++ } I. Now that we are master of if/else if/else, let s explore another way to do this with switch statements A. How do we write a switch? i. Let s write a switch that takes in a letter grade and tells the user what range that grade will be in. B. in the following code, what will happen? i. char a = a ; switch(a) { case a : printf( a ); case b : printf( b ); } a. Something horrible happened! What happened and how do we fix it? b. What is so important about break in switch statements? C. Is the default case in a switch statement important? To what is it similar to in if/else if/else? 6
7 Pointers 7.1 Data Type I. What type of data does a pointer contain? A. How do you initialize a pointer? B. What does the following code do? i. int a; int *p; *p = 5; a. DANGER! Something VERY BAD has happened! What is it and why is it so VERY BAD? b. What is dereferencing, and how can we do it without self destructing our code? ii. int a; int *p = NULL; *p = 5; a. Oh no! We also seg faulted here, what happened? b. Can we dereference a null pointer? 7.2 Functions with Pointers I. We saw earlier that swap prototype, but we didn t write it! Now that we have reviewed pointers, we are ready to tackle it. A. Recall: void swap(int *a, int *b); i. Why do we need the pointers, and how can we finish up this function? B. Let s also write a findmax function using pointers i. Prototype: int findmax(int *x, int *y); ii. This can also be achieved via pass by value, but we want to be fancy and use pointers instead. iii. Why do we need to change the return value from void to int? 7.3 Pointer pointers I. What type of data does a pointer to a pointer contain? A. How do you initialize a pointer to a pointer? B. How do I use the q pointer to manipulate a in the following code? i. int a; int *p =???; int **q =???;??? 7
C. What s wrong with the following code? 8 File I/O i. int a; int *p; int **q; **q = 5; a. This code is spooky scary, why is that? I. What is File I/O? A. What code would we use to open a.txt file? 8.1 File I/O Functions I. What functions can we use once we have opened a file? A. What do the following functions do? i. fopen(); ii. fscanf(); iii. fgets(); iv. fprintf(); v. fputs(); vi. fclose(); 8.2 Reading a file I. What code do we write to read from a.txt file? A. What functions do we need? i. fopen? ii. fscanf? iii. fgets? iv. fprintf? v. fputs? vi. fclose? B. Let s write a function that reads names from a file and then prints them. 8
9 ASCII 9.1 Do characters have values? I. If I have the letter/char a, what value would it have? A. Look at the following code: i. char a = a ; printf( %d, a); a. What do you think it will print? B. What about this? 9.2 ASCII Table i. int a = 97; printf( %c, a); a. Crazy magic! This code is valid, why is that? I. Let s write a function that prints the ASCII values of 33-126. First it will print the integer value and then the character. A. What value is 1? B. What value is A? C. What value is a? 10 Arrays and Strings 10.1 Arrays I. What is an array? A. Is the data in an array consecutive? i. How do i initialize an array? ii. Where does the index of an array always start? II. What will the following code do? A. int arr[5] = {1, 2, 3}, i; for(i = 0; i < 5; i++) printf( %d, arr[i]); i. If I have the code arr[5]; what values are in the cells of the array? 9
10.2 Passing arrays I. If I want to pass an array to a function, what do I send in the function call? A. What parameter will hold what we are sending? B. If I have an array called array and I send it to a max function (returns the max value in the array), what am I technically sending? i. IE: int array[5] = {4, 7, 6, 9, 1}, n = 5; max(array, n); a. What does the max prototype look like? II. When passing arrays, why do we need a pointer? A. Let s write that max function! i. Takes in an array and a length ii. Returns the max value in that array B. Let s write a min function 10.3 Strings i. Takes in an array and a length ii. Returns the max value in that array I. How are strings different from arrays? II. How are are they the same? A. What data type are the cells in a string? B. What extra value do we have at the end of a string? i. What happens if our array is too small to hold the null terminator? C. When we pass a string to a function, is it similar to an array? i. What is different? D. What will the following code print? i. char str[6] = hello ; int i; for (i = 0; i < 6; i++) printf( %c str[i]); E. Let s write a function that finds the smallest alphabetical letter. IE: a is smaller than b F. Let s write a function that finds the biggest alphabetical letter. IE: z is bigger than y 10
10.4 String Functions I. What library do we need to use string functions? II. What are some of the string functions that we can use? A. What do the following functions do? i. strlen(); ii. strcmp(); iii. strcpy(); iv. strcat(); v. strstr(); B. When using string functions, what do we have to be careful of? Can we segmentation fault? i. What could go wrong with the following code? a. char str[6] = { hello }; strcat(str, helloooooo ); ii. What went wrong and why? 11 2D Arrays I. What does a 2D Array look like? A. How can we loop through a 2D array and set values to 0? i. Can we do this with one loop or do we need two? ii. Are 2D arrays similar to a coordinate system? B. When initializing a 2D array, it looks like: int arr[ ][ ]; i. Which spot sets the rows? ii. Which spot sets the columns? C. When using loops to manipulate 2D arrays: i. If we have i for loop 1 and j for loop 2 a. Does i correspond to: rows or columns? b. Does j correspond to: rows or columns? 11
12 Dynamic Memory Allocation (DMA) I. Why do we want to use dynamic memory allocation instead of static? A. Do we always know how big we want an array to be? B. Do we always know how big we want a string to be? II. In order to create an array dynamically, what do we need first? A. What functions do we use? i. What does malloc do? ii. What does calloc do? III. What is wrong with the following code? A. int *p, i, n = 5; for (i = 0; i 5; i++) p = malloc(sizeof(int) * n); 13 Structures i. DANGER! Something very bad is happening here, what is it, and why? I. Why are structures used? Why are they super mega awesome? A. Structures are known as abstract data types, why do you think they are called this? II. Is memory allocated in the following code? A. struct StudentInfo { char *name int ID; } i. There is an error here, what is it? III. How do I make a new struct? A. What is wrong with the following code? i. int main(void) { StudentInfo newstudent; return 0; } a. How can we change the struct so that this code is A-OK? B. How do we change the values in a struct when it s been initialized? i. Let s change the ID to 1111111, how do we write that in code? 12
ii. Let s make the name Aleks, how do we write that in code? IV. Can we create a pointer to a struct? A. How would that look? How do we code that? B. How would we change the data with a pointer? i. Do we still need to dereference? ii. Is there a sneaky shortcut we can use other than *.? C. What benefit is there in creating a pointer to a struct? 13