Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Fibonacci number

I am having problem displaying all 10 of the Fibonacci number. It is the first 10 only that I need to display. I need to display all 10 on the Windows form right now only 55 displays. The console I got to work its when I tried to get the form to work when the trouble begain. Any help would be appreciated. Thanks!!!

#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll> // MessageBox
#include <stdio.h>
using namespace System;
using namespace System::Windows::Forms; // MessageBox
int __stdcall WinMain()

{ int f[10]; //This defines an array called f of 10 integers
int n;

f[0] = 1; //F(0) and F(1) is equal to one
f[1] = 1;


for(n = 2; n < 10; n++)

f[n] = f[n-1] + f[n-2]; //recursive function

for (n = 0; n < 10; n++)


String * str;

str=S"Fibonacci Number";


MessageBox::Show(f[n-1].ToString(),str);





return 0;

}
[1130 byte] By [Fosterd] at [2007-11-11 7:32:16]
# 1 Re: Fibonacci number
Hi,

looks like you forgot some brackets... {}
C/C++ only takes the immediate next statement in for/if/while loops, if {} are omitted.
Try
fo(...)
{
//body first loop
for(...)
{
// body second loop
}
// remainder body first loop
}

Cheers,

Dieter
drkybelk at 2007-11-11 21:02:10 >
# 2 Re: Fibonacci number
int fib( int n ) {
/** the 0th and 1st terms of the sequence are 1 */
if ( n == 0 || n == 1 ) {
return 1;
} else {
return (fib( n - 1 ) + fib( n - 2 )); // sum of the previous two terms
}
}

that will give you the nth term of the fibonacci sequence (fib(0) is 1, fib(1) is 1, fib(2) is 2, fib(3) is 3, fib(4) is 5, etc...)

so you can do:

for (int i = 0; i < 10; i++) {
cout << fib( i ) << endl;
}
destin at 2007-11-11 21:03:05 >