comp.lang.ada
 help / color / mirror / Atom feed
* do while loop help ( newbie)
@ 2006-02-04 23:16 isaac2004
  2006-02-05  2:01 ` jimmaureenrogers
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: isaac2004 @ 2006-02-04 23:16 UTC (permalink / raw)


hi im new to ada and im having trouble with executing a do while loop.
im trying to take an input number called Number and sort it into
ascending and descending order and subtract the two to get a new number
called NewNumber. i want the loop to continue looping until variable
Number is equal to variable NewNumber. if this doesnt make sense here
is my code

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure Assignment2 is

-----------------------------------------------------------------------------------------------
   --| Recieves four variables and uses Kaprekar's Function to arrange
them into descending order and
   --| and subtract the number in ascending order from it
   --| Author: Isaac Levin,
   --| Last Modified: January 2006

----------------------------------------------------------------------------

   subtype Digit is Natural range 0 .. 9;

   D1        : Digit;                   -- a list of four single digit
numbers
   D2        : Digit;
   D3        : Digit;
   D4        : Digit;
   Number    : Natural range 0 .. 9999; -- number taken from user
   Temp      : Natural;
   AsNumber  : Natural;                 -- number in ascending order
   DsNumber  : Natural;                 -- number in descening order
   NewNumber : Natural;                 -- new number derived from
DsNumber - AsNumber



   -- procedure specifications
   procedure Order (
         X : in out Natural;
         Y : in out Natural);

   -- procedure body
   procedure Order (
         X : in out Natural;
         Y : in out Natural) is
      -- pre: X and Y are assigned values;
      -- post: X has the smaller value and Y has the larger value

      Temp : Natural; --copy of number originally in X

   begin  -- Order
      if X > Y then
         Temp := X ;       -- store X into temp
         X := Y ;          -- move old y into x
         Y := Temp ;       -- move old x from temp to y
      end if;
   end Order;
begin



   loop
      begin -- block for exception handler
         Put(Item => "Please enter a four digit number.");
         New_Line;
         Get(Item => Number);
         exit;


      exception
         when Constraint_Error =>
            Put ("The input value or result is out of range.");
            Skip_Line;
            New_Line;
         when Data_Error =>
            Put (Item => "The input value is not well formed.");
            Skip_Line;
            New_Line;
      end;       -- block for exception handler
   end loop;


   while Number /= NewNumber loop

      -- extract digits of number
      Temp := Number ;
   D1:= Temp rem 10 ;
   Temp := Temp / 10 ;
   D2:= Temp REM 10;
   Temp:= Temp / 10;
   D3:= Temp REM 10;
   Temp := Temp / 10;
   D4 := Temp REM 10;

      -- sort the digits of the number
      Order (
         X => D1,
         Y => D2);
      Order (
         X => D1,
         Y => D3);
      Order (
         X => D1,
         Y => D4);
      Order (
         X => D2,
         Y => D3);
      Order (
         X => D2,
         Y => D4);
      Order (
         X => D3,
         Y => D4);

      -- put ascending and descending digits back into numbers

      AsNumber := (D1 * 1000) + (D2 * 100) + (D3 * 10) + D4;
      DsNumber := (D4 * 1000) + (D3 * 100) + (D2 * 10) + D1;

      -- subtract ascending number from descending number
      NewNumber := DsNumber - AsNumber;


      -- display the results
      Put(
         Item  => Dsnumber,
         Width => 1);
      Put(Item => " - ");
      Put(
         Item  => Asnumber,
         Width => 1);
      Put(Item => " = ");
      Put(
         Item  => Newnumber,
         Width => 1);

     Number := NewNumber;

   end loop ;

end Assignment2;

it looks right to me but it just will not start the loop any help would
be great. thanx before hand




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-04 23:16 do while loop help ( newbie) isaac2004
@ 2006-02-05  2:01 ` jimmaureenrogers
  2006-02-05  8:03   ` Pascal Obry
  2006-02-05 12:18   ` Stephen Leake
  2006-02-05  2:07 ` Jeffrey R. Carter
       [not found] ` <bjfau11bp7g4b5id8vprbai6bc81qfgr96@4ax.com>
  2 siblings, 2 replies; 9+ messages in thread
From: jimmaureenrogers @ 2006-02-05  2:01 UTC (permalink / raw)


isaac2004 wrote:
> hi im new to ada and im having trouble with executing a do while loop.
> im trying to take an input number called Number and sort it into
> ascending and descending order and subtract the two to get a new number
> called NewNumber. i want the loop to continue looping until variable
> Number is equal to variable NewNumber. if this doesnt make sense here
> is my code
>
> with Ada.Text_IO;
> use Ada.Text_IO;
> with Ada.Integer_Text_IO;
> use Ada.Integer_Text_IO;
> procedure Assignment2 is
>
> -----------------------------------------------------------------------------------------------
>    --| Recieves four variables and uses Kaprekar's Function to arrange
> them into descending order and
>    --| and subtract the number in ascending order from it
>    --| Author: Isaac Levin,
>    --| Last Modified: January 2006
>
> ----------------------------------------------------------------------------
>
>    subtype Digit is Natural range 0 .. 9;
>
>    D1        : Digit;                   -- a list of four single digit
> numbers
>    D2        : Digit;
>    D3        : Digit;
>    D4        : Digit;
>    Number    : Natural range 0 .. 9999; -- number taken from user
>    Temp      : Natural;
>    AsNumber  : Natural;                 -- number in ascending order
>    DsNumber  : Natural;                 -- number in descening order
>    NewNumber : Natural;                 -- new number derived from
> DsNumber - AsNumber
>
>
>
>    -- procedure specifications
>    procedure Order (
>          X : in out Natural;
>          Y : in out Natural);
>
>    -- procedure body
>    procedure Order (
>          X : in out Natural;
>          Y : in out Natural) is
>       -- pre: X and Y are assigned values;
>       -- post: X has the smaller value and Y has the larger value
>
>       Temp : Natural; --copy of number originally in X
>
>    begin  -- Order
>       if X > Y then
>          Temp := X ;       -- store X into temp
>          X := Y ;          -- move old y into x
>          Y := Temp ;       -- move old x from temp to y
>       end if;
>    end Order;
> begin
>
>
>
>    loop
>       begin -- block for exception handler
>          Put(Item => "Please enter a four digit number.");
>          New_Line;
>          Get(Item => Number);
>          exit;
>
>
>       exception
>          when Constraint_Error =>
>             Put ("The input value or result is out of range.");
>             Skip_Line;
>             New_Line;
>          when Data_Error =>
>             Put (Item => "The input value is not well formed.");
>             Skip_Line;
>             New_Line;
>       end;       -- block for exception handler
>    end loop;
>

Your input routine will not yield the error checking you seem to
think it yields here. You define number as an instance of an
anonymous subtype of Natural. You then use the package
Ada.Integer_Text_IO for your input. That package knows nothing
about your anonymous subtype except that it is a subtype of
Inteter. It will not generat a Constraint_Error upon out of range
input.

You should declare a named subype and then instantiate the generic
package Ada.Text_IO.Integer_IO for that subtype.

subtype Four_Digits_Max is Natural range 0..9999;

package Num_IO is new Ada.Text_Io.Integer_IO(Four_Digits_Max);

Number : Four_Digits_Max;



>
>    while Number /= NewNumber loop

You should have gotten a warning from the compiler here that the value
of NewNumber is used before it is set. You never initialize NewNumber
to any known value before it is first evaluated upon entering the loop.

>
>       -- extract digits of number
>       Temp := Number ;
>    D1:= Temp rem 10 ;
>    Temp := Temp / 10 ;
>    D2:= Temp REM 10;
>    Temp:= Temp / 10;
>    D3:= Temp REM 10;
>    Temp := Temp / 10;
>    D4 := Temp REM 10;
>
>       -- sort the digits of the number
>       Order (
>          X => D1,
>          Y => D2);
>       Order (
>          X => D1,
>          Y => D3);
>       Order (
>          X => D1,
>          Y => D4);
>       Order (
>          X => D2,
>          Y => D3);
>       Order (
>          X => D2,
>          Y => D4);
>       Order (
>          X => D3,
>          Y => D4);
>
>       -- put ascending and descending digits back into numbers
>
>       AsNumber := (D1 * 1000) + (D2 * 100) + (D3 * 10) + D4;
>       DsNumber := (D4 * 1000) + (D3 * 100) + (D2 * 10) + D1;
>
>       -- subtract ascending number from descending number
>       NewNumber := DsNumber - AsNumber;
>
>
>       -- display the results
>       Put(
>          Item  => Dsnumber,
>          Width => 1);
>       Put(Item => " - ");
>       Put(
>          Item  => Asnumber,
>          Width => 1);
>       Put(Item => " = ");
>       Put(
>          Item  => Newnumber,
>          Width => 1);
>
>      Number := NewNumber;
>
>    end loop ;
>
> end Assignment2;
>
> it looks right to me but it just will not start the loop any help would
> be great. thanx before hand

Jim Rogers




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-04 23:16 do while loop help ( newbie) isaac2004
  2006-02-05  2:01 ` jimmaureenrogers
@ 2006-02-05  2:07 ` Jeffrey R. Carter
       [not found] ` <bjfau11bp7g4b5id8vprbai6bc81qfgr96@4ax.com>
  2 siblings, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2006-02-05  2:07 UTC (permalink / raw)


isaac2004 wrote:

>    while Number /= NewNumber loop

Newnumber is uninitialized the 1st time you get here.

>      Number := NewNumber;

Now Number = Newnumber, so the loop will exit after at most 1 time through the 
loop, so why have a loop at all?

-- 
Jeff Carter
"Strange women lying in ponds distributing swords
is no basis for a system of government."
Monty Python & the Holy Grail
66



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
       [not found] ` <bjfau11bp7g4b5id8vprbai6bc81qfgr96@4ax.com>
@ 2006-02-05  4:39   ` isaac2004
  2006-02-05 21:25     ` Björn Persson
  0 siblings, 1 reply; 9+ messages in thread
From: isaac2004 @ 2006-02-05  4:39 UTC (permalink / raw)


thanx for the help i understand about the exit when loop but is there a
way to do it that uses the for while loop instead because i can do it
with the exit when but i guess i didnt specify that earlier. thanx for
the code




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-05  2:01 ` jimmaureenrogers
@ 2006-02-05  8:03   ` Pascal Obry
  2006-02-05 12:18   ` Stephen Leake
  1 sibling, 0 replies; 9+ messages in thread
From: Pascal Obry @ 2006-02-05  8:03 UTC (permalink / raw)
  To: jimmaureenrogers@worldnet.att.net

jimmaureenrogers@worldnet.att.net a �crit :

> You should declare a named subype and then instantiate the generic
> package Ada.Text_IO.Integer_IO for that subtype.
> 
> subtype Four_Digits_Max is Natural range 0..9999;
> 
> package Num_IO is new Ada.Text_Io.Integer_IO(Four_Digits_Max);

No. Since it is a subtype just use Ada.Integer_Text_IO. No need to
instantiate anything.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-05  2:01 ` jimmaureenrogers
  2006-02-05  8:03   ` Pascal Obry
@ 2006-02-05 12:18   ` Stephen Leake
  1 sibling, 0 replies; 9+ messages in thread
From: Stephen Leake @ 2006-02-05 12:18 UTC (permalink / raw)


"jimmaureenrogers@worldnet.att.net" <jimmaureenrogers@worldnet.att.net> writes:

<snip>
>>    Number    : Natural range 0 .. 9999; -- number taken from user
<snip>

>>    loop
>>       begin -- block for exception handler
>>          Put(Item => "Please enter a four digit number.");
>>          New_Line;
>>          Get(Item => Number);
>>          exit;
>>
>>
>>       exception
>>          when Constraint_Error =>
>>             Put ("The input value or result is out of range.");
>>             Skip_Line;
>>             New_Line;
>>          when Data_Error =>
>>             Put (Item => "The input value is not well formed.");
>>             Skip_Line;
>>             New_Line;
>>       end;       -- block for exception handler
>>    end loop;
>
> Your input routine will not yield the error checking you seem to
> think it yields here. You define number as an instance of an
> anonymous subtype of Natural. You then use the package
> Ada.Integer_Text_IO for your input. That package knows nothing
> about your anonymous subtype except that it is a subtype of
> Inteter. It will not generat a Constraint_Error upon out of range
> input.

The range is checked when Get returns, as Item is copied into Number.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-05  4:39   ` isaac2004
@ 2006-02-05 21:25     ` Björn Persson
  2006-02-05 22:25       ` Jeffrey R. Carter
  0 siblings, 1 reply; 9+ messages in thread
From: Björn Persson @ 2006-02-05 21:25 UTC (permalink / raw)


isaac2004 wrote:
> thanx for the help i understand about the exit when loop but is there a
> way to do it that uses the for while loop instead because i can do it
> with the exit when but i guess i didnt specify that earlier. thanx for
> the code

The fact that NewNumber was uninitialized the first time the loop 
condition was evaluated is a hint that "while" might not be the best 
solution. You always want to do the calculation at least once, so 
there's no need to have a condition on entering the loop.

There are several ways to do it with "while" anyway. Here are a few:
� Introduce an extra variable "Previous_Number".
� Do the test in the best place and store the result in a boolean 
variable "Done".
� Enclose part of the loop body in an "if" statement so that it's 
skipped the first time.
� Duplicate part of the loop body outside the loop. This gets very ugly 
if more than one or two lines are duplicated.
� Turn part of the loop body into a procedure and call it both inside 
and outside the loop.

None of those are as clean as a loop-and-a-half with "exit when". What's 
wrong with "exit when"?

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-05 21:25     ` Björn Persson
@ 2006-02-05 22:25       ` Jeffrey R. Carter
  2006-02-06  1:01         ` Björn Persson
  0 siblings, 1 reply; 9+ messages in thread
From: Jeffrey R. Carter @ 2006-02-05 22:25 UTC (permalink / raw)


Bj�rn Persson wrote:

> None of those are as clean as a loop-and-a-half with "exit when". What's 
> wrong with "exit when"?

It's not even a loop and a half. It's a full loop with the test at the end.

At the Ada Launch, 1980 Dec 10, Ichbiah, Barnes, and Firth introduced their new 
language (now called Ada 80). They said "while" was included in the language 
only to facilitate automated translation from other languages. For new code, 
they recommended not using "while", since it often involves negative logic, and 
the condition (True to continue the loop) is opposite the intuitive condition 
(True to exit the loop).

-- 
Jeff Carter
"Pray that there's intelligent life somewhere up in
space, 'cause there's bugger all down here on earth."
Monty Python's Meaning of Life
61



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: do while loop help ( newbie)
  2006-02-05 22:25       ` Jeffrey R. Carter
@ 2006-02-06  1:01         ` Björn Persson
  0 siblings, 0 replies; 9+ messages in thread
From: Björn Persson @ 2006-02-06  1:01 UTC (permalink / raw)


Jeffrey R. Carter wrote:
> Bj�rn Persson wrote:
>> None of those are as clean as a loop-and-a-half with "exit when". 
>> What's wrong with "exit when"?
> 
> It's not even a loop and a half. It's a full loop with the test at the end.

Not at all. Sure, the test is very close to the end; there's just one 
assignment after it, but that's what makes all the difference.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-02-06  1:01 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-04 23:16 do while loop help ( newbie) isaac2004
2006-02-05  2:01 ` jimmaureenrogers
2006-02-05  8:03   ` Pascal Obry
2006-02-05 12:18   ` Stephen Leake
2006-02-05  2:07 ` Jeffrey R. Carter
     [not found] ` <bjfau11bp7g4b5id8vprbai6bc81qfgr96@4ax.com>
2006-02-05  4:39   ` isaac2004
2006-02-05 21:25     ` Björn Persson
2006-02-05 22:25       ` Jeffrey R. Carter
2006-02-06  1:01         ` Björn Persson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox