CSE 130 Fall 2008 Solutions to Sample Exam-2 1. (a) N (b) Y (c) Automatic (d) 3 (e) typedef enum color mycolor; (f) mycolor y= blue; 2. (a) 10, 7, 25, 32, 40 (2 points) 7, 10, 25, 32, 40 (2 points) (b) Check if swaps are performed. If no swaps are performed, the array is sorted. 3. first= 0, last= 8 mid= ( 0 + 8 ) / 2, Y < M first= 5, last= 8 mid= ( 5 + 8 ) / 2, Y < Q first= 7, last= 8 mid= 7, Y < R first= 8, last= 8 mid= ( 8 + 8 ) / 2, Y > W first= 9, last= 8 first > last --> We are done ! Y is not on the list midpoint is calculated 4 times. 4. (a) i. 1 ii. 0 iii. 0 iv. 1 (b) Mystery returns true if n is divisible by 3. It returns false otherwise. 5. (a) The given code assumes that A[0] > A [1]. This is not always the case, as seen in the code given. The way to fix this error is to check which of A[0] and A[1] is larger, and assign each to "largest" and "s_largest" appropriately (b) /* A sort is unnecessary here. */ 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]; // Fix the error as mentioned in (a) above. while( i < size ) { if( A[i] > largest ) { s_largest= largest; largest= A[i]; } else if( A[i] > s_largest ) { s_largest= A[i]; } i++; } printf( "s_largest is %d\n", s_largest ); return 0; } (c) 7, 6, 5, 4, 3, 2, 1 Any decreasing sequence. 6. (a) struct amount { int dollars; int cents; }; (b) int amountCheck( struct amount x, struct amount y ) { if( x.dollars < y.dollars ) return 1; else if( y.dollars < x.dollars ) return 2; else if( x.cents < y.cents ) return 1; else if( y.cents < x.cents ) return 2; else return 0; /* x == y */ } 7. /* There are other valid answers. */ int main() { const int size= 10; int B[ size ][ size ]; int i, j; for( i= 0; i < size; i++ ) { for( j= i+1; j < size; j++ ) { B[i][j]= 0; } } return 0; } 8. double err_percent( double x, double x ) { double error; if( x > y ) error= x - y; else error= y - x; error= ( error * 100 ) / x; return error; } 9. f1= 6.0, f2= 2.0; This is how it works: Pointers "point" to a variable, but do not make a copy of it. They become an alias, or another name for the same variable. In this example, f2 never changed, and f1 changed in ways that may have been unexpected. Breakdown: (fp1 ==> f1, means that fp1 points at f1, and *fp1 IS f1) int main() { float f1= 1.0, f2= 2.0; float *fp1, *fp2; /* At this point f1= 1.0, f2= 2.0, fp1 and fp2 point at garbage. */ fp1= &(f1); /* fp1 ==> f1, fp2 ==> ??? */ fp2= fp1; /* f1= 1.0, f2= 2.0, fp1 ==> f1, fp2= fp1, or: fp2 ==> f1 */ *fp2= f2 * ( *fp1 + *fp2 ); /* Right hand side: f2 * ( f1 + f1 ) --- evaluates to ---> 4.0 * Left hand side: f1 * The statement is equivalent to: f1= f2 * ( f1 + f1 ) * Result: f1= 4.0, f2= 2.0, fp1 ==> f1, fp2 ==> f1 */ fp2= &(f2); /* f1= 4.0, f2= 2.0, fp1 ==> f1, fp2 ==> f2 */ *fp1= *fp2 + *fp1; /* Right hand side: f2 + f1 --- evaluates to ---> 4.0 + 2.0 which is ---> 6.0 * Left hand side: f1 * This statement is equivalent to: f1= f2 + f1 * Result: f1= 6.0, f2= 2.0, fp1 ==> f1, fp2 ==> f2 */ /* Print the results: f1= 6.0, f2= 2.0 */ printf( "f1= %f, f2= *f\n", f1, f2 ); return 0; }