Presentation is loading. Please wait.

Presentation is loading. Please wait.

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)

Similar presentations


Presentation on theme: "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)"— Presentation transcript:

1 VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)

2 Objectives Visual C++ Programming 2  Discover how to recognize a recursive problem  Code a recursive method  Use recursion to draw fractal images  Analyze recursion  Create and code menus

3 Factorial Numbers Visual C++ Programming 3  Factorial numbers  Example: 5! Is 5 x 4 x 3 x 2 x 1  Can be easily computed using a loop  Can also be computed as separate steps from the bottom up  Solving the problem from the bottom up will lead us to a new solution strategy called recursion

4 Visual C++ Programming 4

5 5

6 Recursion Visual C++ Programming 6  Recursion  Refers to re-occurring events of a similar nature  Example: a chain of phone calls  Mathematical example: Factorial numbers  Recursive solutions  Have a base case A stopping point  Have a recursive case A call to continue the same operation at another level  Use backtracking To go back to the previous operation at a higher level

7 Visual C++ Programming 7

8 8

9 9

10 10

11 Visual C++ Programming 11

12 Recursion (continued) Visual C++ Programming 12  Recursion and factorial numbers  Recursive definition of a factorial number (n!)  For all positive integers n, n! is defined as follows If n = 0 or 1 then n! = 1 (this is the base case) If n > 1 then n! = n x (n-1)! (this is the recursive case)  Recursive definitions can be translated easily into recursive methods Example: Factorial() method

13 Visual C++ Programming 13

14 Visual C++ Programming 14

15 Visual C++ Programming 15

16 Recursion vs. Iteration Visual C++ Programming 16  Iteration is usually faster  Does not require background overhead to keep track of each method call  Recursion is more elegant  More closely matches the mathematical definition of the operation it is performing  Recursion can more easily solve some problems  Example: generating fractal images

17 Visual C++ Programming 17

18 Creating Fractal Images Visual C++ Programming 18  Draw a line  Then draw three lines half the size at 90 degree angles off the end of the line you just drew  Apply this strategy recursively to every line you draw  Do this until you have reached a pre-determined stopping point (the base case)  Example: recursive crosses

19 Visual C++ Programming 19

20 Visual C++ Programming 20

21 Visual C++ Programming 21

22 Visual C++ Programming 22

23 Visual C++ Programming 23

24 Visual C++ Programming 24

25 Computer-Generated Fractal Images Visual C++ Programming 25  Drawing lines with DrawLine()  Use the DrawLine() method to create a line  Parameters Pen x1, y1 (coordinates of the start of the line) x2,y2 (coordinates of the end of the line)  Recursive call replaces the starting coordinates of the next line with the ending coordinates of the previous one

26 Visual C++ Programming 26

27 Recursive DrawLine() Method Visual C++ Programming 27  Each level of recursion draws three new lines from the end of the previous one  The DrawBranch() method is called three times to do this  DrawBranch() is recursive  The base case is the level of recursion

28 Visual C++ Programming 28

29 Visual C++ Programming 29

30 Visual C++ Programming 30

31 Visual C++ Programming 31

32 Visual C++ Programming 32

33 Summary Visual C++ Programming 33  Recursion involves a method calling itself  Recursive methods must have  A base case  A recursive case  Problems with mathematically inductive definitions are naturally recursive  Example: factorial numbers  Example: fractal images


Download ppt "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)"

Similar presentations


Ads by Google