>And remember, the C++ standard includes the C standard, more or less.
Standard C++ currently maintains compatibility with C89, not C99. So you can't use VLAs in C++
>Is my program the best it can be (using recursion and arrays)?
No. The entire point of using an array with recursion is to avoid recalculating everything all the time. This is especially true with the Fibonacci sequence, where a
lot of the numbers have to be recalculated. You use an array, but you don't take advantage of it, so your function is just a slower, more wasteful version of the original recursive algorithm.
What you need is for the array to persist between function calls and be able to tell if a number has been calculated or not. If it has, you don't need to go through the recursive chain. This is where you'll see performance improvements:
int fib ( int n )
{
static int fibArray[25] = {0};
if ( n <= 1 )
return 1;
else {
fibArray[0] = 1;
fibArray[1] = 1;
for ( int i = 2 ; i <= n ; i++ ) {
if ( fibArray[i] == 0 )
fibArray[i] = ( fib ( n - 1 ) + ( fib ( n - 2 ) ) );
}
}
return fibArray[n];
}