Reputation: 1
////Even Fibonacci numbers
int i=2;
int sum_of_Even=0;
int fib_array[]={};
fib_array[0]=1;
fib_array[1]=1;
while (fib_array[i]<4000000)
{
fib_array[i]=fib_array[i-1]+fib_array[i-2];
if ((fib_array[i]%2) == 0)
{
sum_of_Even+=fib_array[i];
}
i++;
}
printf("sum of Even terms in the fib sequence = %i\n", sum_of_Even);
On the terminal, the output is 3.. Help! Program looks good...but somehow gives an output of 3 (which is pretty wrong).. Open to suggestions on how to fix this.. Thanks..
Upvotes: 0
Views: 612
Reputation: 153447
Suggestions on how to fix:
And mindful of @Andrey comment:
"For the record: this is a Project Euler question, and people are strongly discouraged from posting solutions to these online."
Use an array of long long fib_array[3]
. To generate Fibonacci numbers only the previous 2 are used to generate the 3rd. After generating a Fibonacci number and testing for evenness, now that you have 3 Fibonacci numbers, discard the eldest and repeat.
OP's present int fib_array[]={};
is not allocating the array needed for OP's approach. fib_array[i]=
causes UB.
Per @Jules solution, even a long
may be insufficient, consider long long
. In anycase , use the matching prinf()
format specifier. Edit: Turns out the answer is < 5,000,000, so long
will work.
long sum_of_Even = 0;
....
printf("sum of Even terms in the fib sequence = %li\n", sum_of_Even);
Upvotes: 0
Reputation: 489
#include <stdio.h>
#include <string.h>
int main()
{
long i,s=0,f[50];
f[0]=1;
f[1]=1;
for (i=2;f[i]<4000000;i++){
f[i] = f[i-1] + f[i-2];
if (f[i]%2 == 0){
printf("%ld ",f[i]);
s += f[i];
}
}
printf("SUM = %ld\n",s);
return 0;
}
Upvotes: -1
Reputation: 4206
The problem is likely here: (Anyway this is a big problem even if it is not the problem.)
int fib_array[]={};
The space allocated for your array in memory will not grow dynamically as you appear to expect. You need to manage the memory for it somehow. Right now you are overflowing this array substantially and it is amazing that this program does not crash or segfault.
Edit: Moreover, every time your while
loop checks its condition for whether to run again, it accesses an entry in your array which has not been initialized! Note:
fib_array[2] < 4000000
, which you are about to set in the body of the loop!fib_array[3] < 4000000
, which you are about to set in the body of the loop!Edit 2: Since (amazingly many) people have been posting that you need to use a 64 bit integer and that this is the source of your problems, I'd like to make the clarifying remark that the answer is in the 5 million range, so a 32 bit integer is plenty big.
Upvotes: 5
Reputation: 15199
fib_array[i] is not initialized when you access it in the while() test. Try changing your while loop to while(true)
and using if (fib_array[i]<4000000) break;
on the line after you set fib_array[i].
Also, your sum_of_Even is going to need to be a 64 bit integer, so you'll need:
#include <stdint.h>
and then declare it as uint64_t
.
Another problem is you aren't declaring how large your fib_array should be, so no actual memory is being allocated to it. Try int fib_array[MAXSIZE];
where MAXSIZE is your calculation for how many entries it will need.
Upvotes: 0