i have this code
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBER 4000000
void fill_fib_nums(int fib_nums[], int num_elements);
int even_sum(int fib_nums[], int num_elements);
int main()
{
int answer, num_elements, fib_nums[33];
fib_nums[0] = 1;
fib_nums[1] = 1;
num_elements = sizeof(fib_nums) / sizeof(fib_nums[0]);
fill_fib_nums(fib_nums, num_elements);
answer = even_sum(fib_nums, num_elements);
printf("the some of the even numbers in the fibonachi sequence that are less than 4,000,000 is %d\n", answer);
return 0;
}
void fill_fib_nums(int fib_nums[], int num_elements)
{
int i, j, next_num = 2;
for (i = 0, j = 1; next_num < num_elements; i++, j++, next_num++)
{
fib_nums[next_num] = fib_nums[i] + fib_nums[j];//add the previous two numbers in the array together and store the result in fib_nums[next_num]
}
}
int even_sum(int fib_nums[], int num_elements)
{
int i, answer;
for (i = 0; i < num_elements; i++)
{
if (fib_nums[i] % 2 == 0) // run if fib_nums[i] is even
{
answer += fib_nums[i];
}
}
return answer;
}
the above doesn't work. however if i change the if statement on line 40 to
Code:
if (fib_nums[i] % 2 == 0)
it does.
i thought that
Code:
if(!some statement that equates to 0)
is the same as
Code:
if (some statement that equates to 0 == 0)
coop