943,989 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 34075
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 3rd, 2004
0

I need help writing a program using Fibonacci sequence

Expand Post »
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;
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gpleonar is offline Offline
2 posts
since Oct 2004
Nov 3rd, 2004
0

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
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 3rd, 2004
0

Re: I need help writing a program using Fibonacci sequence

Here you have some sample code:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Apr 29th, 2007
0

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??
Reputation Points: 24
Solved Threads: 0
Light Poster
naya22 is offline Offline
48 posts
since Apr 2007
Apr 29th, 2007
0

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 29th, 2007
0

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 29th, 2007
0

Re: I need help writing a program using Fibonacci sequence

I don't see a problem resurrecting the thread in this case.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 3rd, 2010
-2
Re: I need help writing a program using Fibonacci sequence
hey i have this code..... n it works


# include <iostream.h>
void main ()
{
int x=0, y=1, b, n=0,ter;

cout<<"Enter The number of terms";
cin>>ter;
cout<<x<<" "<<y<<" ";

while (n<ter-1)
{
b=x+y;
cout<<b<<" ";
x=y;
y=b;

n++;
}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DrAgon PrinceSS is offline Offline
2 posts
since Mar 2010
Mar 3rd, 2010
1
Re: I need help writing a program using Fibonacci sequence
you do realize this thread is almost 6-years old... the OP has probably completely forgotten about it by now.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Mar 3rd, 2010
0
Re: I need help writing a program using Fibonacci sequence
LOL. wht can i say .. i just started c++ and made this prog today.!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DrAgon PrinceSS is offline Offline
2 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Windows Forms in C++
Next Thread in C++ Forum Timeline: How to ping????





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC