CSE 130 Fall 2008 Sample Exam-2 (1) [1+1+2+2+2+2] Short questions. (a) A void function does not have any return statement. (b) Both declarations, [int* p;] and [int *p;] (given inside square brackets for clarity), declare p as an integer pointer. (c) What is the storage class of local variables declared inside a function? ___________ (d) When we declare an enumerated type, compiler assigns integer values to enumerators/members of the enumerated set. What value is assigned to yellow in the following example? _____ enum color { green, red, blue, yellow } (e) Using `typedef' declare another type mycolor which renames the type declared in (d). (f) Declare a variable y of type mycolor declared in (e), and assign it a value blue. This should be just one statement. (2) (a) [4 points] Sort the array [40, 10, 7, 25, 32] using bubble sort. Show array after each pass. (b) [3 points] Suppose we want to stop bubbleSort algorithm as as soon as array is sorted, what will you check/test to stop the algorithm (get out of the loop). No code is expected just describe an idea. (3) [5+1+1 points] For the given array of characters, do a binary search looking for Y. Show all steps, calculation of midpoints, and all comparisons. How many times did you calculate the midpoint? When did you decide that `Y' is not on the list? Array ==> [A, E, G, K, M, P, Q, R, W] Array index starts at 0. (4) (a) [4 points] What value is computed by the recursive function mystery for (i) n = 3? ___ (ii) n = 4? ___ (iii) and n = 5? ___ (iv) and n = 6? ___ (b) [4 points] Function mystery returns either 1 (true) or 0 (false) depending on value of n. What exactly function mystery computes/checks (when it returns a value back to its calling function)? Express your answer in terms of parameter n. Assume that n > 0. int mystery (int n) (b) Space for your answer. { if (n==0) return 1; else if (n==1) return 0; else if (n==2) return 0; else return mystery (n-3); } (5) Complete the program given below that finds and prints the second largest element/value of array A. int main (void) { const int size = 7 int A[size] = { 7, 31, 8, 4, 10, 20, 5 }; int index, i = 2; int largest = A[0], s_largest = A[1]; while (i < size) { } } (a) [3 points] There is a logical error in the part of this program above while loop. What is it, and how will you fix it? Do not write code. Just explain. (b) [5+1 points] Complete the while loop and print. (c) [2 points] Give a list of 7 integers, such that the this program does very little or the least amount of work while determining the second largest value. (6) (a) [2 points] Declare a structure amount with two members, int dollars and int cents. (b) [2+4 points] Write a function amountCheck that takes two parame- ters x and y of type struct amount. It returns an integer value 0 if amounts x and y are equal, else if amount x is less than amount y, it returns 1, else it returns 2. (No credit, if you convert amount to cents and check.) (7) [6 points] Complete the program given below such that the right upper triangular part of the array B is initialized to all zeros. Do not worry about the rest of B. See the example. Example: A 4 x 4 array with its 1 0 0 0 upper triangular part set to 0. 2 2 0 0 3 3 3 0 4 4 4 4 int main () { const int size = 10; int B[size][size]; int i, j; // Write code for Right Upper Triangle Only. // Initialize it to all zeros. Do not worry about the rest of B. // You do not need extra variables. (8) (a) [2+5 points] Write a function err_percent that takes two dou- ble parameters x and y, and returns the percentage error with respect to x. It calculates percentage error by the following formula. Percentage_error = (positive difference between x and y)*100/x At the end, return percentage_error of type double. You may use addi- tional local variables. (9) [6 points] Consider the following program with pointers. What values are printed at the end? f1 = ____, f2 = _____ int main ( ) { float f1 = 1.0, f2 = 2.0; float *fp1, *fp2; fp1 = &(f1); fp2 = fp1; *fp2 = f2 * (*fp1 + *fp2); fp2 = &(f2); *fp1 = *fp2 + *fp1; printf("f1 = %f, f2 = %f\n", f1, f2); return 0; }