DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   I need help writing a program using Fibonacci sequence (http://www.daniweb.com/forums/thread13448.html)

gpleonar Nov 3rd, 2004 2:59 pm
I need help writing a program using Fibonacci sequence
 
The program needs to ask for n and then output the first n terms of the Fibonacci sequence.

exp.
n = 6
1, 1, 2, 3, 5, 8
1+1 =2
2+1=3
3+2=5
5+3=8

My origional program used a nested for loop.
I am not sure if that is right to use in this type a situation.
I would like suggestions to make it work.

#include <iostream>

using namespace std;
int main ()
{
int fOne = 1;
int fTwo = 1;
int fThree = 2;
long fN;
long fNN;

cout << "How many n terms do you want in the sequence: ";
cin >> fN;

for ( fN = 1 ; fN >= 3 ; fN++ )
{
for (fNN = 1; fNN >= fN; fNN++)
fNN = (fN - 1) + (fN - 2);
cout << fN;
}
}

frrossk Nov 3rd, 2004 3:09 pm
Re: I need help writing a program using Fibonacci sequence
 
You have some mistakes in this code:
Quote:

cin >> fN;

for ( fN = 1 ; fN >= 3 ; fN++ )
As you introduce fN from the standard input, why are you initializing it in the for loop? More, if you are initializing fN = 1, then the condition in for loop fN >= 3 is never accomplished.

Then, for the Fibonacci series, as I know, every term of the series, starting with the third term, is the sum of the 2 preceding terms. In a vector, this should be a[n] = a[n-1] + a[n-2], right?
Or, in the for loop
for (fNN = 1; fNN >= fN; fNN++)
    fNN = (fN - 1) + (fN - 2);
cout << fN;
you are adding the same term, subtracting from it first 1, then 2..this is not exactly what you want...

frrossk Nov 3rd, 2004 3:37 pm
Re: I need help writing a program using Fibonacci sequence
 
Here you have some sample code:
#include <iostream> 

using namespace std;
int main ()
{
int a[128];
int fN;
int i;

a[0]=1;
a[1]=1;

cout << "How many n terms do you want in the sequence: ";
cin >> i;

for ( fN = 2 ; fN <= i ; fN++ )
{
a[fN]=a[fN-1]+a[fN-2];
cout << a[fN] <<"\n";
}
}
It has some limitations (you can't enter a value >128, for example), but I hope you get the idea.

naya22 Apr 29th, 2007 9:30 am
Re: I need help writing a program using Fibonacci sequence
 
Okay, when I ran this program, I saw one error:
1. I did not see the first two ones in the Fibonacci sequence.

Also, can you tell me that the a and the i means in this program??

Narue Apr 29th, 2007 9:33 am
Re: I need help writing a program using Fibonacci sequence
 
>1. I did not see the first two ones in the Fibonacci sequence.
Probably because those values weren't printed. They're in the generated sequence though. frrossk's program kind of sucks anyway.

>Also, can you tell me that the a and the i means in this program??
a is an array that holds each number in the sequence and i is the expected length of the sequence.

Salem Apr 29th, 2007 9:35 am
Re: I need help writing a program using Fibonacci sequence
 
> Nov 3rd 2004
> 2 Minutes Ago
Lets see, 2 and a half years.
Now, what was your question again?

Narue Apr 29th, 2007 9:36 am
Re: I need help writing a program using Fibonacci sequence
 
I don't see a problem resurrecting the thread in this case.


All times are GMT -4. The time now is 7:32 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC