Only works til 37th value

I have written a program for the moosonocci sequence, but after the 37th value it starts producing negative numbers. I don't know why it is doing this, any suggestions?

#include <iostream>

using namespace std;

void moosonocci( int size)
{
int myseries [ size ];
myseries[0] =1;
myseries[1]=1;
myseries[2]=1;

cout << myseries[0] << endl << myseries[1] << myseries[2] << endl;

for (int i=3; i<size; i++) {
myseries[i]= myseries [i-1] + myseries[i-2] + myseries [i-3];
cout << myseries[i] << endl;
}
}

void moosonocci1( int series[], int size)
{
if (size==0)
return;
moosonocci1 (series, size-1);

if (size ==1 || size ==2 || size==3) {
series[size -1] = 1;
}
else {

series [size -1] = series[size-2] + series[size-3] + series [size-4];
}
}

int main (void)
{
int myseries[10];
moosonocci1 (myseries, 37);
for (int i=0; i<37; i++)
cout << myseries[i] << endl;


return 0;
}
If a signed integral value gets too high, it will wrap back to the negative values, if you use unsigned long instead of int
( or unsigned long long if your compiler supports it ) you can have a bit higher values

Topic archived. No new replies allowed.