Recursion COMP 104 Recursion Slide 2 Recursion Example

  • Slides: 14
Download presentation
Recursion

Recursion

COMP 104 Recursion / Slide 2 Recursion: Example 0 * What does the following

COMP 104 Recursion / Slide 2 Recursion: Example 0 * What does the following program do? #include <iostream> using namespace std; int fac(int n){ // Assume n >= 0 int product; if(n <= 1) return 1; product = n * fac(n-1); return product; } void main(){ // driver function int number; do{ cout << "Enter integer (negative to stop): "; cin >> number; if(number >= 0) cout << fac(number) << endl; }while(number >= 0); }

COMP 104 Recursion / Slide 3 Recursion: Example 0 * Assume the number typed

COMP 104 Recursion / Slide 3 Recursion: Example 0 * Assume the number typed is 3. fac(3) : 3 <= 1 ? product 3 = 3 * fac(2) : 2 <= 1 ? product 2 = 2 * fac(1) : 1 <= 1 ? return 1 product 2 = 2 * 1 = 2 return product 2 product 3 = 3 * 2 = 6 return product 3 fac(3) has the value 6 No. Yes.

COMP 104 Recursion / Slide 4 Recursion * Recursion is one way to decompose

COMP 104 Recursion / Slide 4 Recursion * Recursion is one way to decompose a task into smaller subtasks. * At least one of the subtasks is a smaller example of the same task. * The smallest example of the same task has a non-recursive solution. Example: The factorial function n! = n * (n-1) * (n-2) *. . . * 1 or n! = n * (n-1)! and 1! = 1

COMP 104 Recursion / Slide 5 Recursion *A recursive solution may be simpler to

COMP 104 Recursion / Slide 5 Recursion *A recursive solution may be simpler to write (once you get used to the idea) than a non-recursive solution. *But a recursive solution may not be as efficient as a non-recursive solution of the same problem.

COMP 104 Recursion / Slide 6 Iterative Factorial // Non-recursive factorial function // Compute

COMP 104 Recursion / Slide 6 Iterative Factorial // Non-recursive factorial function // Compute the factorial using a loop int fac(int n){ // Assume n >= 0 int k, product; if(n <=1) return 1; product = 1; for(k=1; k<=n; k++) product*= k; return product; }

COMP 104 Recursion / Slide 7 Other Recursive Applications *Fibonacci numbers: 0, 1, 1,

COMP 104 Recursion / Slide 7 Other Recursive Applications *Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . where each number is the sum of the preceding two. * Recursive definition: F(0) = 0 n F(1) = 1 n F(n) = F(n-1) + F(n-2) n

COMP 104 Recursion / Slide 8 Other Recursive Applications *Binary search: n Compare search

COMP 104 Recursion / Slide 8 Other Recursive Applications *Binary search: n Compare search element with middle element of the array: n If not equal, then apply binary search to half of the array (if not empty) where the search element would be.

COMP 104 Recursion / Slide 9 Recursion General Form * How to write recursively?

COMP 104 Recursion / Slide 9 Recursion General Form * How to write recursively? int rec(1 -2 parameters){ if(stopping condition) return stopping value; // second stopping condition if needed return value/rec(revised parameters) +-*/ rec(revised parameters); }

COMP 104 Recursion / Slide 10 Recursion: Example 1 *How to write exp(int x,

COMP 104 Recursion / Slide 10 Recursion: Example 1 *How to write exp(int x, int y) recursively? int exp(int x, int y){ if(y==0) return 1; return x * exp(x, y-1); }

COMP 104 Recursion / Slide 11 Recursion: Example 2 * Write a recursive function

COMP 104 Recursion / Slide 11 Recursion: Example 2 * Write a recursive function that takes a double array and its size as input and returns the sum of the array: double asum(int a[], int size){ if(size==0) return 0; return asum(a, size-1)+a[size-1]; }

COMP 104 Recursion / Slide 12 Recursion: Example 3 * Write a recursive function

COMP 104 Recursion / Slide 12 Recursion: Example 3 * Write a recursive function that takes a double array and its size as input and returns the product of the array: double aprod(int a[], int size){ if(size==0) return 1; return aprod(a, size-1)*a[size-1]; }

COMP 104 Recursion / Slide 13 Recursion: Example 4 Write a recursive function that

COMP 104 Recursion / Slide 13 Recursion: Example 4 Write a recursive function that counts the number of zero digits in a non-negative integer * zeros(10200) returns 3 * int zeros(int n){ if(n==0) return 1; if(n < 10) return 0; if(n%10 == 0) return 1 + zeros(n/10); else return zeros(n/10); }

COMP 104 Recursion / Slide 14 Recursion: Example 5 *Write a recursive function to

COMP 104 Recursion / Slide 14 Recursion: Example 5 *Write a recursive function to determine how many factors m are part of n. For example, if n=48 and m=4, then the result is 2 (48=4*4*3). int factors(int n, int m){ if(n%m != 0) return 0; return 1 + factors(n/m, m); }