Download presentation

Presentation is loading. Please wait.

1
Recursion

2
**A function that is defined recursively consists of two parts:**

1. An anchor or base case, in which the value of the function is specified for one or more values of the parameter(s). 2. An inductive, or recursive step, in which the function's value for the current value of the parameter(s) is defined in terms of previously defined function values and/or parameter values.

3
**Example: Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …**

//f(1) = 1; f(2) = 1; //f(n) = f(n-1) + f(n-2) , n>2. int Fib(int n) { if (n<=2) return 1; return Fib(n-1) + Fib(n-2); }

4
**Example: Binary Search**

Recall the binary search on an array of sorted values from COMP165.

5
**void BinSearch (ElementType a[], ElementType item, bool& found, int& position)**

{ int first=0; int last = length-1; int middle; found = False; while(last>=first && !found) middle = (first + last)/2; if (item < a[middle]) last = middle -1; else if (item > a[middle]) first = middle+1; else found = True; } if (found) position = middle;

6
**Recursive Binary Search**

void BinarySearch (ElementType a[], int first, int last, ElementType item, bool & found, int & loc) { if (first > last) found = false; else loc = (first + last) /2; if (item < a[loc]) BinarySearch (a, first, loc-1,item,found, loc); else if (item > a[loc]) BinarySearch (a, loc+1, last, item, found loc); found = true; }

7
**Steps in writing recursive functions**

1. Identify the factor(s) that governs the size of the problem. 2. Identify the solution(s) for the smallest version(s) of the problem. This defines the base case. 3. Identify a general solution by defining the problem in terms of a smaller version of the problem. This defines the recursive step.

8
Recursion Problem Write a recursive function to print values stored in an array of integers. Use the following function header where a in the array and n is the number of values stored in the array. void print(int a[], int n)

9
**Solution void print(int A[], int N) { if (N==0) return; print(A, N-1);**

cout << A[N-1] <<endl; }

10
**Program testing the solution**

#include <iostream.h> void print(int[], int); void main() { int A[] = {3, 4, 5, 6, 7}; int N = 5; print(A, N); } void print(int A[], int N) if (N==0) return; print(A, N-1); cout << A[N-1] <<endl;

11
**Output from execution nadine.ncat.edu> cxx recmod.cc**

nadine.ncat.edu> a.out 3 4 5 6 7

12
**void print(int A[], int N) { static int n=0; n++; **

Modify the recursive print function to count the number of calls made to the function and print the value of N for each call. void print(int A[], int N) { static int n=0; n++; cout<<"call: "<<n<<" N= "<<N<<endl; if (N==0) { cout<<“no. of calls: "<< n <<endl; return; } print(A, N-1); cout << A[N-1] <<endl;

13
**Output from execution nadine.ncat.edu> cxx recursion.cc**

nadine.ncat.edu> a.out call: 1 N= 5 call: 2 N= 4 call: 3 N= 3 call: 4 N= 2 call: 5 N= 1 call: 6 N= 0 no of calls: 6 3 4 5 6 7

14
**Modify the recursive print function to print the values out in reverse order**

15
**Solution #include <iostream.h> void print(int[], int);**

void main() { int A[] = {3, 4, 5, 6, 7}; int N = 5; print(A, N); } void print(int A[], int N) if (N==0) return; cout << A[N-1] <<endl; print(A, N-1);

16
Execution nadine.ncat.edu> a.out 7 6 5 4 3

17
Exercise Write a recursive function to sum the values stored in an array of integers. Use the following function header where a in the array and n is the number of values stored in the array. int sum(int a[], int n)

Similar presentations

OK

1 Chapter 8 Recursion. 2 Objectives To know what is a recursive function and the benefits of using recursive functions (§8.1). To determine the base.

1 Chapter 8 Recursion. 2 Objectives To know what is a recursive function and the benefits of using recursive functions (§8.1). To determine the base.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on id ego superego images Ppt on job evaluation form Ppt on fire extinguisher types Ppt online gambling addiction Ppt on object oriented programming with c++ book Ppt on boilers operations manager Ppt on second law of thermodynamics explained Ppt on static circuit breaker Ppt on low level language programming Ppt on water pollution for class 8