Thread: My Ansi C Fibonacci code stinks! obvious bug?

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    My Ansi C Fibonacci code stinks! obvious bug?

    I know I have an obvious bug -- well, a calculation error somewhere. Doing a quick fibonacci code sample, only requirements is NOT to use arrays and NOT to do it recursively.

    This is what I came up with, and its obviously wrong!

    Code:
    int fibonacci(int userinput)
    
    {
    
    int result = 0;
    int counter = 0;
    
    if (userinput == 0) return result = 0;
           else if (userinput == 1) return result = 1;
                 else		
                      for (counter = userinput; counter > 1; counter--)
                            {
                              result += ((counter - 1) + (counter - 2));
                            }
    return (result);
    }
    I am sure the "bad guy" is the line result += (...) but what is wrong?

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    21
    This version actually works, but how can I improve it?
    Code:
    int fibonacci(int userinput)
    { 
    
                if (userinput < 2) 
                    return userinput; 
    
                else  
                { 
                    int n1 = 1, n2 = 1, tmp = 0; 
    
                    for (int i = 2; i < userinput; i++) 
                    { 
                        tmp = n2; 
                        n2 = n1 + n2; 
                        n1 = tmp; 
                    } 
    
                    return n2; 
                } 
            }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Hansie View Post
    This version actually works, but how can I improve it?
    Code:
    int fibonacci(int userinput)
    { 
    
                if (userinput < 2) 
                    return userinput; 
    
                else  
                { 
                    int n1 = 1, n2 = 1, tmp = 0; 
    
                    for (int i = 2; i < userinput; i++) 
                    { 
                        tmp = n2; 
                        n2 = n1 + n2; 
                        n1 = tmp; 
                    } 
    
                    return n2; 
                } 
            }

    you can name your vars in more readable way and add comments explaining what is going on for one who does not know the algorithm...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Fibonacci sequence - isn't that the sequence 1, 1, 2, 3, 5, 8, 13, 21, etc.? If so, then what you need are two numbers that form the starting point. For the next number, you add the previous 2 numbers. It continues like that. Study the pattern and write the program for it that follows the pattern. I can see that you apparently have this in your code, but comments and better variable names would be helpful. The output could be named "output", the previous number as "PrevNumber" and the one before it "Prev2Number" (use whatever, but something more clear than "n1" and "n2").
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You should refer to the first 3 'elements' of the Fibonacci sequence as 'a', 'b' & 'c'. Since that makes sense, c = a + b, where a = 1 (first iteration) and b = 2 (first iteration).

    Then on the Nth iteration, a = b, b = c, c = a+b

    Such as, 1 (a), 2 (a,b), 3(a,b,c), 5 (a,b,c), 8 (a,b,c), etc

    And there is a mathematical way to get the Nth element of the Fibonacci sequence using a formula, rather than using a bruteforce method.
    Last edited by zacs7; 05-23-2007 at 12:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM