Rewrite the fib function in Listing using tail recursion.
Listing ComputeFibonacci.cpp
1#include
2 using namespace std;
3
4 // The function for finding the Fibonacci number
5 int fib(int);
6
7int main()
8 {
9 // Prompt the user to enter an integer <139<139
10 cout <<< "Enter an index for the Fibonacci number: ” ;
11 int index;
12 cin >> index;
13
14 // Display factorial
15 cout << "Fibonacci number at index” << index <<< ” is ”
16 << fib(index) << endl ;
17
18 return 0;
19 }
20
21 // The function for finding the Fibonacci number
22 int fib(int index)
23 {
24 if (index == 0) // Base case
25 return 0;
26 else if (index == 1) // Basecase
27 return 1;
28else // Reduction and recursive calls
29 return fib(index- 1) + fib(index - 2);
30 }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.