Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is supposed to write 1 1 2 3 5 8 13 21 but it probably does not. Wh

ID: 3673708 • Letter: T

Question

This program is supposed to write 1 1 2 3 5 8 13 21     but it probably does not. What is the program doing that is incorrect? (We're not asking you explain why the incorrect action leads to the particular outcome it does, and we're not asking you to propose a fix to the problem.)

#include <iostream>
using namespace std;

int fibonacci( int n )
{
int tmp;
int a = 1;
int b = 1;   

for (int i = 0; i < n-2; i++)
{
  tmp = a+b;
a = b;
  b = tmp;
}
  return b;
}

int* computeFibonacciSequence(int& n)
{
int arr[8];
n = 8;
for (int k = 0; k < n; k++)
{
arr[k] = fibonacci( k );
}
return arr;
}

int main()
{
int m;
int* ptr = computeFibonacciSequence(m);
for (int i = 0; i < m; i++)
{
cout << ptr[i] << ' ';
}
return( 0 );
}

Explanation / Answer

The function int fibonacci( int n ) ,

During the third instance, for iteration n=2, program has to start summing up the two previous fibonacci numbers. But it does not. This is what the program is doing that is incorrect.

--

During the third instance, for iteration n=2, loop does not execute[becuase i<n-2 is false].

I have answered the Question as per instructions. If the answer does not meet your expectation, please revert back.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote