I am trying to make a program where the user enters a number, then that number is entered into a function and it outputs the element of the Fibonacci sequence that that number is.
It's giving me an error that says
stack around the variable 'fib' was corrupted
I don't know if this is a logic error or a program error. Also I don't know if my logic for finding the Fibonacci number is correct.
#include <iostream>
using namespace std;
int Fibonacci(int num)
{
int fib[] = { 0 }, answer, i = 3;
fib[1] = fib[2] = 1;
fib[0] = 0;
for (i = 3; i < num; i++)
{
fib[i] = fib[i - 2] + fib[i - 1];
}
answer = fib[num];
return answer;
}
int main()
{
int user_number, i = 0;
cin >> user_number;
cout << Fibonacci(user_number) << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire