Fibonacci Numbers

[php]#include <iostream>
using namespace std;

// The function for finding the Fibonacci number
int fib(int);

int main()
{
// Prompt the user to enter an integer
cout << "Enter an index for the Fibonacci number: ";
int index;
cin >> index;

// Display factorial
cout << "Fibonacci number at index " << index << " is "
<< fib(index) << endl;

return 0;
}

// The function for finding the Fibonacci number
int fib(int index)
{
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);
}
[/php]

So i have that code, and i'm supposed to find the number of times the 'fib' function is called.

Any idea how to do that?
Last edited on
You can make a global variable initialized to 0 and make it increasing each time fib get called
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned calls = 0;
//...
int main()
{
    //...
   cout << "'fib' called " << calls << " times";
}

int fib(int index)
{
   calls++;
   //...
}
Last edited on
Topic archived. No new replies allowed.