### CS51 - Spring 2010 - Lecture 14

• Exercise 12.1.13: What is it about broccoli that makes it challenging to try and write iteratively, even just to create it?

• procedural recursion
- so far, we've only looked at structural recursion
- much more commonly seen in CS is what's called procedural recursion
- writing a method that calls itself
- similar to mathematical induction
- basic idea
- assume you have a method that does what you works, but it only works on problems that are smaller than your current problem
- how can you use this method to solve the larger problem...
- steps for procedural recursion
- define the recursion in words
- assume you have a method that works for smaller problems, define the solution to the larger problem with respect to this method
- base case: when does the recursion end?
- determine for what input(s) the method will stop and
- what the answer is for that input
- write the method
- first, check for the base case and take the appropriate base case action
- otherwise
- do the recursive case
- make a "recursive" call to your method on something that is smaller

• summing up the numbers from 1 to some input, n
private int sum(int n){
...
}

- iterative version:
private int sum(int n){
int totalSum = 0;

while( n > 0 ){
totalSum += n;
n--;
}

}

- recursive version:
- how can we define this recursively (in words)?
- the sum of the numbers from 1...n is n plus the sum of the numbers from 1...n-1
- what is our recursive case?
- sum(n) = n + sum(n-1)
- what is our base case? When do we stop?
- if n is equal to 1, then we know that the sum is 1
- write the method:

private int sum(int n){
if( n == 1 ){
return 1;
} else {
return n + sum(n-1);
}
}
- why does this work?
- first, we call it with n
- we return n plus whatever is returned by sum(n-1)
- which returns n-1 plus whatever is returned by sum(n-2)
- so we get n + n-1 + n-2 + n-3 + ...
- eventually, n == 1 and we return a 1
- so our final sum is: n + n-1 + n-1 + n-3 + ... + 1, which is what we wanted

• show Powers demo
- we want to calculate base to the power n
- how can we do this?
- basic idea is that we want to multiply 1 times base n times
- iteratively:

private double itPower(double base, long n){
double ans = 1.0;
while (n > 0) {
ans = ans * base;
n--;
}

return ans;
}

- recursively:
- how can we define this recursively (in words)? Remember, just assume you have a method that does what you want it to do, but for smaller problems
- the base^n is base times base^(n-1)
- what is our recursive case:
- power(base, n) = base * power(base, n-1)
- what is our base case
- when n == 0 we should just return 1
- alternatively, when n == 1, we could just return base
- either one is fine! what would be the difference?
- write the method:

private double power(double base, long n) {
if (n == 0) {
return 1;
} else {
return base * power(base, n - 1);
}
}

- why does this work?
- first we call it with base and n
- we return base times whatever is returned by power(base, n-1)
- which returns base times whatever is returned by power(base, n-2)
- so we best base * base * base * ...
- eventually, n == 0 and we return 1, so we get:
- base * base * base * ... * base * 1, where base is multiplied n times since each time we reduce n by 1

• show Hanoi demo
- called towers of hanoi
- invented in 1883 by French mathematician Edouard Lucas (http://en.wikipedia.org/wiki/Tower_of_Hanoi)
- simple game:
- move the tower from the left side to the right
- you may only move one disc at a time
- you may only place a smaller disc on top of a larger disc
- how do you solve this? Recursion!
- harder to solve iteratively
- how can we define this recursively (in words)?
- to solve the puzzle:
- move the top n-1 discs to the center peg
- move the bottom disk to the right peg
- move the n-1 discs on the center peg to the right peg
- can we generalize this?
- three pegs
- current
- destination
- other
- want to move n discs from current to destination
- move top n-1 discs from current to other
- move bottom disc to destination
- move n-1 discs from other discs to destination
- what is the base case?
- there are no discs to be moved, don't do anything
hanoi(n, current, destination, other){
if( n == 0 ){
// don't do anything, just stop the recursion
}else{
hanoi(n-1, current, other, destination)
move the bottom disc to destination
hanoi(n-1, other, destination, current)
}
}

• midterm review
- close book, closed notes, etc.
- we will give you copies of:
- Objectdraw quick reference
- GUI cheat sheet
- types of questions
- 1-2 code this up problems
- 1-2 what does this code do problems
- a few short answer questions
- will cover all the material before recursion (will not have any questions on recursion)
- topics (roughly)
- basic programming
- variables
- instance
- local
- constants
- methods
- classes
- parameters
- commenting
- objectdraw components
- booleans
- if-else
- ints and doubles
- while loops
- active objects
- defining and manipulating multiple classes
- GUIs
- randomness
- colors
- images
- audio
- interfaces

• look at Lab 7