C Help I need help correcting my current program so it outpu

C++ Help

I need help correcting my current program so it outputs the expecte results pasted below. I am including the proble statement, my current code, and my results versus expected results. This should be basic C++ using functions and loops. There shouldnt be hard coding.

Problem statement:

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two; for example, the first 6 Fibonacci numbers are 0, 1, 1, 2, 3, 5. Write a program that asks the user for a number between 5 and 22 (inclusive) and then displays that many Fibonacci numbers. YOU MUST write a function called fibonacci which is called from main as shown in the template. Note that the function adds up each of the Fibonacci numbers and returns the last one to main. Add a newline every 10th output value. For example:

My current code:

#include <iostream>
using namespace std;

int fibonacci(int number1, int number2);

int main() {
  
   int result;
   int userValue;
   int number1;
   int number2;
   int minValue = 5;
   int maxValue = 22;
   int newLine = 10;
   int fibonacciArray[] = {0,1,1,2,3,5,8,13,21,34,55};
  
   cout << \"Please enter a number between 5 and 22:\" << endl;
   cin >> userValue;
   cout << userValue;
   number1 = fibonacciArray[userValue - 2];
   number2 = fibonacciArray[userValue - 1];
   result = fibonacci(number1,number2);
  
   if(userValue < minValue || userValue > maxValue){
       cout << \"Please follow the directions!\" << endl;
   } else {
       for(int i = 0; i < userValue; i++){
           if(i == newLine - 1){
               cout << endl;
           }
           cout << \" \" << fibonacciArray[i];
       }
       cout << endl;
       cout << \"Fibonacci # \" << userValue << \" is \" << result << endl;
   }
  
   return 0;
}

int fibonacci(int number1, int number2){
  
   return number1 + number2;
  
}

My results versus expected results:

Input

12

Your output

Please enter a number between 5 and 22: 12

0 1 1 2 3 5 8 13 21 34 55 0

Fibonacci # 12 is 55

Expected output

Please enter a number between 5 and 22: 12

0 1 1 2 3 5 8 13 21 34 55 89

Fibonacci # 12 is 144

Compare output

1/1

Input

23

Your output

Please enter a number between 5 and 22: 23

Please follow the directions!

12

Your output

Please enter a number between 5 and 22: 12

0 1 1 2 3 5 8 13 21 34 55 0

Fibonacci # 12 is 55

Expected output

Please enter a number between 5 and 22: 12

0 1 1 2 3 5 8 13 21 34 55 89

Fibonacci # 12 is 144

Solution

I have corrected the code :

#include <iostream>
using namespace std;

int fib[23];
void fibonacci();

int main() {

    int result;
    int userValue;
    int number1;
    int number2;
    int minValue = 5;
    int maxValue = 22;
    int newLine = 10;
    fibonacci();
  
    cout << \"Please enter a number between 5 and 22:\" << endl;
    cin >> userValue;

    if(userValue < minValue || userValue > maxValue){
        cout << \"Please follow the directions!\" << endl;
    } else {
        for(int i = 0; i < userValue; i++){
            if(i == newLine - 1){
                cout << endl;
            }
            cout << \" \" << fib[i];
        }
        cout << endl;
        cout << \"Fibonacci # \" << userValue << \" is \" << fib[userValue] << endl;
    }
  
    return 0;
}

void fibonacci(){
    fib[0] = 0;
    fib[1] = 1;
    for(int i = 2; i <= 22; i++){
        fib[i] = fib[i-1] + fib[i-2];
    }
}

C++ Help I need help correcting my current program so it outputs the expecte results pasted below. I am including the proble statement, my current code, and my
C++ Help I need help correcting my current program so it outputs the expecte results pasted below. I am including the proble statement, my current code, and my
C++ Help I need help correcting my current program so it outputs the expecte results pasted below. I am including the proble statement, my current code, and my

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site