What is going wrong in this code (fibonacci sequence)?

  c++, fibonacci

Here is the problem, I know how it works (the fibonacci sequence) and I know what I am supposed to do, but I don’t know what is going wrong. I don’t wanna see how someone else did it cause that would ruin the fun of this, I want to write my own code to do it.

this is how far ive gotten:

#include <iostream>
using namespace std;

int fibr(int ans) {
    int fib[5] = { 0, 0, 0, 0, 0 };

    for (int i = 0; i < 5; i++)
    {
        if (fib[i] > 1)
        {
            fib[i] = fib[i - 2] + fib[i - 1];
        }
        else
        {
            fib[i] = 1;
        }
    }


    return fib[ans];
}

int main() {
    int ans;
    cin >> ans;

    cout << fibr(ans);

    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT