{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# ex2KEY - COP 3502 Exam#2 Summer 2002 KEY NAME July 8...

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

Array X 1. (10 points-total) Given the following global array of numbers and the algorithm shown below, answer questions (a) and (b) Array X index 0 1 2 3 4 5 6 value 4 6 2 1 7 3 5 void function Test_2(int a, int b) { int i=0, j=0, k=0; while (a <= b) { if (X[a] > X[b]) { i = X[a]; X[a] = X[a] – X[b]; a++; } else if (X[a] < X[b]) { j = X[b]; X[b] = X[b] + X[a]; b--; } else { a++; k = X[a] + X[b]; b--; } } } (a)[7 pts] Show the array X after the loop has finished assuming is was called with Test_2(1, 6) . index 0 1 2 3 4 5 6 value 4 1 1 1 9 5 7 (b)[3 pts] What value will the following variables contain after the loop is finished? variable i j k value 2 7 10 2. (10 points) 1 COP 3502 – Exam #2 – Summer 2002 NAME: July 8, 2001 (100 points) KEY

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

View Full Document
Given the following array of integers, show the first five passes of the bubble sort on this array. Array X index 0 1 2 3 4 5 6 7 8 9 10 11 value 41 36 22 13 17 32 25 34 28 3 27 14 index 0 1 2 3 4 5 6 7 8 9 10 11 initial 41 36 22 13 17 32 25 34 28 3 27 14 pass 1 3 41 36 22 13 17 32 25 34 28 14 27 pass 2 3 13 41 36 22 14 17 32 25 34 28 27 pass 3 3 13 14 41 36 22 17 25 32 27 34 28 pass 4 3 13 14 17 41 36 22 25 27 32 28 34 pass 5 3 13 14 17 22 41 36 25 27 28 32 34 3. (10 points) Write a recursive function (in C) which will correctly print the first n odd integer numbers. You must define this function using only a single numeric parameter to be passed to the procedure. Assume that the initial call has the form: print_odd(n) where n indicates how many odd numbers are to be printed. For example, the call print_odd(6) would produce the following output: 1 3 5 7 9 11. Assume that n > 0. 4. (10 points) 2 void function print_odd (int n) { //print the first n odd integers recursively. // Assume n > 0. if (n ==1) printf(“1 \n”); else { print_odd(n-1); printf(“%d “, (2*n)-1); } }//end function print_odd
The following program produces exactly two lines of output. Show exactly what is output by this program. Assume execution begins in the algorithm Q3.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}