Final sol4 - Computer Science I Spring 2009 Final exam 1 8...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science I Spring 2009 Final exam 1 2 3 4 5 6 7 8 9 10 11 12 8 12 12 8 6 2 2 6 18 10 10 6 Closed book, closed notes, no computers or calculators of any kind. Name: RCS login: Circle your lab section: (01 MTh 10:00) (02 MTh 12:00) (04 MTh 4:00) (05 MTh 6:00) (06 TF 10:00) (07 TF 12:00) 1. Show the output displayed by the following program as it would appear on the screen of a computer. (8 points) #include <iostream> using namespace std; int main () { int a[4][4]; int b, c, d, e; for (b=3; b >= 0; b--) { for (c = 0; c < 4; c++) { if ((b+c) % 2 == 0 ) { a[b][c] = b; } else { a[c][b] = b; } } } for (d = 0; d < 2; d++) { for (e = 0; e < 4; e++) cout << a[e][d] << " "; cout << endl; } return 0; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
0 0 2 0 1 1 1 3 1 point per value 2. Write functions to do the following. a. Compute the factorial of a positive number (4 points). b. Count how many items on the border (top, left, right, bottom) of a 10 by 20 array have the value 6. (8 points). int fac ( int n) { if (n = 1) return 1 ; else return n * fac (n – 1); } 2 point for simple case and 2 point for simplifying case int border_count ( int array[][20]) { int res = 0; for ( int i = 0; i < 20; i++) { if (array[0][i] == 6) res++; } for ( int i = 0; i < 20; i++) { if (array[9][i] == 6) res++; } for ( int i = 1; i < 9; i++) { if (array[i][0] == 6) res++; } for ( int i = 1; i < 9; i++) { if (array[i][19] == 6) res++; } return res; } 2 point for each loop Remove 1 point each time 6 can be found twice at a[0][0], a[9,19], a[0][19] or a[9][0]
Background image of page 2
3. Consider an unsorted list with 1,000,000 numbers in it stored in a one-dimensional array. And assume log (1,000,000) = 20 a) In the average case, approximately how many numbers in the list will have to be examined to find a particular number if the list has been sorted? (2 points) 20 b) In the average case, approximately how many operations must be performed to sort the list using Merge Sort. (2 points) 20, 000,000 c) If only one number is out of order in a list and it is two locations from where it needs to be how many bubble operations must be performed to sort the list using Bubble Sort. Explain your answer. (4 points) 2,000,000 or 3,000,000 depending if move is forwards or backwards (3 points) 1 or 2 passes to move the number and 1 pass to deduce that list is now sorted
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 9

Final sol4 - Computer Science I Spring 2009 Final exam 1 8...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online