I got this from my lecture.
It's correct but my compiler won't take it. It says that the int f [100] =0 is invalid....but why? My teacher ran it and it worked, so take a look below.
Here's a look at my code.
//Memoization with Fibonacci Numbers
#include %26lt;iostream%26gt;
using namespace std;
int f[100] = 0; //initialize array to 0.
long fib ( int n ){
if ( n == 1 || n == 2) //base case
return 1;
if ( f[n] %26gt; 0 )
return f[n];
else
return f[n] = fib (n-1) + fib (n-2);
}
int main () {
int k;
cin %26gt;%26gt; k;
for ( int i =1; i %26lt;=k; i++)
cout %26lt;%26lt; i %26lt;%26lt;" "%26lt;%26lt; fib(i)%26lt;%26lt; endl;
return 0;
}
C++ Help...this is Urgent!?
int f[100] = {0}; //initialize array to 0.
Enjoy :)
Reply:I am more familiar with Visual Basic.NET, but I notice that you initiated an array with only one value. I know that in VB that would be unacceptable, but perhaps in C that is allowed.
Wish I could be of more help to you, but good luck, and, by the way, thank you for your honesty, that this is for your class. (A lot of people are demanding that others do their homework for them, disguising their question to look like it's NOT a homework assignment, and yet it obviously IS one - LOL), so I DO thank you for your honesty, and I truly hope that you DO find the cause of your problem.
Like I said, in VB, if you initiate an array variable, you need to initialize the entire array, not just a single value.
tarot cards
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment