Like Tree1Likes
  • 1 Post By robinnbastar

Fibonacci sequence wont run.

This is a discussion on Fibonacci sequence wont run. within the C Programming forums, part of the General Programming Boards category; This is the second problem from Project Euler. here the detail: Each new term in the Fibonacci sequence is generated ...

  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, 45 views)

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

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    "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,385
    You may want to look into recursion

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by Click_here
    You may want to look into recursion
    An iterative solution is perfectly fine, and probably better, here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,385
    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.
    iMalc likes this.

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, 11:27 AM
  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, 08: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21