SUNY Geneseo Department of Computer Science
{Date}
CSci 141, Fall 2003
Prof. Doug Baldwin
Hand out Lab 4
Using robot class outside of labs - problems with packages
Play-act last lecture's power-of-2 algorithm
int powerOf2( int n )
if n = 0
return 1
if n >= 1
return 2 * powerOf2( n-1 )
if n <= -1
return (1 / 2) * powerOf2( n+1 )
Recursion works like any other message
Using people to keep track of invocations of methods...
Section 6.5
Review definition of algorithm
How recursive algorithm works, like we just did
Inversion - last started is first done
Three parts of recursive algorithm
Multiple recursive references
a()
if .....
"if" arm 1
... a() ...
... a() ...
else if ....
"if" arm 2
... a() ...
else
"else" arm
Can't have recursive call before test
Hiding recursion / Hiding messy message interfaces
root( n )
return squareRoot( n, 0, n )
Design a recursive algorithm that makes a robot draw an n-tile line and then go back to its original tile.
Need to retrace steps
Control order of steps by when/where recursive call is
Take as many steps back as you took forward
Algorithm outline
Alternative
Take advantage of inversion
if n = 0
turn around
else
paint
move
recurse( n - 1 )
move