while loop for fibonacci series

This is a discussion on while loop for fibonacci series within the C++ Programming forums, part of the General Programming Boards category; Hi Which version of the two do you prefer? I prefer the "1" because the while condition seems more understandable ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    while loop for fibonacci series

    Hi

    Which version of the two do you prefer? I prefer the "1" because the while condition seems more understandable to me (don't really know if it's correct or not). Any suggestions.

    EDIT: There is some problem with CODE 1. It just keeps running.

    And I'm also getting this warning: warning: this decimal constant is unsigned only in ISO C90|

    What does this warning mean? Could you please tell me? Thanks.

    CODE 1
    Code:
    // fibo.cpp
    // demonstrates WHILE loops using fibonacci series
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
       const unsigned long limit = 4294967295; //largest unsigned long
       unsigned long next_to_last=0;       //next-to-last term
       unsigned long last=1;       //last term
    
       while( (next_to_last + last) < limit )   //don't let results get too big
          {
    
          cout << last << "  ";    //display last term
          long new_last = next_to_last + last;  //add last two terms
          next_to_last = last;             //variables move forward
          last = new_last;              //   in the series
    
          }
    
       cout << endl << endl;
    
       system("pause");
       return 0;
    }

    CODE 2
    Code:
    // fibo.cpp
    // demonstrates WHILE loops using fibonacci series
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
       const unsigned long limit = 4294967295; //largest unsigned long
       unsigned long next_to_last=0;       //next-to-last term
       unsigned long last=1;       //last term
    
       while( next_to_last < limit / 2 )   //don't let results get too big
          {
    
          cout << last << "  ";    //display last term
          long new_last = next_to_last + last;  //add last two terms
          next_to_last = last;             //variables move forward
          last = new_last;              //   in the series
    
          }
    
       cout << endl << endl;
    
       system("pause");
       return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You may prefer the test in #1, but as you have noted it doesn't work. When you overflow an unsigned variable, you start back over again at 0; so if next_to_last + last would (if done in "real math") go over the limit, the computer will be unable to detect that since next_to_last + last would in fact be 17 or whatever.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    You may prefer the test in #1, but as you have noted it doesn't work. When you overflow an unsigned variable, you start back over again at 0; so if next_to_last + last would (if done in "real math") go over the limit, the computer will be unable to detect that since next_to_last + last would in fact be 17 or whatever.
    Thanks a lot, tabstop. I didn't know that. What does happen in case of signed variable? Where does it start? At 0 or maximum negative value allowed? Please let me now. Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jackson6612 View Post
    Thanks a lot, tabstop. I didn't know that. What does happen in case of signed variable? Where does it start? At 0 or maximum negative value allowed? Please let me now. Thank you.
    No one knows. Or more precisely, you have to know how your system does signed integers to know what it does when you overflow.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Okay. Anyway, thanks. But what does that warning mean? Would you please tell me? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ Program to find the sum of the Fibonacci Series
    By newbie09 in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2011, 12:55 AM
  2. Fractions in fibonacci series
    By Jamkirk in forum C Programming
    Replies: 4
    Last Post: 01-10-2008, 01:01 PM
  3. Fibonacci series using recursion
    By IPCHU in forum C Programming
    Replies: 1
    Last Post: 12-07-2006, 06:05 AM
  4. fibonacci problem using while loop
    By galmca in forum C Programming
    Replies: 6
    Last Post: 10-02-2004, 06:12 AM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 12:54 AM

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