My problem is that my program doesn't print out the first number in the sequence. Like if i ask it to print out 10 numbers it will only print out 9 numbers and skip the first number that it should start with. example of the output is at the bottom below the 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";
}
}

Example of the output:

How many n terms do you want in the sequence: 10
2
3
5
8
13
21
34
55
89
Press any key to continue . . .

Edit: My bad, I just re-read the code.
A fibonacci sequence starts with 0 for a reason, you omitted it.
Also, you print starting from a[2] (fN=2)

cout << a[0] << "\n" << a[1] << "\n";
for(fN=2;fN<=i;++fN) { // note that because fN starts at 2, the range from 2 - 10 is only 9 numbers including 10, use (i+1) if you want 1 more
  a[fN]=a[fN-1]+a[fN-2];
  cout << a[fN] <<"\n";
}

So then i should change the array at which it starts at to a[-1] ?
Or what should i change to get the program to print out the first numbers

I edited my post, please read it again.

a[-1] would -not- do what you are thinking, don't do that. At best it would be a[2^32] and crash your program due to access violation, and at worst it wouldn't compile. Or maybe the best/worst cases are opposite, who knows.

Just print the first 2 before your loop.
I'd also include 0 in your sequence for completeness.
If you want your range to be 10 numbers, change fN<=i to fN<=(i+1)

The fibonacci sequence is:
0,1,1,2,3,5,8, .... etc

You are missing the first three numbers in the sequence. You can do it the way unimportant suggested, but you will need to catch the exceptions if the user enters 1 as i, in which case you will just want to display the 0.

ideally, I think you should do

a[0]=0;
a[1]=1; 
cout << "How many n terms do you want in the sequence: "; //you missed this semicolon
cin >> i;
for ( fN = 0 ; fN < i ; fN++ )
{
    a[fN+2]=a[fN]+a[fN+1];
    cout << a[fN] <<"\n";
}

For an input of 5 as i, it will display 0,1,1,2,3 .. It will calculate the next to numbers, but will not display them. If you don't want to calculate the extra 2 positions in the array, you can add an if statement if (fN+2 < i) then calculate next value.

#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 = 0 ; fN < i ; fN++ )
{
	a[fN+2]=a[fN]+a[fN-1];
	cout << a[fN] <<"\n";
}
}

I input this as you said and i get this as an output :

How many n terms do you want in the sequence: 5
1
1
-858993459
2
-858993458
Press any key to continue . . .

To me it makes more sense to start from the current index and add the previous 2:

#include <iostream> 

using namespace std;

int main()
{
    int a[128] = {1, 1};
    int terms;
    cout << "How many n terms do you want in the sequence: ";
    cin >> terms;
    for (int x = 2; x < terms; ++x)
        a[x] = a[x - 1] + a[x - 2];
    for (int x = 0; x < terms; ++x)
        cout << a[x] << endl;
}

But the bug in your code is you are using fN-1 instead of fN+1 in the second operand for the addition:

a[fN+2]=a[fN]+a[fN-1];

should be

a[fN+2]=a[fN]+a[fN+1];

with the operand changed from [fN-1] to [fN+1], it makes the sequence work now, thank you very much for your help, i knew something wasn't right with one of my operands when it gave me a negative integer.

#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 = 0 ; fN < i ; fN++ )
{
	a[fN+2]=a[fN]+a[fN-1];
	cout << a[fN] <<"\n";
}
}

I input this as you said and i get this as an output :

How many n terms do you want in the sequence: 5
1
1
-858993459
2
-858993458
Press any key to continue . . .

Sorry, I had a typo ... fixed it in the post ... line 18 should be

a[fN+2]=a[fN]+a[fN+1];  //change the - to a +

I'm working without a compiler :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.