#Why my code is generating more values than initialized in array

12 messages · Page 1 of 1 (latest)

mortal mango
#

#include<iostream>
'''hsusing namespace std;
int main () {
int a[100];
a[1]=1;
a[0]=0;

for(int i=2; i<100; i++) {
    a[i]=a[i-1]+a[i-2];
}

for(int i=0; i<100; i++){
    cout<<a[i]<<" ";
}

return 0;

}

#

After generating a sequence of 100 fibonacci numbers, it does not stop and keeps generating false values and then at some point it stops

#

I don't understand why this is happening

#

If anyone can help

shrewd ice
#

i ran your code and it generates negative values after some point

#

that's because it overflows

#

meaning that an int (which is typically 32 bits) can only represents numbers in the range [2^31, 2^31 - 1]

#

try using a data type that is able to hold larger values

#

long, for example