# Simple program not working, don't know why

• 01-29-2009
Bakster
Simple program not working, don't know why
Hello everybody, I'm pretty much a beginner at using C, so my problem shouldn't be too difficult for you to solve, I hope. I wrote this program to sum all the even numbers in the fibonacci sequence below 4 million:

Code:

```#include <stdio.h> int main() {     int current = 0;     int prev = 1;     int prev2 = 0;     int sum = 0;     do {           current += (prev + prev2);           prev2 = prev;           prev = current;           if (current%2==0){             sum += current;             }                } while (current < 4000000);     printf("The answer is %d.", sum);     getchar();     return 0; }```
When I run this program, it returns "The answer is 0.", and I can't figure out why. Any ideas?
• 01-29-2009
laserlight
You might want to print out to check if you are actually generating it Fibonacci sequence. Alternatively, step through your code using a debugger and watch how the value of current increases, comparing it to what you know of the expected sequence.
• 01-29-2009
esbo
The first pass results in 0
On the second pass all the variables are 0 hence so is the result!

Actualy current =2 so maybe not.
• 01-29-2009
laserlight
Quote:

Originally Posted by esbo
Actualy current =2 so maybe not.

current is never equal to 2 ;)
• 01-29-2009
esbo
Yes I think I miss read that somewhere.
It seems the problem is the line for the sum is never executed otherwise it would be atleast 1
on the first pass?

Maybe making all the numbers floats would help?
• 01-29-2009
MK27
And current is never an even number either. Stick this in BEFORE the if at line 13:
Code:

`printf("current=%d prev=%d prev2=%d\n",current,prev,prev2);`
Whoops! This is not the fibonacci sequence at all!
• 01-29-2009
Bakster
Quote:

Originally Posted by MK27
And current is never an even number either. Stick this in BEFORE the if at line 13:
Code:

`printf("current=%d prev=%d prev2=%d\n",current,prev,prev2);`
Whoops! This is not the fibonacci sequence at all!

Yeah that would have been the smart thing to do. I'll get to work trying to fix it. Thanks!
• 01-29-2009
laserlight
Quote:

Originally Posted by esbo
It seems the problem is the line for the sum is never executed otherwise it would be atleast 1
on the first pass?

Yes, the statement to add to sum is never executed, but sum cannot be 1 since it must be an even number. The main problem is that Bakster is not generating the Fibonacci sequence, but rather a variant of it where the current number is twice of the previous plus the number just before the previous. Luckily, there is a simple fix involving the removal of a single character in the source code.
• 01-29-2009
Bakster
Quote:

Originally Posted by laserlight
Yes, the statement to add to sum is never executed, but sum cannot be 1 since it must be an even number. The main problem is that Bakster is not generating the Fibonacci sequence, but rather a variant of it where the current number is twice of the previous plus the number just before the previous. Luckily, there is a simple fix involving the removal of a single character in the source code.

Ah yes, I see the problem now. Removing the '+' from '+=' on line 10 fixed it. Thanks a lot! :D
• 01-29-2009
laserlight
You're welcome :)

However, note that you have an off by one error, though it might not actually surface. The problem is that you only perform the check for the boundary (4000000) after you compute the new current number and perform the check for whether current is an even number. This means that it is possible for current to exceed the boundary and yet be added to the total. It does not surface in this case because the last number generated is odd, but if you changed the boundary to say, 800000, you would get an incorrect result because of this.
• 01-29-2009
esbo
Quote:

Originally Posted by Bakster
Ah yes, I see the problem now. Removing the '+' from '+=' on line 10 fixed it. Thanks a lot! :D

Ha ha well spotted, it's always hard to find bugs in programs because they always do *exactly* what you tell them to do!!
• 01-29-2009
Bakster
Quote:

Originally Posted by laserlight
You're welcome :)

However, note that you have an off by one error, though it might not actually surface. The problem is that you only perform the check for the boundary (4000000) after you compute the new current number and perform the check for whether current is an even number. This means that it is possible for current to exceed the boundary and yet be added to the total. It does not surface in this case because the last number generated is odd, but if you changed the boundary to say, 800000, you would get an incorrect result because of this.

Yes I noticed that when I added the printf function MK27 posted, and edited my if condition:
Code:

`if (current%2==0 && current < 4000000)`
Quote:

Originally Posted by esbo
Ha ha well spotted, it's always hard to find bugs in programs because they always do *exactly* what you tell them to do!!

Didn't spot it by myself, hence the thread, but laserlight's explanation of what my attempt at generating a Fibonacci sequence actually did pointed me in the right direction :)