Chapter 10A

advertisement
Visual C++ Programming:
Concepts and Projects
Chapter 10A: Recursion
(Concepts)
Objectives
In this chapter, you will:
• Discover how to recognize a recursive problem
• Code a recursive method
• Use recursion to draw fractal images
• Analyze recursion
• Create and code menus
Programming with Visual C++
2
Factorial Numbers
• Factorial numbers
– Example: 5! = 5 x 4 x 3 x 2 x 1
– Can be easily computed using a loop
Programming with Visual C++
3
Factorial Numbers (continued)
• Factorial numbers
– 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
Programming with Visual C++
4
Recursion
• Recursion
• Refers to re-occurring events of a similar nature
• Mathematical example: factorial numbers
• Everyday example: a chain of phone calls
• A problem (finding an apartment) must be solved by a
chain of similar events (phone calls)
• The events repeat themselves until an answer is
obtained
• The answer follows the same path back
Programming with Visual C++
5
Recursion (continued)
Programming with Visual C++
6
Recursion (continued)
Programming with Visual C++
7
Recursion
• Recursive solutions
• Have a recursive case
• A call to continue the same operation at another level
• Have a base case
• A stopping point
• Use backtracking
• To go back to the previous operation at a higher level
Programming with Visual C++
8
Recursion (continued)
• Algorithm for computing factorial numbers
Programming with Visual C++
9
Recursion (continued)
Programming with Visual C++
10
Recursion (continued)
Programming with Visual C++
11
Recursion (continued)
• 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)
Programming with Visual C++
12
Recursion (continued)
– Recursive definitions can be translated easily into
recursive methods
• Example: Factorial() method
Programming with Visual C++
13
Recursion (continued)
Programming with Visual C++
14
Recursion (continued)
Programming with Visual C++
15
Recursion vs. Iteration
• 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
Programming with Visual C++
16
Recursion vs. Iteration (continued)
Programming with Visual C++
17
Creating Fractal Images
• Draw a line
– Then draw three lines half the size (at 90 degree
angles to one another) from the end of the line
you just drew
• Apply this strategy recursively to every line
you draw
• Do this until you have reached a
predetermined stopping point (the base case)
• Example: recursive crosses
Programming with Visual C++
18
Creating Fractal Images (continued)
Programming with Visual C++
19
Creating Fractal Images (continued)
Programming with Visual C++
20
Creating Fractal Images (continued)
Programming with Visual C++
21
Creating Fractal Images (continued)
Programming with Visual C++
22
Creating Fractal Images (continued)
Programming with Visual C++
23
Creating Fractal Images (continued)
Programming with Visual C++
24
Computer-Generated Fractal Images
• Drawing lines with Graphics class method
DrawLine()
• Use DrawLine() to create a line
– Parameters
• Pen
• x1, y1 (coordinates of the start of the line)
• x2, y2 (coordinates of the end of the line)
Programming with Visual C++
25
Computer-Generated Fractal Images
(continued)
• The starting coordinates of the line are (x, y)
• The ending coordinates are (newX, newY)
• In a recursive solution, there are many levels of
line-drawing tasks
• The coordinates of the end of the previous line
become the coordinates of the starting point of
the next one
Programming with Visual C++
26
Recursive DrawBranch()
Method
Programming with Visual C++
27
Recursive DrawBranch() Method
(continued)
• DrawBranch() is recursive
• 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
• The base case is the last level of recursion
Programming with Visual C++
28
Recursive DrawBranch() Method
(continued)
Programming with Visual C++
29
Recursive DrawBranch() Method
(continued)
Programming with Visual C++
30
Recursive DrawBranch() Method
(continued)
• Algorithm for method DrawBranch()
Programming with Visual C++
31
Recursive DrawBranch() Method
(continued)
• A single branch with five levels of recursion
looks like a pattern of crosses
• You will develop a program in the Tutorial to
implement this
Programming with Visual C++
32
Recursive DrawBranch() Method
(continued)
Programming with Visual C++
33
Summary
• Recursion involves a method calling itself
• Recursive methods must have:
– A base case
– A recursive case
• Recursive backtracking
– After the base case is reached, the method
returns to the previous one
– Backtracking continues until the program returns
to the original method call
Programming with Visual C++
34
Summary (continued)
• Some problems have easy and elegant
recursive solutions
– Tasks with mathematically inductive definitions
are naturally recursive
– Example: factorial numbers
– Example: fractal images
Programming with Visual C++
35
Download