whats happening here



1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream>
using namespace std;
int recursion(int x);
int main()
{
	
	cout<<recursion(7);
}
int recursion(int x)
{
    if(x==1||x==0) return x;
    //else return recursion(x-1)+recursion(x-2); whats happening here?
}
closed account (SECMoG1T)
 
else return recursion(x-1)+recursion(x-2);

Theee is called a recursive call what happens is that
The function recursion Iis called repeatedly while decreasing the value of x
Until x is true for the base case
if(x==1||x==0) return x;

http://www.cplusplus.com/articles/D2N36Up4/
http://www.cprogramming.com/tutorial/lesson16.html
Last edited on
rec(6)+rec(5) =13
rec(5)+rec(3) =7
rec(1)+rec(1) =2
is this how its running?
Call graph looks like
                rec(7)
     rec(6)       +       rec(5)
rec(5) + rec (4)     rec(4) + rec(3)
//etc
Until it hits 1 or 0 which evaluates to 1. Then in starts roll backward until original is calculated.

It is very uneffective way to calculate Fibonacci numbers.
Topic archived. No new replies allowed.