0

I wrote this code to produce a fibonacci seqeunce to a cretain number. However, when you get to a certain number, it starts producing negative numbers, and I don't know what to do.

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
long lMax;
lMax = 0;
long x;
int Count;
cout << "Fibonacci Sequence Generator\nGenerate to what number?\n";
cin >> lMax;
int FibOut[lMax+1];
FibOut[1] = 1;
FibOut[2] = 1;
Count = 0;
do
{
Count++;
if (Count < 3)
{
Count = 3;
}
FibOut[Count] = FibOut[Count - 2] + FibOut[Count - 1];
}
while (Count < lMax);
Count = 0;
do
{
Count++;
cout << FibOut[Count] << "\n";
}
while (Count < lMax);
cout << "Press ENTER to end...\n";
x = cin.get();
return 0;
}
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Salem
0

I wrote this code to produce a fibonacci seqeunce to a cretain number. However, when you get to a certain number, it starts producing negative numbers, and I don't know what to do.

Can you be slightly less vague than "a certain number"? It helps to give us all the information you have.

Also please format your code. It's very difficult to follow without proper indentation.

0

> it starts producing negative numbers
Yeah, somewhere between 40 and 50 IIRC, the size of F(n) becomes larger than what can be stored in a 32-bit integer and that's what you're seeing.

One fix is to use a bignum package like GMP

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.