Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion.

Similar presentations


Presentation on theme: "Recursion."— Presentation transcript:

1 Recursion

2 Circular Definition vs Recursive Definition
oak A tree that grows from an acorn, which is a nut produced by an oak tree Recursion See recursion

3 Non-circular definition
Recursion If you don’t get it, see recursion Has a terminating case (i.e., “if you get it.”)

4 Recursive Definition Recursive Definition
Uses the term being defined as part of the definition Is not a circular definition Factorial of n (n!) n! = 1, when n = // base case n! = n (n – 1)!, when n > 0

5 Recursive Function Invokes itself within the function
Is an example of divide-and-conquor technique Recursive factorial function int fact(int n){ if (n == 0) return 1; else return n * fact(n – 1); } Invocation cout << fact(4);

6 Recursive Factorial Function
fact(4) return 4 * fact(3) Recursive Factorial Function fact(3) return 3 * fact(2) fact(2) return 2 * fact(1) fact(1) return 1 * fact(0) fact(0) return 1 fact(1) return 1 * 1 fact(2) return 2 * 1 fact(3) return 3 * 2 fact(4) return 4 * 3

7 Sum: … + n2 Write a recursive function which returns the sum of squares of n integers. Invocation cin >> n; cout << sumSquares(n);

8 Sum: … + n2 n = 1: sum = 1 n = 2: sum = (1) + 22 n = 3 sum = (1 + 22)+ 32 n = 4: sum = ( ) + 42 n = 5: sum = ( )+ 52 … n = n: sum = ( …(n -1)2)+ n2

9 Sum: … + n2 long sumSquares(int n){ if (n == ) return 0; else return sumSquares(n – 1) + n * n; }

10 Recursive Sum of array elements
Given: int list[] = {2, 4, 6, 8, 10}; int count = 5; cout << sumArray(list, count);

11 Recursive Sum of array elements
n = 1: sum = list[0] n = 2: sum = (list[0]) + list[1] n = 3: sum = (list[0] + list[1]) list[2] n = 4: sum = (list[0] + list[1] + list[2]) list[3] n = n: sum = (list[0] + list[1]… list[n – 2]) list[n – 1]

12 Recursive Sum of array elements
long sumArray(int list[], int n){ if (n == 1) return list[0]; else return sumArray(list, n – 2) list[n – 1]; }

13 Fibonacci Numbers

14 Fibonacci Numbers fib(n) = 1, when n = 0 fib(n) = 1, when n = 1 fib(n) = fib(n -1) + fib(n – 2), when n > 1 Fibonacci Function int fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2); }

15 Golden Ratio Greek and Renaissance Architect considered the golden ratio for a rectangle. 1

16 Golden Ratio Euclid: "A straight line is said to have been cut in extreme and mean ratio when, as the whole line is to the greater segment, so is the greater to the less.” (Elements) a b a + b a b a + 1 a 1 φ φ = = = = a a

17 Golden Ration & Fibonacci Nos.
Fib Ratio Decimal / / / / / / / / /  φ

18 Your Turn Write and test a recursive function which imports positive integer n and returns the sum of consecutive integers from 1 to n, inclusive. Write and test a recursive function which returns xn. Write a C++ program which generates the first n Fibonacci numbers, where n is input from the console.

19 Your Turn Write and test a recursive function which imports an int array, start index, & last index and prints the list contents from position start to last. Write and test a recursive function which imports an int array and its count and prints the list contents in reverse order.


Download ppt "Recursion."

Similar presentations


Ads by Google