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: 3674120 • 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

#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= new int[n];
for (int k = 0; k < n; k++)
{
arr[k] = fibonacci( k );
}
return arr;
}

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

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