CSC 212 Problems For Chapter 4 with Solution, Summer 2013

Notre Dame University–Louaize · Program Design and Data Abstraction I · 14 pages · with solution

All CSC 212 files
CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 1

Stuck on a question? A tutor can walk you through it.

CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 2 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 3 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 4 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 5 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 6 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 7 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 8 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 9 CSC 212 Problems For Chapter 4 with Solution, Summer 2013, page 10

Showing the first 10 of 14 pages. Download the PDF to see all of it.

More CSC 212 files

Show the text of this chapter
Chapter 4 Exercices 1 1) Write C++ statements to accomplish each of the following: a) Display the value of the seventh element of character array f. b) Input a value into element 4 of single-subscripted floating-point array b. c) Initialize each of the 5 elements of single-subscripted integer array g to 8. d) Total and print the elements of floating-point array c of 100 elements. e) Copy array “a” into the first portion of array “b”. Assume that the two arrays have the following declaration: double a[11], b[34]; f) Determine and print the smallest and largest values contained in 99-element floating-point array w. Answer: a) ANS: cout << f[ 6 ] << ’\n’; b) ANS: cin >> b[ 3 ]; c) ANS: int g[ 5 ] = { 8, 8, 8, 8, 8 }; or for ( int j = 0; j < 5; ++j ) g[ j ] = 8; d) ANS: float total = 0; for ( int k = 0; k < 100; ++k ) { total += c[ k ]; // assume total is declared and initialized cout << c[ k ] << ’\n’ } cout << total << ’\n’ e) ANS: for ( int i = 0; i < 11; ++i ) b[ i ] = a[ i ]; f) ANS: // assume all variables are declared and initialized for ( int j = 0; j < 99; ++j ) if ( w[ j ] < smallest ) smallest = w[ j ]; else if ( w[ j ] > largest ) largest = w[ j ]; Chapter 4 Exercices 2 2) Consider a 2-by-3 integer array “t”. a) Write a declaration for “t”. b) How many rows does “t” have? c) How many columns does “t” have? d) How many elements does “t” have? e) Write the names of all elements in the second row of “t”. f) Write the names of all the elements in the third column of “t”. g) Write a single statement that sets the element of “t” in row 1 and column 2 to zero. h) Write a series of statements that initialize each element of “t” to zero. Do not use a loop. i) Write a nested “for” structure that initializes each element of “t” to zero. j) Write a statement that inputs the values of the elements of “t” from the terminal. k) Write a series of statements that determine and print the smallest value in array “t”. l) Write a statement that displays the elements of the first row of “t”. m) Write a statement that totals the elements of the fourth column of “t”. n) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row. o) Write a statement that totals the elements of the second column of “t”. Answer: a) ANS: int t[ 2 ][ 3 ]; b) ANS: 2 c) ANS: 3 d) ANS: 6 e) ANS: t[ 1 ][ 0 ], t[ 1 ][ 1 ], t[ 1 ][ 2 ] f) ANS: t[ 0 ][ 2 ], t[ 1 ][ 2 ] g) ANS: t[ 0 ][ 1 ] = 0; h) ANS: t[ 0 ][ 0 ] = 0; t[ 0 ][ 1 ] = 0; t[ 0 ][ 2 ] = 0; t[ 1 ][ 0 ] = 0; t[ 1 ][ 1 ] = 0; t[ 1 ][ 2 ] = 0; i) ANS: for ( int i = 0; i < 2; ++i ) for ( int j = 0; j < 3; ++j ) t[ i ][ j ] = 0; j) ANS: for ( int r = 0; r < 2; ++r ) for ( int c = 0; c < 3; ++c ) cin >> t[ r ][ c ] ; or cin >> t[0] [0] >> t[0] [1] >> t[0] [2]>> t[1] [0] >> t[1] [1] >> t[1] [2]; k) ANS: int smallest = t[ 0 ][ 0 ]; for ( int r = 0; r < 2; ++r ) for ( int c = 0; c < 3; ++c ) if ( t[ r ][ c ] < smallest ) smallest = t[ r ][ c ]; cout << smallest; l) ANS: cout << t[ 0 ][ 0 ] << ’ ’ << t[ 0 ][ 1 ] << ’ ’ << t[ 0 ][ 2 ] << ’\n’; m) ANS: “t” does not contain a fourth column. n) ANS: cout << " 0 1 2\n"; for ( int r = 0; r < 2; ++r ) { cout << r << ’ ’; for ( int c = 0; c < 3; ++c ) cout << t[ r ][ c ] << " "; cout << ’\n’; } o) ANS: int total = 0; for ( int n = 0; n < 2; n++ ) total += t[n][1] Chapter 4 Exercices 3 3) Write single statements that perform the following single-subscripted array operations. a) Initialize the 10 elements of integer array counts to zero. b) Add 1 to each of the 15 elements of integer array bonus. c) Read 12 values for double array “monthlyTemperatures” from the keyboard. d) Print the 5 values of integer array “bestScores” in column format. Answer: a) ANS: int counts[ 10 ] = { 0 }; b) ANS: for ( int i = 0; i < 15; ++i ) ++bonus[ i ]; c) ANS: for ( int p = 0; p < 12; ++p ) cin >> monthlyTemperatures[ p ]; d) ANS: for ( int u = 0; u < 5; ++u ) cout << bestScores[ u ] << ’\t’; Chapter 4 Exercices 4 4) The following bubble sort is inefficient for large arrays void bubbleSort (int a[], int size){ int hold, pass; for ( pass = 1; pass < size; pass++ ) for( int j = 0; j < size - 1; j++ ) if ( a[ j ] > a [ j + 1 ] ) { hold = a [ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } } Make the following simple modifications to improve the performance: a) After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place”, and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on. b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed. // Exercise 4 Part A Solution void bubbleSort ( int a[], int size ) { int hold, pass; for ( pass = 1; pass < size; pass++ ) for ( int j = 0; j < size - pass; j++ ) if ( a[ j ] > a [ j + 1 ] ) { hold = a [ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } } Chapter 4 Exercices 5 // Exercise 4 Part B Solution void bubbleSort ( int a[], int size ) { int hold, pass; bool swapcheck; for ( pass = 1; pass < size; pass++ ) { swapcheck = false; for ( int j = 0; j < size - pass; j++ ) if( a[ j ] > a [ j + 1 ]) { swapcheck = true; hold = a [ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } if ( swapcheck == false ) break; } } Chapter 4 Exercices 6 5) Label the elements of 3-by-5 double-subscripted array sales to indicate the order in which they are set to zero by the following program segment: for ( row = 0; row < 3; row++ ) for ( column = 0; column < 5; column++ ) sales[ row ][ column ] = 0; Answer: sales[ 0 ][ 0 ], sales[ 0 ][ 1 ], sales[ 0 ][ 2 ], sales[ 0 ][ 3 ], sales[ 0 ][ 4 ], sales[ 1 ][ 0 ], sales[ 1 ][ 1 ], sales[ 1 ][ 2 ], sales[ 1 ][ 3 ], sales[ 1 ][ 4 ], sales[ 2 ][ 0 ], sales[ 2 ][ 1 ], sales[ 2 ][ 2 ], sales[ 2 ][ 3 ], sales[ 2 ][ 4 ] Chapter 4 Exercices 7 6) Use a single-subscripted array to solve the following problem. Read in 20 numbers between 10 and 100. As each number is read, save it only if it is not a duplicate of a number already read. Finally, print the array contents. // Exercise 6 Solution #include <iostream.h> #include <iomanip.h> int main() { const int SIZE = 20; int a[ SIZE ] = { 0 }, duplicate, value; cout << " Enter 20 integers between 10 and 100: \n"; for ( int i = 0; i < SIZE; ++i ) { duplicate = 0; do { cin >> value; } while (value < 10 || value > 100); for ( int j = 0; j <i; ++j ) if ( value == a[ j ] ) { duplicate = 1; break; } if ( !duplicate ) a[ i ] = value; } cout << "\n The nonduplicate values are: \n"; for ( int i = 0; i < 20; ++i ) if (a[i] != 0) cout << setw( 4 ) << a[ i ]; cout << endl; return 0; } Sample output: Enter 20 integers: 22 56 78 94 22 94 38 10 11 12 22 12 13 14 15 16 17 88 88 77 The nonduplicate values are: 22 56 78 94 38 10 11 12 13 14 15 16 17 88 77 Chapter 4 Exercices 8 7) Having 2 integer arrays sArr and dArr of the same size. Write a function that fills the array dArr with values between 1 and 100 such that each element of dArr is the corresponding value of array sArr rounded up to the next multiple of 10. Thus, 1 round to 10, 10 stays as 10, 11 rounds to 20, 20 stays as 20, 28 rounds to 30, and so on... // Exercise 7 Solution #include <iostream.h> #include <stdlib.h> #include <time.h> void roundArr (const int [], int [], int); void main () { const int size = 10; int sArr[size], dArr[size]; srand (time(NULL)); for ( int i = 0; i < size; i++ ) { sArr [i] = 1 + rand () % 100; cout << sArr [i] << " "; } cout << endl; roundArr ( sArr, dArr, size ); for ( int j = 0; j < size; j++ ) { cout << dArr [j] << " "; } cout << endl; } void roundArr ( const int srcA[], int dstA[], int aSize) { for ( int i = 0; i < aSize; i++ ) { if ( ( srcA [i] %10 ) == 0 ) dstA[i] = srcA[i]; else dstA[i] = srcA[i] + ( 10 - srcA[i] % 10 ); } } Chapter 4 Exercices 9 8) Having 2 two-dimensional arrays sArr and dArr of the same size. Write a function that fills the array dArr with values such that each element of dArr is the corresponding value of array sArr minus the minimum value of array sArr. Minimum Value sArr 14 3 21 6 43 64 23 96 10 13 8 7 dArr 11 0 18 3 40 61 20 93 7 10 5 4 // Exercise 8 Solution #include <iostream.h> #include <stdlib.h> #include <time.h> int findMin ( const int [][4], int, int ); void remBias ( const int [][4], int [][4], int, int, int ); void dispArr ( const int [][4], int, int ); void main () { const int rows = 3, cols = 4; int sArr[rows][cols], dArr[rows][cols]; int minimum; srand (time(NULL)); for ( int i = 0 ; i < rows; i++ ) for ( int j = 0; j < cols; j++ ) sArr [i][j] = 1 + rand() % 100; dispArr ( sArr, rows, cols ); minimum = findMin ( sArr, rows, cols ); cout << minimum << endl; remBias ( sArr, dArr, rows, cols, minimum ); dispArr ( dArr, rows, cols ); cout << endl; } Chapter 4 Exercices 10 int findMin ( const int arr[][4], int rSize, int cSize ) { int min = arr[0][0]; for ( int i = 0 ; i < rSize; i++ ) for ( int j = 0; j < cSize; j++ ) if ( arr[i][j] < min ) min = arr[i][j]; return min; } void remBias ( const int src[][4], int dest[][4], int row, int col, int min ) { for ( int i = 0 ; i < row; i++ ) for ( int j = 0; j < col; j++ ) dest[i][j] = src[i][j] - min; } void dispArr ( const int arr[][4], int rSize, int cSize ) { for ( int i = 0 ; i < rSize; i++ ){ for ( int j = 0; j < cSize; j++ ) cout << arr[i][j] << " "; cout << endl; } } What is the output Exercise Exercise 1: #include <iostream.h> int whatIsThis ( int [], int ); int main() { const int arraySize = 10; int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = whatIsThis ( a, arraySize ); cout << "Result is " << result << endl; return 0; } int whatIsThis ( int b[], int size ) { if ( size == 1 ) return b[ 0 ]; else return b[ size - 1 ] + whatIsThis ( b, size – 1 ); } Answer: Result is 55 Function WhatIsThis is a recursive function that adds the elements of the array Exercise 2: #include <iostream.h> void b ( int x ) { static int a[5]; if ( x == 6 ) { for ( int i = 0 ; i < 5 ; i ++ ) cout << a[i] ; cout << endl; } else ++a [x]; } void main ( ) { b ( 2 ); b ( 4 ); b ( 6 ); } Answer: 00101 Exercise 3: #include <iostream.h> void main ( ) { char phrase [] = “You are the best”; int cnt = 0, cnt2 = 0 ; for ( int i = 0; phrase[i] != ‘\0’; i++ ) { cnt = cnt + 1; if ( phrase[i] == ’o’ || phrase[i] == ’a’ || phrase[i] == ’i’ ) cnt2 = cnt2 + 1; } cout << cnt << ” “ << cnt2 << endl; } Answer: 16 2 For each element of the character array phrase, cnt is being incremented, so cnt holds the number of character in phrase. cnt2 is being incremented only if the letter ‘o’ or the letter ‘a’ or the letter ‘i’ is encountered in the array phrase.