| | |
I need help writing a program using Fibonacci sequence
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 2
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
You have some mistakes in this code:
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
you are adding the same term, subtracting from it first 1, then 2..this is not exactly what you want...
•
•
•
•
cin >> fN;
for ( fN = 1 ; fN >= 3 ; fN++ )
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)
for (fNN = 1; fNN >= fN; fNN++) fNN = (fN - 1) + (fN - 2); cout << fN;
Here you have some sample code:
It has some limitations (you can't enter a value >128, for example), but I hope you get the idea.
C++ Syntax (Toggle Plain Text)
#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"; } }
>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.
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.
New members chased away this month: 5
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Please help about USER INPUT
- Next Thread: 1%2 ? what is the answer ???
Views: 24269 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






