Review for Quiz 7

One-page version suitable for printing.

Question 7-1

Consider the following recursive class method.

public static void printStuff(int n) {
    if(n > 0) {
        printStuff(n - 1);
        IO.println(n);
        printStuff(n - 2);
    }
}
Suppose a program called printStuff(4). What would the computer print?

Solution

Question 7-2

Consider the following recursive class method.

public static int f(int n) {
    if(n <= 1) {
        return 1;
    } else {
        return f(n - 1) + f(n / 2);
    }
}
Draw the recursion tree illustrating how this function would compute f(5).

Solution