Problem with Fibonacci assignment

im having difficulty understanding this assignment

Write a program that will trace how Fibonacci numbers are generated recursively (for any N) and display the trace in the following way :



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Example (N=4):

Entering level 0
        Entering level 2
                Entering level 4
                Exiting level 4
            Entering level 3
            Exiting level 3
        Exiting level 2
    Entering level 1
            Entering level 3
            Exiting level 3
        Entering level 2
                Entering level 4
                Exiting level 4
            Entering level 3
            Exiting level 3
        Exiting level 2
    Exiting level 1
Exiting level 0


i dont see how the level relates...

heres my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
int fib(int n);
int main()
{
    for(int i=1; i<=10; i++)
    {
        cout << fib(i) << endl;
    }
    
    system("PAUSE");
    return 0;
}

/***********functions*************/
int fib(int n)
{
   if(n<=0)
  {
       //cout << "-" << endl;
      return 0;
  } 
  if(1 == n)
  {
      return 1;
  }
  else
  {
      return fib(n-1) + fib(n-2);
  }

}
The problem should've been more informative. Anyways this is all what I can do..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void FillTabs(int level)
{
        //As many tabs as the level
    for (int i=0;i<level;i++)
        cout << "    ";
}
/***********functions*************/
int fib(int n,int level)
{

  FillTabs(level);
  cout << "Entering Level " << level<<endl;

  int result =0;
   if(n<=0)
  {
    result = 0;
  }
  else if(1 == n)
  {
    result =1;
  }
  else
  {
      int fib1 = fib(n-1,level+2);
      int fib2 = fib(n-2,level+1);
      result =  fib1+fib2;
  }

    FillTabs(level);
    cout << "Exiting Level " << level<<endl;
    return result;
}

Topic archived. No new replies allowed.