Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,

Similar presentations


Presentation on theme: "Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,"— Presentation transcript:

1 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Recursion

2 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion 15-2 Recursive functions The programs discussed so far have been structured as functions that invoke one another in a disciplined manner. For some problems it is useful to have functions call themselves

3 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion 15-3 A recursive function is a function that calls itself directly or indirectly through another function.

4 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-4 1.The function actually knows how to solve only the simplest case(s), called the base case(s). 2.If the function is called with a base case, the function simply returns a result.

5 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-5 3.If the function is called with a more complex problem, the function divides the problem into two conceptual pieces: –the piece that the function knows how to do –a piece that the function does not know how to do

6 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-6 4.To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or slightly smaller version of the original problem. 5.Because this new problem looks like the original problem, the function calls a fresh copy of itself to work on the smaller problem – this is referred to as a recursive call and also the recursive step.

7 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-7 6.The recursive step executes while the original call to the function has not finished executing. 7.The recursive step can result in many more recursive calls.

8 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-8 8.In order for recursion to eventually terminate, each time the function calls itself with a slightly simpler version of the original problem, the sequence of smaller problems must converge on the base case.

9 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conceptual view of recursion 15-9 9.When the sequence reaches the base case, the function returns a result to the previous copy of the function. This previous copy then returns a result to the copy that called it, etc. until the original function returns a final result to the caller.

10 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of Recursive Problems and Solutions 15-10 Problems that can be solved by recursion have the following characteristics: –One or more stopping cases (base cases) have a simple, non-recursive solution. –The other cases of the problem can be reduced to problems that are closer to the base cases. –Eventually the problem can be reduced to only base cases, which are relatively easy to solve.

11 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley To solve a recursive problem: 15-11 1.Try to express the problem as a simpler version of itself. 2.Determine the base cases. 3.Determine the recursive steps.

12 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley To solve a recursive problem: 15-12 Our recursive algorithms will generally consist of an if statement of the form: if (base case is reached) Solve it else Split the problem into simpler cases using recursion

13 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 15-13 A simple, but not useful, application The "traditional" example of recursion involves computing mathematical recurrences, commonly the factorial function. The factorial (and other recurrences) are trivial to calculate in a single for loop in a way that is much more CPU and memory efficient! Nevertheless, the simple example provides a useful first step in understanding the approach. Computing n! (n factorial) n! = n*(n-1)*(n-2)*(n-3)*...*1 0! = 1 1! = 1... 5! = 5*4*3*2*1 = 120

14 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 15-14 By observing the following relationship, we can develop a recursive definition: n! = n*(n-1)! #include int factorial (int val) { if (val == 0)// base case return 1; else return (val * factorial (val - 1)); //Every recursive function must include a mechanism for halting the recursion!! }

15 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example 15-15 int main() { int factorial; factorial = fact(5); printf("%d \n", factorial); }


Download ppt "Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,"

Similar presentations


Ads by Google