Recursion Functions reminder returntype namearg Type 1 arg
- Slides: 66
Recursion
Functions – reminder return-type name(arg. Type 1 arg 1, arg. Type 2 arg 2, …) { function body; return value; } A function call other functions. Return causes the execution of the function to terminate and returns a value to the calling function. The type of the value returned must be the same as the return-type defined for the function.
A recursive definition C functions can also call themselves! However, usually not with the same parameters (why? ) Some functions can be defined using smaller occurrences of themselves. Such a definition is called a “recursive definition” of a function.
Recursive calling Example: void func(int n){ putchar(`*`); func(n); } Infinite series of * What is the problem ? Look for other problem …
Factorial By definition : n! = 1*2*3*… *(n-1)*n (n-1)! *n Thus, we can also define factorial the following way: 0! = 1 n! = n*(n-1)! for n>0
Example - factorial int factorial(int n){ int fact = 1; while (n >= 1) { fact *=n; n--; } return fact; } int fact. Rec(int n){ if (n==0 || n==1) return 1; return n*fact. Rec(n-1); }
Conclusions for Recursive calling Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied.
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) n 4 Returns…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) n 4 Returns… 4*…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) Fact. Rec(3) n 4 n 3 Returns…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) Fact. Rec(3) n 4 n 3 Returns…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n 4 n 3 Returns… 3*… return n*fact. Rec(n-1); }
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 n Returns… 2 Returns… return n*fact. Rec(n-1); }
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 n Returns… 2 Returns… return n*fact. Rec(n-1); }
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 n Returns… 2 Returns… return n*fact. Rec(n-1); } 2*…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 Fact. Rec(1) n Returns… 2 n Returns… 1 Returns…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 Fact. Rec(1) n Returns… 2 n Returns… 1 Returns…
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 Fact. Rec(1) n Returns… 2 n Returns… 1
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n n 4 Fact. Rec(2) 3 n Returns… 2 Returns… return n*fact. Rec(n-1); } 2*1
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; Fact. Rec(4) Fact. Rec(3) n 4 n 3 Returns… 3*2 return n*fact. Rec(n-1); }
Recursive factorial – step by step int fact. Rec(int n) { if (n==0 || n==1) return 1; return n*fact. Rec(n-1); } Fact. Rec(4) n 4 Returns… 4*6
1 דוגמא - הדפסת מספרים #include <stdio. h> void print 1(int n){ if (n>=0){ printf("%d ", n); print 1(n-1); } } void main(){ int i = 3; print 1(i); putchar('n'); } 32 1 0
2 דוגמא - הדפסת מספרים #include <stdio. h> void print 2(int n){ if (n>=0){ print 2(n-1); printf("%d ", n); } } void main(){ int i = 3; print 2(i); putchar('n'); } 01 2 3
3 דוגמא - הדפסת מספרים #include <stdio. h> void print 3(int n){ if (n>=0){ printf("%d ", n); print 3(n-1); printf("%d ", n); } } void main(){ int i = 3; print 3(i); putchar('n'); } 32 1 0 01 2 3
4 דוגמא - הדפסת מספרים #include <stdio. h> void print 4(int n){ if (n>=0){ print 4(n-1); printf("%d ", n); print 4(n-1); } 01 0 } void main(){ int i = 3; print 4(i); putchar('n'); } 2 01 0 301 0 2 01 0
Another example - power y X = x*x*…*x y times Recursive definitions (assume non-negative y): Base: x 0=1 y y-1 1. X = X*(X ) y y/2 2 2. X = (X ) (for even y’s only)
Fibonacci Series Fibonacci definition: n 0 = 0 n 1 = 1 nn = nn-1 + nn-2 0 1 1 2 3 5 8 13 21 34 55 …
Fibonacci Iterative void fibonacci(int n) { int Fn, Fn 1, Fn 2, ind; Fn 2 = 0 ; Fn 1 = 1 ; Fn = 0 ; if ( n == 1 ) Fn = 1 ; for (ind=2 ; ind <= n ; ind++){ Fn = Fn 1 + Fn 2; Fn 2 = Fn 1; Fn 1 = Fn; } printf("F(%d) = %d n", n, Fn); }
Fibonacci Recursive int fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; return fibonacci(n-1) + fibonacci(n-2); fibonacci(5) } fibonacci(4) fibonacci(3) fibonacci(2) fibonacci(1) fibonacci(0) fibonacci(1)
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… y 5
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… y 5
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… y 5
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… y 5
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… 2*… y 5
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 x 5 y 2 Returns… 4
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 x 5 y 2 Returns… 4
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 x 5 y 2 Returns… 4
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 x 5 y 2 Returns… 4 Returns… square(…)
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 x Returns… 2 Returns… 4 y 2 square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 x Returns… 2 Returns… 4 y 2 square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 x Returns… 2 Returns… 4 y 2 square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 x Returns… 2 Returns… 4 y 2 square(…) Returns… square(…)
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns… 2*…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 y Returns… rec_pow(2, 0) square(…) 2 x 1 y Returns… square(…) 2 0 Returns… 2*… Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 y Returns… rec_pow(2, 0) square(…) 2 x 1 y Returns… square(…) 2 0 Returns… 2*… Returns…
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 y Returns… rec_pow(2, 0) square(…) 2 x 1 y Returns… square(…) 2 0 Returns… 2*… Returns… 1
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 rec_pow(2, 4 y 1) Returns… x 2 x Returns… 2 y square(…) 2 1 Returns… square(…) Returns… 2*1
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 rec_pow(2, 5 y 2) x 2 x Returns… 2 Returns… 4 y 2 square(…) Returns… square(2)
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x y 4) rec_pow(2, 2 x 5 y 2 Returns… 4 Returns… square(4)
rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x, y/2)); else return x*rec_pow(x, y-1); } rec_pow(2, 5) x 2 Returns… 2*16 y 5
Exercise Write a program that receives two non-negative integers and computes their product recursively. Not * multiple operator !! Hint: Notice that the product a*b is actually a+a+…+a (b times).
Solution int rec. Mult( int x, int y ) { if( x == 0) return 0; return y + rec. Mult( x-1, y); }
Exercise Given the following iterative version of sum-of-digits calculation Find the recursive definition of this function (don’t forget the base case!) int sum_digits(int n){ int sum = 0; while (n > 0) { sum += n%10; n = n/10; } return sum; }
Solution int sum. Of. Digits( int x ) { if( x < 0) x *= -1; if( x == 0 ) return 0; else return x % 10 + sum. Of. Digits( x / 10 ); }
More uses Recursion is a general approach to programming functions. Its uses are not confined to calculating mathematical expressions! For example : write a function that finds the max member in an array of integer.
Solution int rec_max(int arr[ ], int size){ int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } }
חיפוש בינארי int Binary. Search(int arr[], int x, int left, int right) { int middle; if(left>right) return -1; else { middle=(left+right)/2); if(arr[middle]==x) return middle; else if(x < arr[middle]) return Binary. Search(arr, x, left, middle-1); else return Binary. Search(arr, x, middle+1, right); } }
Towers of Hanoi The Towers of Hanoi problem consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following rules: Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. No disk may be placed on top of a smaller disk.
Towers of Hanoi
Recursive Solution To move n disks from peg A to peg C: move n− 1 disks from A to B. This leaves disk #n alone on peg A move disk #n from A to C move n− 1 disks from B to C so they sit on disk #n
Recursive Solution - Function void hanoi(int x, char from, char to, char aux){ if(x==1){ printf("Move Disk From %c to %cn", from, to); } else { hanoi(x-1, from, aux, to); printf("Move Disk From %c to %cn", from, to); hanoi(x-1, aux, to, from); } }
Scope of variables - reminder A variable declared within a function is unrelated to variables declared elsewhere. A function cannot access variables that are declared in other functions.
- To understand recursion you must understand recursion
- Arg
- Arg and poopy
- Arg
- Xyloprin
- Thesis reminder
- Stir you up by way of reminder
- Phytel appointment reminder
- What does advertising mean?
- I693 reminder letter
- Growth mindset crumpled paper activity
- Binder reminder
- Critical reminder
- Adjectives for reminder
- Computation symbols
- Pda language
- Pda
- Pda reminder 1
- In dna adenine always pairs with
- Thanks for the reminder.
- Sql reminder
- Resolution of flowers for algernon
- Vba recursion
- Using recursion in models and decision making
- Direct and indirect recursion
- Phoneme vs morpheme
- Recursive descent parser
- Tail recursion
- Remove indirect left recursion
- Recursion error fanfic
- Iterative vs recursive
- Box trace recursion
- Recursive form
- What is recursion can be a powerful tool for solving?
- Anagram recursion java
- Big 0
- Recursion time complexity
- Recursion in java
- Linear recursion example
- Box trace recursion
- Recursion ap computer science
- Prolog recursion
- English ruler recursion
- Recursion time complexity
- Mips recursive
- Left recursion
- Recursion algorithm
- Recursion matlab
- Generative recursion
- Recursion
- Recursion vs loop
- Tail recursive
- Recursion
- Clojure recur
- Recursion in assembly
- Left recursion
- Using recursion in models and decision making sheet 3
- Telic recursion
- Recursion tree method
- Replace recursion with iteration
- Python recursion practice
- Recursion in assembly language 8086
- Mc escher drawing hands
- Application of recursion
- What is inline function
- Recursion in discrete mathematics
- Cs106b lectures