Problem

Rewrite the fib function in Listing using tail recursion.Listing ComputeFibonacci.cpp1#inc...

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 }

Step-by-Step Solution

Request Professional Solution

Request Solution!

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.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT