CMPSC 16 PRACTICE MIDTERM 2 SUMMER 2014 All programming questions in this exam should be answered based on the programming language C.
Sample Exam Problem. Know the definitions of the following terms and be able to match them with a desciptive phrase (as in exam 1). abtraction, reusability, function invocation, function definition, function prototype, formal parameters, actual prameters, call-by-value, scope, global variable, local variable, recursive function, array, dereference operator, address operator, pointer, dynamic memory, static memory. Sample Exam Problem. Consider an input file input.txt with the following content: CS 16 8/5.2014. The first character in the file is C and the last character is.. There is a blank (space) character after "CS". Consider the code segment below. Show the contents of the file output.txt after this code is executed by completing the output shown below: CODE SEGMENT: #include <stdio.h> int main(void) { char c; int value = 0; int i; FILE * input, * output; input = fopen("input.txt", "r"); output = fopen("output.txt", "w"); fscanf(input, "%c", &c); fprintf(output, "c: %c\n\n", c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value); fscanf(input, "%c", &c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value); OUTPUT: c: value: value: c: value: for(i = 0; i <= 6; i++) fscanf(input, "%c", &c); fprintf(output, "c: %c\n\n", c); fscanf(input, "%d", &value); fprintf(output, "value: %d\n\n", value);
Sample Exam Problem. Show the contents of the array x after all the statements in the following code segment are executed. List the contents of x starting with the first element of the array, and write a question mark? if the contents are not defined. CODE SEGMENT: CONTENTS OF x: int x[5]; x[2] = 1; x[3] = 5; x[x[2]] = x[2] * 2; x[5 - x[1]] = 9; /* What are the contents of x here? */ Sample Exam Problem. Write a C programming statement using malloc that will declare a pointer named xp which points to a dynamically allocate block of memory with space for 10 integer values. Fill in your answer on the blank line. (Hint: see the lab on pointers). int * xp =
Sample Exam Problem. Given the following declarations, show the results of evaluating the following expressions. If the expression accesses a section of memory that contains unknown values write?. (Hint: for studying, try it out in ch) int y[6] = { 2,3,5,7,11,13; int *yp = &y[2]; EXPRESSION: *y VALUE: *yp *(yp + y[0]) *yp + y[0] yp[4] + 1
Sample Exam Problem. Given the following code segment, show the output that will be generated by this code by completing the partial output shown below. CODE SEGMENT: #include<stdio.h> int value = 0; void func1(int x) { x = 5; void func2(int *x) { *x = 10; void func3(int x) { value = x; int main(void) { int x = 0; func1(x); printf("x is: %d\n\n", x); func2(&x); printf("x is: %d\n\n", x); OUTPUT: x is: x is: value is: value is: x is: value is: value is: func1(value); printf("value is: %d\n\n", value); func2(&value); printf("value is: %d\n\n", value); func3(5); printf("x is: %d\n\n", x); printf("value is: %d\n\n", value); func2(&value); func3(value - 1); printf("value is: %d\n\n", value);
Sample Exam Problem Write a code segement to compute the sum of three 3-by-3 matrices. We want to compute D = A + B + C. Fill in the missing portion below. #include<stdio.h> int main(){ int A[3][3] = {{-1,3,6,{19, -8-1,{-8, -6, 1; int B[3][3] = {{12, 0, 0, {5, 2, -1, {6,5,7; int C[3][3] = {{9,8,7, {1,2,3, {5,4,6; int D[3][3]; int i, j, n = 3; // Compute D = A + B + C // Fill in the missing code here // print D printf("\nmatrix D = A + B + C:\n"); for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ printf("%3i ", D[i][j]); printf("\n"); The program should produce the following expected output: Matrix D = A + B + C: 20 11 13 25-5 2 3 3 14
Sample Exam Problem. Consider the imaginary memory layout below in which addresses are four digit integers and all values stored in memory are represented as positive integers. Assume that an integer takes up 4 bytes, so that adjacent memory addresses differ by 4. So, incrementing an address by 1 results in an address that is 4 bytes away. For example, &y is 1012, &y + 1 is 1016, &y + 2 is 1020, and so on. The addresses, memory contents, and variable names associated with particular memory blocks are shown in the table. Address 1000 1004 1008 1012 1016 1020 1024 1028 Memory Contents 4 1024 10 5 1004 16 1012 1 Variable x y z Fill in the following table showing the value of the given expressions. EXPRESSION: x VALUE: *1008 &y *&y &x+1 *z+4 **(&x+4) *(&z+1)
Sample Exam Problem. Write a function that searches for a value in an array. The function will take three arguments: 1) an integer value that is greater than 0 and indicates the size of the array, 2) an integer array of that size, and 3) an integer value that we would like to search. The function should return an integer value. It should return -1 if the value that is being searched is not in the array. Otherwise, it should return the position of the value in the array (return 0 for the first position). Assume that there are no duplicates in the list - every item is unique. #include <stdio.h> int findval(int size, int values[], int value); int main(void) { int arraysize =...; int result; int value =...; int myarray[] =...; result = findval(arraysize, myarray, value); if (result == -1) printf("the value %d is not in the list.\n", value); else printf("the value %d is at position %d in the list.\n", value, result); int findval(int size, int values[], int value) { int result, i; /* write the missing part below */ return result;
Sample Exam Problem. Write a recursive function that adds all the elements in an array. The function will take two arguments, 1) an integer value that is greater than 0 and indicates the size of the array, and 2) an integer array of that size. The function should return an integer value that is the sum of the elements in the array. The function should add elements of the input array using recursion and should not use a loop. #include <stdio.h> int recursivesum(int size, int values[]); int main(void) { int arraysize =...; int result; int myarray[] =...; result = recursivesum(arraysize, myarray); printf("the sum is %d\n", result); int recursivesum(int size, int values[]) { /* write the missing part below */