The trouble is that you test for i < n (where n == 3 in your example call), but you return f[3] which has not been set to anything. You are 'lucky' that you're getting zeroes rather than random garbage.
Change the '<' to '<='.
Working Code #1
Retaining the full size array.
#include <iostream>
using namespace std;
static int fibonacci(int n)
{
int f[100] = { 0, 1 };
if (n < 0 || n > 100)
return -1;
else if (n < 2)
return f[n];
for (int i = 2; i <= n; i++)
{
f[i] = f[i-2] + f[i-1];
//cout << "f[" << i << "] = " << f[i] << endl;
}
return f[n];
}
int main()
{
for (int i = 0; i < 8; i++)
cout << "fib(" << i << ") = " << fibonacci(i) << endl;
return 0;
}
Sample Output #1
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
Working Code #2
This uses an array of size 3, at the cost of a lot of modulo operations:
#include <iostream>
using namespace std;
static int fibonacci(int n)
{
int f[3] = { 0, 1, 0 };
if (n < 0 || n > 100)
return -1;
else if (n < 2)
return f[n];
for (int i = 2; i <= n; i++)
{
f[i%3] = f[(i-2)%3] + f[(i-1)%3];
//cout << "f[" << i << "] = " << f[i%3] << endl;
}
return f[n%3];
}
int main()
{
for (int i = 0; i < 8; i++)
cout << "fib(" << i << ") = " << fibonacci(i) << endl;
return 0;
}
It produces the same output - so there is no point in repeating it.
Working Code #3
Avoiding arrays and modulo operations:
#include <iostream>
using namespace std;
static int fibonacci(int n)
{
int f0 = 0;
int f1 = 1;
if (n < 0 || n > 46)
return -1;
else if (n == 0)
return f0;
else if (n == 1)
return f1;
int fn;
for (int i = 2; i <= n; i++)
{
int fn = f0 + f1;
f0 = f1;
f1 = fn;
//cout << "f[" << i << "] = " << fn << endl;
}
return f1;
}
int main()
{
for (int i = -2; i < 50; i++)
cout << "fib(" << i << ") = " << fibonacci(i) << endl;
return 0;
}
The limit 46 is empirically determined as correct for 32-bit signed integers.
Example Output #3
fib(-2) = -1
fib(-1) = -1
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
fib(11) = 89
fib(12) = 144
fib(13) = 233
fib(14) = 377
fib(15) = 610
fib(16) = 987
fib(17) = 1597
fib(18) = 2584
fib(19) = 4181
fib(20) = 6765
fib(21) = 10946
fib(22) = 17711
fib(23) = 28657
fib(24) = 46368
fib(25) = 75025
fib(26) = 121393
fib(27) = 196418
fib(28) = 317811
fib(29) = 514229
fib(30) = 832040
fib(31) = 1346269
fib(32) = 2178309
fib(33) = 3524578
fib(34) = 5702887
fib(35) = 9227465
fib(36) = 14930352
fib(37) = 24157817
fib(38) = 39088169
fib(39) = 63245986
fib(40) = 102334155
fib(41) = 165580141
fib(42) = 267914296
fib(43) = 433494437
fib(44) = 701408733
fib(45) = 1134903170
fib(46) = 1836311903
fib(47) = -1
fib(48) = -1
fib(49) = -1
int f[100] = { 0, 1 }and it will initialize all the other elements to 0 automatically. Also, you could make the array static (and add a static counter of the last calculated position) so you don't have to recalculate values you already know every time. Just FYIfibonacci(48). Useuint64_tinstead ofintto get correct values upton==94. codepad.org/ApUew5IYfarray if your measurements show that a program spends too much time in thefibonacci()function codepad.org/Ve7pvSjP