Computer Science 228 - Spring 1997 Exam 1 Solutions ---------------------------------- // Question 1 #include // For int abs( int ) void list::tail-insert( const int x ) { // Declare and initialize the new node to be inserted listnode * nnode = new listnode; nnode->data = x; nnode->next = NULL; // Check special case of empty list if ( head == NULL ) head = tail = nnode; else tail = tail->next = nnode; retrun; } //-------------------------------------------------------------- void list::make-positive() { // Declare a pointer to traverse the list, start at head. listnode * tmp = head; while ( tmp != NULL ) tmp->data = abs( tmp->data ); return; } // Question 2: class listnode { friend ostream & operator<<( ostream &, const listnode & ); public: listnode( const listnode & ); const listnode & operator=( const listnode & ); listnode operator-( const listnode & ) const; listnode operator++(); private: int data; listnode * next; }; /*------------------------------------------------------------ Criteria/points: ---------------- 3 - Basic class declaration ( class, private, public... ) 2 - Copy constructor 2 - Assignment operator 2 - Subtraction operator ( int or listnode accepted ) 1 - Increment operatorn ( almost anything accepted ) 2 - Stream insertion operator 2 - Data members -- 14 - Total ------------------------------------------------------------*/ // Question 3 #include void printArray( int a[], int size ) { for( int j = 0; j < size; j++ ) cout << a[ j ] << " "; return; } int main() { // This part given: const int arraySize = 10; int bits[ arraySize ] = { 1 ,0, 0, 0, 1, 1, 0, 1, 1, 0 }; int tmp, i, n; int * p, *q; cout << "BEFORE: "; printArray( bits, arraySize ); cout << endl; // Solution part A: tmp = bits[ arraySize -1 ]; for ( i = arraySize - 1; i > 0; i-- ) bits[ i ] = bits[ i - 1 ]; bits[ 0 ] = tmp; // Sample output added: cout << "AFTER A: "; printArray( bits, arraySize ); cout << endl; // Given part B, We'll just use what we had.... p = bits; q = p + 1; n = arraySize/2; for ( i = 0; i < n; i++ ) { // fill in the code to exchange the values at the // locations given by p and q tmp = *p; *p = *q; *q = tmp; // fill in the code to advance the pointers p = p + 2; q = q + 2; } // Sample B output added: cout << "AFTER B: "; printArray( bits, arraySize ); cout << endl; return 0; } /* Sample output: -------------- BEFORE: 1 0 0 0 1 1 0 1 1 0 AFTER A: 0 1 0 0 0 1 1 0 1 1 AFTER B: 1 0 0 0 1 0 0 1 1 1 */ // Question 4 #include void printArray( int * a, int n ) { for ( int k = 0; k < n; k++ ) cout << a[k] << " "; cout << endl; } int main() { const int n = 10; int A[n] = { -1, 7, 10, 5, 11, 17, 6, 0, 1, 10 }; int tmp, i, j; for ( i = 0; i < ( n - 1 ); i++ ) { for ( j = i; j < ( n - 1 ); j++ ) if ( A[j] < A[j + 1] ) { tmp = A[j]; A[j] = A[j + 1]; A[j + 1] = tmp; } if ( i == 0 ) printArray( A, n ); } printArray( A, n ); return 0; } /*------------------------------------------------------------ Output after the first iteration of the outer loop: 7 10 5 11 17 6 0 1 10 -1 Output at the end: 7 10 17 11 10 6 5 1 0 -1 Explanation: A failed attempt at sorting the array in decreasing order. Part B: ------- i. 32 ii. a ^ b ( a raised to the power b ) If b was not positive it would never reach the base condition of b == 1 because it would continue to grow in the negative direction. This would cause an infinite recursive call. -----------------------------------------------------------*/