Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

while loop for fibonacci series

fibonacci loop

  • Please log in to reply
14 replies to this topic

#1 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 20 May 2011 - 01:10 PM

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
// 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( ([COLOR="red"]next_to_last + last[/COLOR]) < 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
// 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( [COLOR="red"]next_to_last < limit / 2[/COLOR] )   //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;
}

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 May 2011 - 01:17 PM

In Code 1, you have a pretty good chance of having the addition wrap into negative, causing it to incorrectly be "true".
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 20 May 2011 - 01:25 PM

Thanks, WingedPanther.

How could I modify the CODE 1 to make it work? Please let me know. I don't understand and like the while condition in CODE 2!
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 May 2011 - 03:15 PM

Here's the problem, if unsigned long limit truly is 4294967295 on your architecture, then 4294967294 + 2 = 1, which is clearly less than limit.

You really need something like CODE 2, which is simply checking to make sure you remain below half-way to limit.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 21 May 2011 - 12:42 PM

Here is another reason justifying condition 2. Look at it again.

next_to_last < limit / 2

Next to last is the 2nd last number, so based upon fibonacci rule, last would be greater than it. Now if you sum them both they sure would go beyond limit.

Hence if next to last is less than half of limit, last would sure be more than half and their sum would exceed limit.

For e.g. if limit was 4, next to last is 2 and last is 3, then limit /2 would be 4/2 = 2, so next to last shouldn't go beyond 2 which is correct.
  • 0

#6 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 21 May 2011 - 01:05 PM

Here's the problem, if unsigned long limit truly is 4294967295 on your architecture, then 4294967294 + 2 = 1, which is clearly less than limit.


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


Thanks a lot, WingedPanther, Fayyaz.

That means when the answer exceed the maximum available value of an unsigned variable which is 4294967295 in this case, it starts back again at 0. What does happen in case of signed int variable? Where does it start? At 0 or maximum negative value allowed? Please let me now. Thank you.

What do you make of that warning?

Please help me with the above queries. Thanks.
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#7 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 21 May 2011 - 01:33 PM

I think that adding a 'UL' would help with the warning:
const unsigned long limit = 4294967295UL; //largest unsigned long

We can add these prefixes with values to enforce their type (UL means unsigned long).

And yes adding one to max positive value starts off with the largest negative value.
  • 0

#8 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 21 May 2011 - 01:53 PM

I think that adding a 'UL' would help with the warning:

const unsigned long limit = 4294967295UL; //largest unsigned long

We can add these prefixes with values to enforce their type (UL means unsigned long).

And yes adding one to max positive value starts off with the largest negative value.


Thank you.

It did work. I didn't get any warning after adding the "UL". Can I use "unsigned long long limit" to expand the available range? Please let me know. Thanks.

In the following code 'number' is declared float. Strictly speaking, is the user supposed to enter every number in the format "xx.xx"? I mean even when the user intends to enter an 'integer' she/he is to enter the integer as "integer.0". Do you get my question?

float number;
cout << "enter the number: ";
cin >> number;

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9 fkl

fkl

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 21 May 2011 - 02:05 PM

Thank you.

It did work. I didn't get any warning after adding the "UL". Can I use "unsigned long long limit" to expand the available range? Please let me know. Thanks.


You are most welcome.
Prefixes such as U and UL etc. only appear with constants. So if i understand correctly, to use it with variable "limit" used above, you need to cast that to unsigned long long i.e.

(unsigned long long)limit;

Also do remember that you actually need to have a data type large enough to store a variable value. When we do addition such as a + b where a is an int and b is a long. The compiler converts a to long (converts all operands into the largest type of the two operands), then sums them both up. At that time the result is also a long variable. This is why i think casting "limit" would make the loop work correctly, since it will also cast the sum of 2nd last and last to long long before comparison, thereby giving correct result.

In the following code 'number' is declared float. Strictly speaking, is the user supposed to enter every number in the format "xx.xx"? I mean even when the user intends to enter an 'integer' she/he is to enter the integer as "integer.0". Do you get my question?

float number;
cout << "enter the number: ";
cin >> number;


Yes - you can enter just a 4 when cin is expecting a float (it will automatically make it 4.0)
  • 0

#10 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 21 May 2011 - 02:51 PM

Thanks a lot, Fayyaz.
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 21 May 2011 - 05:33 PM

I think that adding a 'UL' would help with the warning:

const unsigned long limit = 4294967295UL; //largest unsigned long

We can add these prefixes with values to enforce their type (UL means unsigned long).

And yes adding one to max positive value starts off with the largest negative value.

It rolls over to the largest negative.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#12 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 21 May 2011 - 05:53 PM

Thanks, WingedPanther.
  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)





Also tagged with one or more of these keywords: fibonacci, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download