SUNY Geneseo Department of Computer Science


Intermediate Recursion

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 4

Questions?

Sections 6.4 and 6.5

Ways to make sure recursion terminates

Three parts

Inversion

Don't do recursion before test

Designing Intermediate Recursion

Recursive algorithm to print n 1's followed by n 2's

    print12( n )
        if n > 0
            print 1
            print12( n-1 )
            print 2
        else

Print n 1's followed by 2n 2's

    print1Double2( n )
        if n > 0
            print 1
            print1Double2( n-1 )
            print 2
            print 2
        else
            do nothing

Use the robot to draw a "bracket" n tiles on a side, n >= 1

[Upside-Down "L" with n Vertical Tiles and n Horizontal]

    // In some subclass of Robot...
    bracket( n )
        if n > 1
            this.paint(Yellow)
            this.move()
            this.bracket( n-1 )
            this.move()
            this.paint( Yellow )
        else
            this.paint( Yellow )
            this.turnLeft()

Next

Some final discussion of the "bracket" algorithm

How do you know a recursive algorithm is really correct?


Next Lecture