COMP 11 - Introduction to Computer Science - Fall 2002

Week 7: Additional Questions for Chapters 6 and 18

1.
What does the following function return on the call f1(5,6)?
int f1(int x, int y) {
int z;
z = x;
x = y;
y = z;
return z;
}


2.
Below we give the main program calling f1() from the previous part. What does the program print?
int main() {
int a=1,b=2,x=3,y=4,z=5;
x = f1(a,b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "x= " << x<< endl;
cout << "y= " << y << endl;
cout << "z= " << z << endl;
}


3.
What does the following function return on the call f2(5,6)?
int f2(int x, int & y) {
int z;
if (x>y)
return(2*x);
else {
y = 2*y;
return(y/2);
}
}


4.
Below we give the main program calling f2() from the previous part. What does the program print?
int main() {
int a=1,b=2,x=3,y=4,z=5;
x = f2(a,b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "x= " << x<< endl;
cout << "y= " << y << endl;
cout << "z= " << z << endl;
}


5.
What does the following function return on the call f3(5,30)?
int f3(int x, int  y) {
int result;
if (x>y)
result = x;
else
result = f3(x+10,y);
cout << "Returning " << result << endl;
return result;
}


6.
What does the function print when called as in the previous part?

7.
Professor Numbers who works with MegaFlies has discovered that the following equation describes the population size of MegaFlies over time when starting with one MegaFly at time point 1: size(1) =1 and size(t) = 5 \times size(t-1) +3. Write a recursive function size() taking one integer argument t that and returns the size of a population at time t.

Details TBA

Week 11: Additional Questions for Chapters 9 and 18

The questions refer to the arrays defined below:

  int a[10] = {1,9,4,6,8,6,3,5,5,1};
int b[10] = {1,11,21,31,41,51,61,71,81,91};


1.
What numbers in b[] are compared when binary search is searching for the number 35? Please list these numbers in sequence and explain your answer.
2.
What numbers in b[] are compared when binary search is searching for the number 81? Please list these numbers in sequence and explain your answer.
3.
Describe the operation of bubble sort when sorting the array a[]. You should give the contents of the array after each round (that is at the end of each iteration of the do-while loop in the program on page 605 in your textbook).
4.
Describe the operation of selection sort when sorting the array a[]. You should give the contents of the array after each round (that is at the end of each iteration of the for loop involving startScan in the program on page 609 in your textbook).
5.
Describe the operation of quick-sort (the program on page 1145 in your textbook) when sorting the array a[] as follows. You do not need to show all the operation of the algorithm but you should show one level deep into the recursion.

More precisely, show the contents of the array after the first call to partition and specify the value of pivotIndex. Then for each part obtained do the same showing the result of running partition and the pivotIndex.

Details TBA