COP 3223 Introduction to Programming with C - Study Union - Spring 2018 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 + 3 * 2; b += a - 6 / 3; a = a + b; b++; printf("%d,%d\n", a,b); A) 23,37 B) 36,22 C) 37,23 D) 38,24 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 += y % x; 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? (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
4) 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); 5) 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 findsum that returns an integer and has one integer named input as a parameter 2) Given the function prototype above, fill in the function so that it finds the sum of the numbers from [0,input] inclusive. 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 to the bth power 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 amount of even numbers in the 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) What would be the array elements of a string that contains the word 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 a single string. If the string contains every letter of the alphabet, return 1, otherwise return 0. void stringstuff(char str[]) 4 File I/O 1) Write a program that scans in all of the integers from a file named input.txt. The file is of the following format: the first line contains an integer n, and will be followed by n integers. Print out the amount of numbers that are divisible by n. int main(void) 7
5 Pointers 1) What is wrong with the following lines of code? How can it be fixed? 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) Call the function from the previous question so that the values of a and b are swapped int a = 5, b = 7; 3) What is the output of the following program? int foo(int *a, int *b) *a = 2 + *a - *b; *b = *b + *a; printf("a = %d, b = %d\n", *a, *b); return 2+(*b); int main() int x = 12, y = 4, z = 3; y = foo(&x, &z); printf("x = %d, y = %d, z = %d\n", x, y, z); return 0; 9
7 Dynamic Memory Allocation 1) What are the differences between the malloc function and the calloc function? 2) 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 the array with values that are the negative of their index. int main(void) 10
3) 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 1) Create a struct named Employee that contains a character array of size 50 for their name, an int for their id, and a float for their wage 2) Using the struct created above, create a variable named employee1 with the name Bob, an id of 15, and a wage of 18.50 8.1 Pointers to Structs 3) if we have a pointer x to the employee struct from the previous two questions, show the two ways to change the wage of the struct pointed to by x to 19.00. 9 Linked Lists 1) What are some differences between Linked Lists and Arrays? 12
2) Given the following struct for a linked list node, write a function that is given the head of the list, and the value of the largest number in the list, and returns the value that appears most often in the list. typedef struct node int data; struct node *next; node; int mostoften(node *head, int largestnum) 13