Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007ACS-1805 Ron McFadyen1 Chapter 8 Recursion.

Similar presentations


Presentation on theme: "Fall 2007ACS-1805 Ron McFadyen1 Chapter 8 Recursion."— Presentation transcript:

1 Fall 2007ACS-1805 Ron McFadyen1 Chapter 8 Recursion

2 Fall 2007ACS-1805 Ron McFadyen2 Recursion Recursion is a special form of repetition Recursion is a technique that can be used when the problem to be solved can be stated “in terms of itself” There is no “recursion” statement. Recursion is done in Alice by writing a method that calls itself, or by writing a function that calls itself. Warning: these can be difficult to write. While it is true that recursion can be used to obtain very simple and elegant solutions, experience shows us that learning to use recursion is actually quite difficult.

3 Fall 2007ACS-1805 Ron McFadyen3 Recursion Consider the problem of the alienOnWheels moving towards the rock. We’ve seen several approaches Now consider the following algorithm move1Metre: alienOnWheels moves forward 1 metre If the distance between the alienOnWheels and the rock is greater than 1 metre then call move1Metre move alien to rock: alienOnWheels turns to face rock If the distance between the alienOnWheels and the rock is greater than 1 metre then call move1Metre Because it calls itself move1Metre is a recursive method

4 Fall 2007ACS-1805 Ron McFadyen4 Recursion In class … use Alice to move the robot recursively

5 Fall 2007ACS-1805 Ron McFadyen5 Recursion move1Metre has the general structure of methodRecurse Do something If condition then call methodRecurse

6 Fall 2007ACS-1805 Ron McFadyen6 Recursion Another simple example: Suppose you need to calculate n! Note:1!=1 2!=2*1=2 3!=3*2*1=6 Or more generally n!=n*(n-1)! That is n! is defined in terms of factorial

7 Fall 2007ACS-1805 Ron McFadyen7 Recursion Consider: factorial with parameter n: If n>1 then return n*factorial(n-1) Else return 1

8 Fall 2007ACS-1805 Ron McFadyen8 Recursion In class … use Alice to calculate n!

9 Fall 2007ACS-1805 Ron McFadyen9 Recursion In class discussion of text examples: Horse race Towers of Hanoi

10 Fall 2007ACS-1805 Ron McFadyen10 Tips/Techniques Camera & animation controls Point of view Runtime speed

11 Fall 2007ACS-1805 Ron McFadyen11 Tips/Techniques Camera & animation controls Point of view Runtime speed

12 Fall 2007ACS-1805 Ron McFadyen12 Tips/Techniques Camera & animation controls Point of view: an objects point of view is the combination of the objects position and orientation The set point of view method can be used to move the camera to a pre- determined point of view Each object has a pointOfView property

13 Fall 2007ACS-1805 Ron McFadyen13 Tips/Techniques Camera & animation controls When you are in the Add Objects mode you can use controls To creating dummy objects and To move the camera to dummy objects

14 Fall 2007ACS-1805 Ron McFadyen14 Tips/Techniques Runtime speed


Download ppt "Fall 2007ACS-1805 Ron McFadyen1 Chapter 8 Recursion."

Similar presentations


Ads by Google