Thread: Fibonacci sequence wont run.

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    9

    Fibonacci sequence wont run.

    This is the second problem from Project Euler. here the detail:
    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
    By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


    and here is my code in C:
    Code:
    #include <stdio.h>
    int main()
    {
        int sum = 0, a = 1, b = 2, c=0;
        while(b<4000000)
            {
                c=a+b;
                if ( c%2==0 )
                {
                    continue;
                    if (c<4000000)
                    {
                        sum=sum+c;
                    }
                }
                a = b;
                b = c;
                
            }
        sum=sum+1;
        printf ("%d", sum);
        return 0;
    }
    Compiling run without error, however when i run the program(.exe file) from command prompt, it do nothing. I dont know what to do next, since there is no error statement to look at.

    Here the attachment if it help.
    Attached Files Attached Files
    • File Type: c 1.c (280 Bytes, 145 views)

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you have a "continue;" statement?
    I asking because it will likely result in a endless loop.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You have an extraneous "continue" statement messing up your control flow. Get rid of that.

    You aren't summing in the value 2 (starting value of b).

    You have a strange extra increment of sum just before you print it.

    And "do not exceed four million" means b <= 4000000. (This would only make a difference if 4000000 happened to be a fib, but it's still the correct-to-the-letter code.)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    "Run" the program on paper. It will take almost no time to see what's going on. Note that "continue" means you want to restart the loop immediately.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    okay so here the new code i got:
    Code:
    #include <stdio.h>
    int main()
    {
    	int sum = 0, a = 0, b = 1, c = 0;
    	while(b<=4000000)
    		{
    			c=a+b;
    			if ( (c%2==1) && (c<=4000000) )
    			{
    				sum=sum+c;
    			}
    			a = b;
    			b = c;
    			
    		}
    	printf ("%d", sum);
    	return 0;
    }
    it finally run.

    To everyone: since i want only even number, i try to tell the compiler to continue back to loop if c%2==0. I should have just use "!=" instead. and the increment of 1 at the end was for the fact that i start a=1 , b=2; so 1 was not in the final sum. Now i start a=0 , b= 1 and the final sum is no longer needed.

    i still got the wrong result for the project, but that is for me to figure out the right algorithmic.
    thanks everyone for reply.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You may want to look into recursion

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    You may want to look into recursion
    An iterative solution is perfectly fine, and probably better, here.
    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

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    An iterative solution is perfectly fine, and probably better, here.
    The more I think about it, the more I agree with you

    Going through the numbers only once is a good thing.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    I got it!!!!! Woooohooooo
    they want the sum of even number, not odd number. so i change "c%2==1" to "c%2==0".
    and it works. Off to problem number 3.
    thanks everyone for replying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci sequence
    By paut in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2011, 12:27 PM
  2. Fibonacci Sequence
    By iGuardian in forum C++ Programming
    Replies: 19
    Last Post: 10-21-2011, 12:34 AM
  3. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 07:17 AM
  4. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  5. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 10:40 PM

Tags for this Thread