COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional................................. 5 3.2 Two Dimensional................................. 6 3.3 Strings and String Functions.......................... 6 4 File I/O 7 5 Pointers 8 6 Pass by Reference Functions 9 7 Dynamic Memory Allocation 10 8 Structs 12 8.1 Pointers to Structs................................ 12 9 Linked Lists 12 1
1 Code Tracing 1) What is the output of the following segment of code? int a = 5, b = 9; a = b + 15 * 2; b += a - 6 / 3; a += a + b; b++; printf("%d,%d\n", a,b); A) 120,43 B) 120,24 C) 124,47 D) 115,30 2) What is the output of the following segment of code? int x = 12, y = 4, z = 7; if(x > 2 * z) x = 3 * y; y += 5* z; z -= x * y + 4; else if(y * 2 > 6) x += 15 * 2; y = x + z * 2 / 7; z += x % y; else x = y + 12; y = x % z + 4; z = x + y * y % 2; printf("%d %d %d\n", x,y,z); 2
3) What is the output of the following segment of code? int a = 7, b, c; b = a % 6 + 5; c = b * a / (b % a); a /= (c % b); printf("%d, %d, %d\n", b, c, a); 4) What is the output of the following segment of code? (Hint: There may be more output lines than necessary) int i; int j; for(i = 0; i < 3; i++) for(j = i; j < 4; j++) printf("%d", i + j); if(j == 3) printf("\n"); else printf(" "); Output Line 1: Output Line 2: Output Line 3: Output Line 4: Output Line 5: Output Line 6: 3
5) What is the output of the following segment of code? int i = 25, a = 7, b = 13, c = 12; while(i % 10 == 0) a *= i % b * c + c * 2; b -= b % c * a / 4 * (b+a); c = c * a + b - (c * b + 2 * a); a -= b; b += a * 2; c += 5; i--; printf("%d %d %d\n", a, b, c); 6) What is the difference between a while loop, and a do while loop? 2 Pass by Value Functions 1) Write a function prototype for a function named findarea that returns an integer and has two integers named width and height as parameters 2) Given the function prototype above, fill in the function so that it finds the area of a square with dimensions width and height 4
3)What is the output of the following lines of code? int main(void) int x = 12, y = 15, z; // mult(a,b) returns a * b z = mult(y,3); // pow(a,b) returns a^b x = pow(1,2); z += pow(y,1) + x; printf("%d %d %d\n", x, y, z); return 0; 3 Statically Allocated Arrays 3.1 One Dimensional 1) Write a function that receives an integer array and an integer representing the size of the array, and returns the an integer that holds the largest value in that array 5
3.2 Two Dimensional 1) In main, write a program that creates a 4x4 two dimensional array and populates it with random values between 1-100 3.3 Strings and String Functions 1) Draw the boxes representing an array that contains the string hello 2) When using the string copy function, which argument is the destination and which is the source? 3) Does the string length function account for the null terminator? 4) True or False: strcat(str1, str2) returns the string str2str1 5) What does the string compare function return if two strings are lexicographically equivalent? 6
6) Fill in the following function that is given two strings of equal length and compares them. If they are the same, create a 3rd string that is the result of concatenating the second string with the first, and print that string void stringstuff(char str1[], char str2[]) 4 File I/O 1) Write a program that scans in all of the integers from a file named input.txt and outputs their sum. The file is of the following format: the first line contains an integer n, and will be followed by n integers. int main(void) 7
5 Pointers 1) What is wrong with the following lines of code? int x = 15; int *p = &x; *p = 9; printf("%d\n", p); 2) Fill in the blanks with the proper data types that each variable should have. r; c; h; i; a; r = &c; a = r; *r = 17; *h = a; *a = 99; i = &h; 3) How would we set the element at position 3 of an array called array to be equal to 7 using pointer notation? 8
6 Pass by Reference Functions 1) Fill in the following function that is given two pointers as parameters and then swaps their values void swap(int *x, int *y) 2) Use the function from the previous question so that the values of a and b are swapped int a = 5, b = 7; 9
7 Dynamic Memory Allocation 1) Fill in the following main function so that you ask the user to input an integer, and then use that integer to allocate an integer array of that size, and populate it with random values between 1-100 int main(void) 10
2) You are given a file called words.txt that contains an integer n, followed by n lines that contain one string each. Allocate a two dimensional character array that contains exactly n strings, and where each row of the array is allocated memory that is exactly equal to the size of the string int main(void) 11
8 Structs 8.1 Pointers to Structs 9 Linked Lists 12