Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7257

Re: code not working to the end

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: code not working to the end
Date Thu, 30 Jun 2011 11:16:38 +1200
Lines 98
Message-ID <971q3lFfpqU7@mid.individual.net> (permalink)
References <893ff82f-7f7c-4e2f-b99c-8d7231df90e2@t5g2000yqj.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net uhxT6cvlKM0S92XliepHcgCEeArGPwz3mqmAitpDQsGhaHvG0A
Cancel-Lock sha1:gHwsnBlCIkILWXI++YlQoGbEih4=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <893ff82f-7f7c-4e2f-b99c-8d7231df90e2@t5g2000yqj.googlegroups.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:7257

Show key headers only | View raw


On 06/30/11 11:03 AM, net user wrote:
> hi,
>
> Anything to improve for someone learning in the code below...

It would have been helpful to have posted the real code!

> /* 1- Compute a n*2 value for 64 iterations */
> /* 2- Maximum integer variable iterations for n*2 */
> /* 3- Computer the number of iteration needed to reach the value in
> question 1 */
> int main()
> try
> {
>          long double var_long_double = 1 ;
>          long double var_long_double2 = 1 ;
>          unsigned int var_integer = 1 ;
>          unsigned int var_integer2 = 1 ;
>          int i = 1 ;
>          int nb = 1 ;

Declaring these where they are used is better style.

>          // QUESTION 1
>          while (i<  64)

A for loop would have been clearer.

>          {
>                  var_long_double *= 2 ;
>                  i++ ;
>          }
>          cout<<  "\n1- Value for 64 iteration : "<<  var_long_double ;
>          //QUESTION 2
>          while (var_integer>  0)

A for loop would have been clearer.

>          {
>                  cout<<  "\n"<<  nb<<  "\tvar_int : "<<  var_integer ;

This outputs 2^(n-1)).

>                  var_integer *= 2 ;
>                  nb++ ;
>                  if (var_integer>  0)
>                          var_integer2 = var_integer ;
>          }
>          cout<<  "\n\n2- Number as integer variable for "<<  nb<<  "
> incrementations : "<<  var_integer2 ;
>          // QUESTION 3
>          i = 1 ;
>          while (var_long_double>= 0)

This will run for (almost) ever.
>          {
>                  var_long_double /= 2 ;

var_long_double will never become zero.

>                  if (var_long_double>= 0)
>                          var_long_double2 = var_long_double ;
>                  else
>                          break ;
>                  i++ ;
>          }
>          cout<<  "\n3-Number of maximum iteration for i as int "<<  i
> <<
> "\n" ;
>          cout<<  "\n\n" ;
>          system("pause") ;
>          return 0 ;
> }
>
> catch (runtime_error e)

Catching exceptions by value is not a good idea, catch by const reference.

> {
>          cout<<  e.what()<<  '\n' ;
>          keep_window_open("~") ;
> }
>
> catch(...)
> {
>          cout<<  "Exiting\n" ;
>          keep_window_open("~") ;
> }
>
> I am running this on a 64 bits computer using Windev.
> Above the fact that I don't see the code above very reliable, I don't
> understand why I get to -2 147 483 648 with an unsigned int variable.
>  From my understanding it should be 4 294 967 295...

Where do you get that value?

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

code not working to the end net user <netuser501@gmail.com> - 2011-06-29 16:03 -0700
  Re: code not working to the end Ian Collins <ian-news@hotmail.com> - 2011-06-30 11:16 +1200

csiph-web