Thread: Iteration help please

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Iteration help please

    // Listing 7.15
    // Demonstrates solving the nth
    // Fibonacci number using iteration

    #include <iostream>

    int fib(int position);
    int main()
    {
    using namespace std;
    int answer, position;
    cout << "Which position? ";
    cin >> position;
    cout << "\n";

    answer = fib(position);
    cout << answer << " is the ";
    cout << position << "th Fibonacci number.\n";
    return 0;
    }

    int fib(int n)
    {
    int minusTwo=1, minusOne=1, answer=2;

    if (n < 3)
    return 1;

    for (n -= 3; n; n--)
    {
    minusTwo = minusOne;
    minusOne = answer;
    answer = minusOne + minusTwo;
    }

    return answer;
    }

    /* I need with this problem because I am not really sure why n is being decreased or what the minusTwo=1 and so on mean. I do know that what this problem is trying to accomplish is to find the Fibonacci number, which is 1,1,2,3,5. the nth number being the sum of the two previous number except the first two numbers of the sequence, here being 1 and 1. */

    the book says


    /* This is exactly how you would solve this problem with pencil and paper. If you were asked for the fifth Fibonacci number, you would write 1,1,2 and think, "two more to do". Youw would then add 2+1 and write, and think, "one more to find" Finally, you would write 3+2 and the answer would be 5. */



    // I just don't quite understand it however.



    //please help.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's just plain wierd. I don't know where you got that code, but it's very difficult to follow. This would be a better implementation of the fibonacci series.
    Code:
    long fib(int n){
        long current, prev, prev_prev;
    
        current = prev = 1; /*assign 1 to current and prev, first iteration*/
      
        while(n > 2){ /*if n < 2 there's no need, the answer is 1*/
            n -= 1; /*current = 1, so the first iteration is done*/
            prev_prev = prev;
            prev = current;
            current = prev + prev_prev;
        }
        return current;
    }
    Much easier to follow even without the comments

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    You don't seem to be looking for the recursive version, but if you're by any chance noy familiar with it, this may help you understand the iterative one:

    double fib(int n) {
    if (n<2) return 1;
    else return fib(n-1)+fib(n-2);
    }
    int main() {
    for (int i=0; i<10;i++){
    cout <<i<<": "<<fib(i)<<endl;
    }
    return 0;
    }
    Regards, Al

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The recursive version is easier, but I don't recommend using it for anything but describing recursion. The save in complexity isn't worth the horrible decrease in efficiency.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  2. iteration revisited
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 09-05-2007, 09:38 AM
  3. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  4. recursivity & iteration
    By Marlon in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 01:58 PM
  5. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 06:19 PM