CSE 130 Fall 2005 Solutions to Sample Final Exam 1. a. stackNode must be declared before it is used in the stack structure. b. 0 is the only integer value that can be directly assigned to a pointer. c. The type for m should be "enum month" 2. void addAtBeginning( float newValue ) { listNode *newNode= (listNode *) calloc( 1, sizeof( listNode ) ); if( newNode != NULL ) { newNode->next= head; newNode->value= newValue; head= newNode; } } 3. int findMedian( int data[], int N ) { int i, j, temp; /** Sort the array, so that the median must be in the middle **/ /** Note that this is not the most efficient way to implement a bubble sort, but that was not the purpose of this question **/ for( i= 0; i < N; i++ ) { for( j= 1; j< N; j++ ) { if( data[ j - 1 ] > data[ j ] ) { temp= data[ j - 1 ]; data[ j - 1 ]= data[ j ]; data[ j ]= temp; } } } /** Now return the middle object of the sorted array, which must be the median **/ return data[ N / 2 ]; } 4. int i; for( i= 5; i <= 50; i= i + 5 ) { printf( "%d\n", i * ( i + 1 ) / 2 ); } 5. struct time { int hour; int minute; int second; }; 6. { 23, 45, 1, 24, 19, 6 } { 87 } { 23, 1, 24, 19, 6 } { 45, 87 } { 23, 1, 19, 6 } { 24, 45, 87 } { 1, 19, 6 } { 23, 24, 45, 87 } { 1, 6 } { 19, 23, 24, 45, 87 } { 1 } { 6, 19, 23, 24, 45, 87 } {} { 1, 6, 19, 23, 24, 45, 87 } 7. void exchange( float *x, float *y ) { float temp= *x; *x= *y; *y= temp; }