I need help writing a program using Fibonacci sequence

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: gpleonar is an unknown quantity at this point 
Solved Threads: 0
gpleonar gpleonar is offline Offline
Newbie Poster

I need help writing a program using Fibonacci sequence

 
0
  #1
Nov 3rd, 2004
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;
}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: I need help writing a program using Fibonacci sequence

 
0
  #2
Nov 3rd, 2004
You have some mistakes in this code:
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
  1. for (fNN = 1; fNN >= fN; fNN++)
  2. fNN = (fN - 1) + (fN - 2);
  3. cout << fN;
you are adding the same term, subtracting from it first 1, then 2..this is not exactly what you want...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: I need help writing a program using Fibonacci sequence

 
0
  #3
Nov 3rd, 2004
Here you have some sample code:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int main ()
  5. {
  6. int a[128];
  7. int fN;
  8. int i;
  9.  
  10. a[0]=1;
  11. a[1]=1;
  12.  
  13. cout << "How many n terms do you want in the sequence: ";
  14. cin >> i;
  15.  
  16. for ( fN = 2 ; fN <= i ; fN++ )
  17. {
  18. a[fN]=a[fN-1]+a[fN-2];
  19. cout << a[fN] <<"\n";
  20. }
  21. }
It has some limitations (you can't enter a value >128, for example), but I hope you get the idea.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 48
Reputation: naya22 is an unknown quantity at this point 
Solved Threads: 0
naya22's Avatar
naya22 naya22 is offline Offline
Light Poster

Re: I need help writing a program using Fibonacci sequence

 
0
  #4
Apr 29th, 2007
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??
If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for a lifetime.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: I need help writing a program using Fibonacci sequence

 
0
  #5
Apr 29th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: I need help writing a program using Fibonacci sequence

 
0
  #6
Apr 29th, 2007
> Nov 3rd 2004
> 2 Minutes Ago
Lets see, 2 and a half years.
Now, what was your question again?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: I need help writing a program using Fibonacci sequence

 
0
  #7
Apr 29th, 2007
I don't see a problem resurrecting the thread in this case.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC