Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Development and Design Using C++, Third Edition

Similar presentations


Presentation on theme: "Program Development and Design Using C++, Third Edition"— Presentation transcript:

1 Program Development and Design Using C++, Third Edition
Chapter 8 Recursion Program Development and Design Using C++, Third Edition

2 Program Development and Design Using C++, Third Edition
Objectives You should be able to: Mentally execute recursive functions Write and understand programs which contain recursive functions Convert a while loop into a call to a recursive function Program Development and Design Using C++, Third Edition

3 Recursion (Chapter 6 in textbook-section 6.6)
A function that is defined in terms of itself is called recursive e.g. f(x)=2f(x-1) + x2 int fun(int x) { if (x==0) return 0; else return 2*fun(x-1)+x*x; } Program Development and Design Using C++, Third Edition

4 Program Development and Design Using C++, Third Edition
Recursion Recursive functions Functions that call themselves Can only solve a base case If not base case Break problem into smaller problem(s) Launch new copy of function to work on the smaller problem (recursive call/recursive step) Slowly converges towards base case Function makes call to itself inside the return statement Eventually base case gets solved Answer works way back up, solves entire problem Program Development and Design Using C++, Third Edition

5 Recursion: How the Computation Is Performed
C++ allocates new memory locations for function parameters and local variables as each function is called Allocation made dynamically in memory stack Memory stack: Area of memory used for rapidly storing and retrieving data Last-in/first-out Program Development and Design Using C++, Third Edition

6 Two fundamental rules of recursion:
Base cases. You must always have some base cases which can be solved without recursion Making Progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case Program Development and Design Using C++, Third Edition

7 An incorrect recursive function
int sum(int n) { return n+sum(n-1); } This function does not have a base case Program Development and Design Using C++, Third Edition

8 A correct recursive function
int sum(int n) { if (n==0) return 0; else return n+sum(n-1); } Program Development and Design Using C++, Third Edition

9 A non-terminating recursive program
int Bad(int n) { if (n==0) return 0; else return Bad(n/3+1)+n-1; } Program Development and Design Using C++, Third Edition

10 Program Development and Design Using C++, Third Edition
Factorial example factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… Base case (1! = 0! = 1) Program Development and Design Using C++, Third Edition

11 Recursive factorial function
int factorial(int n) { if ( n==0 ) // base case return 1; else // recursive step return n*factorial(n - 1); } Program Development and Design Using C++, Third Edition

12 Testing the factorial function
void main() { int i; // Loop 5 times. During each iteration, calculate // factorial( i ) and display result. for ( i = 0; i <= 10; i++ ) cout << i << "! = "<< factorial( i ) << endl; } Output: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 Program Development and Design Using C++, Third Edition

13 Drawing a triangle recursively
void Triangle(int n) { //draws a right-angled triangle if (n!=0) line(n,'*'); //function Line from previous chapter cout<<endl; Triangle(n-1); } Program Development and Design Using C++, Third Edition

14 Testing the function Triangle
#include <iostream.h> void line(int x, char ch) ; //function prototypes void Triangle(int n); void line(int x, char ch) //function definitions { int i; for(i=1; i<=x; i++) cout << ch ; } void Triangle(int n) if (n!=0) line(n, '*'); cout<<endl; Triangle(n-1); Program Development and Design Using C++, Third Edition

15 Testing the function Triangle(ctd)
void main() { int x; cout<<"Please enter the height of the triangle:"; cin>>x; while (x<=0) cout<<"Please enter a positive height of the triangle:"; } Triangle(x); Program Development and Design Using C++, Third Edition

16 The connection between recursion and looping
We can always re-write a while loop as a recursive function: while (<expression>) <statement> //This is any sequence of statements in curly brackets This while loop can be replaced by a call to the recursive function f: void f() { if (<expression>) <statement>; f(); } Program Development and Design Using C++, Third Edition

17 A non-recursive Triangle function
void Triangle(int n) { while (n!=0) line(n,'*'); cout<<endl; n--; } Program Development and Design Using C++, Third Edition

18 Program Development and Design Using C++, Third Edition
Example Using Recursion: Fibonacci Sequence (exercises 6.6, pg. 355, ex. 1) Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8... Each number sum of two previous ones (except for the first two numbers). Write a recursive function that returns the nth number in the Fibonacci sequence, when n is passes to the function as argument. For example: when n=8, the function should return the eighth number in the sequence, which is 13. Program Development and Design Using C++, Third Edition

19 The recursive Fibonacci function
long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n – 2 ); } Program Development and Design Using C++, Third Edition

20 The recursive Fibonacci function
return 1 return 0 return + Program Development and Design Using C++, Third Edition

21 What does this function do?
void Rev() { int i; cin>>i; if (i!=-1) Rev(); cout<<i; } Program Development and Design Using C++, Third Edition

22 Program Development and Design Using C++, Third Edition
Answer: Reads a sequence of integers, terminated by -1 and outputs them in reverse order (excluding the -1). Example run: User enters: 2 5 7 8 -1 Output: 8 2 Program Development and Design Using C++, Third Edition

23 Program Development and Design Using C++, Third Edition
Exercises: The recursive solution to the problem of finding the Fibonacci number we gave earlier, is very innefficient (why?). Write a non-recursive Fibonacci function. Program Development and Design Using C++, Third Edition

24 Exercises (continued):
The greatest common divisor (gcd) of two integers p and q is the largest integer that divides both (e.g. gcd of 18 and 12 is 6). An algorithm to compute the greatest common divisor of two integers p and q is the following: Let r be the remainder of p divided by q. If r is 0, then q is the greatest common divisor. Otherwise, set p equal to q, then q equal to r, and repeat the process. Write a recursive function that implements the above algorithm. Program Development and Design Using C++, Third Edition

25 Exercises (continued):
Write a recursive function called locate that takes a string str, a character ch and an integer i and returns the index position of the first occurrence of character ch in string str. (The function assumes that there is an occurrence of the character at or after the index i in the string str). Example: cout<<locate("hello", 'l',0); should print 2. Program Development and Design Using C++, Third Edition


Download ppt "Program Development and Design Using C++, Third Edition"

Similar presentations


Ads by Google