Question

In: Computer Science

Hi. Please could you assist me with the question below. I found the same question on...

Hi. Please could you assist me with the question below. I found the same question on the Chegg website, unfortunately, the answer given does not make use of the driver function which is required by the question. Please could you provide me with the full code for this question, including the main function using c++ and codeblocks.

The standard recursive version of the Fibonacci function (explained in Malik pg. 379) is extremely inefficient due to identical calls being repeated. Consider the following iterative solution:
int fibonacci( int n )

{

if( n < 2 )

return n;

int oldprev = 0;

int prev = 1;

int result;

for( int i = 2; i <= n; i++ )

{

result = oldprev + prev;

oldprev = prev;

prev = result;

}

return result;

}


This function is far more efficient than the recursive version because it uses values previously computed.

Write a different recursive version of the Fibonacci function that is based on this idea (of the above iterative solution). It must be called from the following driver function:

int fibonacci( int n )

{

int oldprev;

return fibonacci( n, oldprev );

}

Thank You

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

#include <iostream>
using namespace std;

// Helper function for fibonacci series
int helper_fibonacci(int oldprev, int prev, int steps_remaining)
{
//The Answer is here because steps_remaining=0
if(steps_remaining == 0)
return prev;
else if(steps_remaining == -1)
return oldprev;
else
//return the updated values as follows
return helper_fibonacci(prev, oldprev+prev, steps_remaining-1);
}

//fibonacci function here.
int fibonacci( int n )
{
int oldprev=1;
int prev = 1;
return helper_fibonacci(oldprev, prev, n-1 );
}

//Driver program to test the code.
int main()
{
cout<<"The First Ten Fibonacci Numbers are: "<<endl;
for(int i=1;i<=10;i++)
cout<<fibonacci(i)<<", ";
   return 0;
}

=======================

SCREENSHOTS:



Related Solutions

Can someone please assist me with this question. The information that I found was not of...
Can someone please assist me with this question. The information that I found was not of help. Discuss how using systems thinking will influence your decision-making. I have to do a full paragraph on this question.
Hi, could you please assist with this question? You are an investment banking consultant advising a...
Hi, could you please assist with this question? You are an investment banking consultant advising a mining company. The CEO of the company tells you that she believes that capital structure does not have an impact on firm value. On what basis might they make this comment? State whether you agree or not with this position and explain why. Does the nature of this firm’s business risk change your answer? Fully explain your reasoning. Thanks kindly
Hi, could you please assist with this question? You are an investment banking consultant advising an...
Hi, could you please assist with this question? You are an investment banking consultant advising an investment company. The CEO of the company tells you that she believes that capital structure does not have an impact on firm value. On what basis might they make this comment? State whether you agree or not with this position and explain why. Does the nature of this firm’s business risk change your answer? Fully explain your reasoning.
Hi There Could you please assist on below short essay... DOMESTIC workers will no longer be...
Hi There Could you please assist on below short essay... DOMESTIC workers will no longer be subjected to meagre salaries as the government has adjusted the minimum wage for domestic workers, with effect from December last year. The announcement was made by the Department of Labour Minister Mildred Oliphant and it coincided with the signing into law of the National Minimum Wage Bill by President Cyril Ramaphosa. According to the Department of Labour’s new rates, domestic workers working in Area...
Hi please assist me in answering question in short essay type format a & b separate....
Hi please assist me in answering question in short essay type format a & b separate. Thank you so much. 1a) Discuss the cash conversion cycle. What are the 3 stages and how is it calculated. 1b) Discuss what is finiancial planning. Provide meaning of Financial Planning, Types of Financial Plan (medium, short term, long term). What period it covers?
Hi. Please could I get a complete answer to the question below using c++ and codeblocks...
Hi. Please could I get a complete answer to the question below using c++ and codeblocks Add a recursive member function to bSearchTreeType that returns the depth of a given node whose info member contains a specific value, and returns -1 if the node is not in the tree. For example, tree in figure 11-8 on page 622 of Malik,z the depth of 40 is 4, the depth of node 50 is 1 etc. Use the following header:    template<class...
Hi could you respond to the below questions for me pls: What are some of the...
Hi could you respond to the below questions for me pls: What are some of the ethical and legal issues related to physician assisted suicide? What role should patient's preferences and their perspective of quality of life impact whether assisted suicide is legal?
Could someone please assist me with this question. Select one of the strategy formulation analytic tools...
Could someone please assist me with this question. Select one of the strategy formulation analytic tools Special tools they can use to assist them in formulating strategies include the following: and complete based on the information (NIKE) gathered in the Week 3 Learning Activities; the tools to select from are: Space Matrix Boston Consulting Group (BCG) Matrix IE Matrix Once you have completed the tool discuss the outcome in terms of what strategic direction the selected company should take and...
Could you please assist with this question? If possible could you illustrate in Excel? The Langley...
Could you please assist with this question? If possible could you illustrate in Excel? The Langley tire company measures their tire stems to see if they hold air.   They produce 300 tires a month and record the defective rate for each day's production. Below are the results of this month's tests. a. Determine the 3 sigma UCL and LCL b. Does the tire stem process appear to be in statisical control? Day Fraction Defective Day Fraction Defective Day Fraction Defective...
Hi, could you provide me with a brief response to this question and an explanation so...
Hi, could you provide me with a brief response to this question and an explanation so I can understand TM2 has recently internally transferred to our team as a client liaison officer. They get the job done but are slow and their efficiency is below par. Yhey have not met any KPIs, RPC etc and now they are demonstrating bad timekeeping, being late on deliverables and the error margin of their work is increasing causing lost productivity. They are not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT