| Quiz | Posted (Thursdays) |
Given in class (Thursdays) |
||
|---|---|---|---|---|
| 26 | Sep | 3 | Oct | |
You will fare better on the quiz if you try working the problems before looking at the solutions. If you don't understand the question or its answer, please get help.
Directions: Study the following recursive procedures and answer the questions that follow. Do these practice exercises on paper. Do not type them into a computer. You'll learn much more doing them by hand.
int factorial(int n) {
if (n <= 1)
return 1;
else
return (n * factorial(n-1));
}
int sum1toN(int n) {
if (n < 1)
return 0;
else
return (n + sum1toN(n-1));
}
int add(int i, int j) { // assumes i >= 0
if (i == 0)
return j;
else
add(i-1, j+1);
}
int fib(int n) { // assumes n >= 0
if (n <= 1)
return n;
else
return (fib(n-1) + fib(n-2));
}
factorial(5).
For this same computation, draw the execution stack as it would
look just before returning from factorial(1),
the last recursive call.
sum1toN(5).
add(5,9).
For this same computation, draw the execution stack as it would
look just before returning from add(0,14),
the last recursive call.
fib(5).