Thread: Simple program not working, don't know why

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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.
    Last edited by esbo; 01-29-2009 at 12:59 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by esbo
    Actualy current =2 so maybe not.
    current is never equal to 2
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Quote Originally Posted by MK27 View Post
    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!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Quote Originally Posted by laserlight View Post
    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!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Bakster View Post
    Ah yes, I see the problem now. Removing the '+' from '+=' on line 10 fixed it. Thanks a lot!
    Ha ha well spotted, it's always hard to find bugs in programs because they always do *exactly* what you tell them to do!!

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Quote Originally Posted by laserlight View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM