visual c++ programming: concepts and projects

35
Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)

Upload: melva

Post on 14-Jan-2016

24 views

Category:

Documents


2 download

DESCRIPTION

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. Factorial Numbers. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual C++ Programming: Concepts and Projects

Visual C++ Programming: Concepts and Projects

Chapter 10A: Recursion (Concepts)

Page 2: Visual C++ Programming: Concepts and Projects

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

Page 3: Visual C++ Programming: Concepts and Projects

Factorial Numbers

Programming with Visual C++ 3

• Factorial numbers– Example: 5! = 5 x 4 x 3 x 2 x 1– Can be easily computed using a loop

Page 4: Visual C++ Programming: Concepts and Projects

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

Page 5: Visual C++ Programming: Concepts and Projects

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

Page 6: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 6

Page 7: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 7

Page 8: Visual C++ Programming: Concepts and Projects

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

Page 9: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 9

• Algorithm for computing factorial numbers

Page 10: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 10

Page 11: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 11

Page 12: Visual C++ Programming: Concepts and Projects

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

Page 13: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 13

– Recursive definitions can be translated easily into recursive methods• Example: Factorial() method

Page 14: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 14

Page 15: Visual C++ Programming: Concepts and Projects

Recursion (continued)

Programming with Visual C++ 15

Page 16: Visual C++ Programming: Concepts and Projects

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

Page 17: Visual C++ Programming: Concepts and Projects

Recursion vs. Iteration (continued)

Programming with Visual C++ 17

Page 18: Visual C++ Programming: Concepts and Projects

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

Page 19: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 19

Page 20: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 20

Page 21: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 21

Page 22: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 22

Page 23: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 23

Page 24: Visual C++ Programming: Concepts and Projects

Creating Fractal Images (continued)

Programming with Visual C++ 24

Page 25: Visual C++ Programming: Concepts and Projects

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

Page 26: Visual C++ Programming: Concepts and Projects

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

Page 27: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method

Programming with Visual C++ 27

Page 28: Visual C++ Programming: Concepts and Projects

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

Page 29: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method (continued)

Programming with Visual C++ 29

Page 30: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method (continued)

Programming with Visual C++ 30

Page 31: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method (continued)

Programming with Visual C++ 31

• Algorithm for method DrawBranch()

Page 32: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method (continued)

Programming with Visual C++ 32

• 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

Page 33: Visual C++ Programming: Concepts and Projects

Recursive DrawBranch() Method (continued)

Programming with Visual C++ 33

Page 34: Visual C++ Programming: Concepts and Projects

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

Page 35: Visual C++ Programming: Concepts and Projects

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