600.109 INTRO TO PROGRAMMING IN C/C++ Spring ANSWERS for PRACTICE TEST 2 What is the exact output of each code segment? -> cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(4); n1 = 1; while (n1 <= 3) { r1 = pow(3,n1) / 8.0; cout << r1 << endl; n1++; } 0.3750 1.1250 3.3750 -> Write a code segment, including all variable declarations, to read and output the sum of a bunch of integers, until the end of input occurs. int num, sum = 0; while (cin >> num) { sum += num; } cout << sum << endl; -> Standard input and output operators and functions (such as << and >>) may be used with which type of external file? sequential text files (not random access) -> All external files must be initialized with which ifstream function before it can be accessed in a program? open (or constructor with parameter) -> Write a statement to create a stream for writing to an external text file called "outfile.txt". ofstream of("outfile.txt"); -> Given the following array, write the array as it exists in memory after the first pass of the outer loop of a SELECTION SORT. r = {17, 32, 1, 22, 5} after 1st pass: r = 1 32 17 22 5 -> Given the following array, write the array as it exists in memory after the first pass of the outer loop of a BUBBLE SORT. r = {17, 32, 1, 22, 5} after 1st pass: r = 17 1 22 5 32 -> Given the following array, what numbers would be looked at (in what order) during a BINARY SEARCH for the number 2? (State any assumptions you make about the search details.) r = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} assume div (truncate) to get middle index - (left of middle) 8 4 2 assume round up to get middle index (right of middle) 9 5 3 2 -> Given the following array, what numbers would be looked at during a LINEAR SEARCH for the number 12? r = {23, 13, 29, 31, 20, 30, 12, 24, 90} 23 13 29 31 20 30 12 -> If an array is sorted already, which type of search is faster: binary or linear? binary -> If an array has N elements, what is the time complexity for a bubble sort? O(N^2) (N squared) -> What sorting algorithm is faster than a selection sort? mergesort, quicksort -> What is the output of the following program? /************************ ACTUAL PROGRAM *********************/ #include void funk1 (int, float); int funk2 (double &, int); int main() { const int m = 15; int i, j, k; float y; double x, z; i=4; y=2.2; funk1(i, y); funk1(15, 0); k = 13; for (i=8; i<=20; i+=3) { cout << i << ' ' << k << endl; k++; } x = 3; cout << "funk2: " << funk2(x,3) << endl; cout << "x: " << x << endl; z = 11.5; cout << "funk2 again: " << funk2(z,-2) << endl; cout << "z: " << z << endl; return 0; } // end of main void funk1 (int i, float x) { int count = 1; float result=0; while (count <= i) { result += x; count++; } cout << result << endl; } int funk2 (double & num, int rep) { int count = 0; do { num = num + 1.1; rep--; count++; } while (rep > 0); return count; } /** OUTPUT 8.8 0 8 13 11 14 14 15 17 16 20 17 funk2: 3 x: 6.3 funk2 again: 1 z: 12.6 **/ -> Write a loop which prints the odd integers from 12 down to 3 (including 3). Include necessary variable declarations. for (int i=11; i >= 3; i -= 2) { cout << i <<" "; } // or int i = 12; while (i >= 3) { if (i%2 == 1) cout << i << " "; i--; } -> What is the output of the following program? #include int main () { int ra[] = {5, 10, 15, 20, 25}; doit(ra, 5); } void doit(int r[], int i) { for ( ; i > 0; i--) { cout << r[i-1] << " "; } cout << endl; } ================ 25 20 15 10 5 ================ -> What is the output of the following program? #include int main () { const int MAX=4; char w[] = "spring fair"; double r[MAX]; int t[4][6]; for (int i=0; i=0; n-=2) cout << r[n] << ' '; cout << endl; cout << w[5] << endl; for (int k=0; k<6; k++) for (int j=0; j<4; j++) t[j][k] = j + k; cout << t[2][1] << endl; return 0; } ========= 0 1 2 3 9 1 g 3 =========