comp.lang.ada
 help / color / mirror / Atom feed
* if statements
@ 2000-11-03 14:42 sc297
  2000-11-03  0:00 ` David Starner
                   ` (4 more replies)
  0 siblings, 5 replies; 168+ messages in thread
From: sc297 @ 2000-11-03 14:42 UTC (permalink / raw)


can anyone show me an if statement that will swap the values of A and B,
if A is the greater value




^ permalink raw reply	[flat|nested] 168+ messages in thread
* Re: if statements
@ 2000-11-09  0:00 James Hassett
  2000-11-10  0:02 ` Robert Dewar
  0 siblings, 1 reply; 168+ messages in thread
From: James Hassett @ 2000-11-09  0:00 UTC (permalink / raw)
  To: comp.lang.ada

Ken Garlington <Ken.Garlington@computer.org> wrote:
> "Robert Dewar" <robert_dewar@my-deja.com> wrote in message
> news:8udce8$1qi$1@nnrp1.deja.com...
> 
> : I would write the above as:
> :
> :    procedure Conditional_Swap (A, B : in out Integer) is
> :    begin
> :       if A < B then
> :          declare
> :             Temp : constant Integer := A;
> :          begin
> :             A := B;
> :             B := Temp;
> :          end;
> :       end if;
> :    end Conditional_Swap;
> 
> This causes the *smaller* value to end up in A and the *larger* value to end
> up in B? Now I REALLY feel like a beginning student!
> 
> Here's the problem statement as I understand it:
> 
> : >   "Exercise 4.1: Write an if statement which will swap the values of
> : >    variables A and B if necessary so that the smaller value ends up in
> : >    A and the larger value ends up in B."
> 
> I always thought that a test such as "A < B" was true only if A was
> *already* smaller than B (in which case you shouldn't do a swap, right?).

This defect has been carried forward in a surprising number of
posts, so I'm glad Ken caught it.  I believe it originated with
Daniel Allex, who wrote

> procedure Swap ( A,    B : in out Integer ) is
>      Temp : Integer := 0;
>    begin
>      if A <= B then
>          Temp := A;
>          A := B;
>          B := Temp;
>      end if;
>    end;

When I saw this "solution" offered, I thought maybe Daniel had
deliberately introduced the bug to ensure that if the student
simply handed in the offered solution, it would be wrong, but if he
bothered to try to understand it, he would likely find and correct
the error.  I thought about posting a correction, but decided that
maybe the correction was best left as an exercise for the student.

I was surprised that Robert didn't catch this defect, but the
discussion has drifted far from the original problem posed, and
it is easy to lose sight of such (critical) details as which way
the test ought to go.  We rarely need to forgive Robert for
technical errors, so I'm willing to cut him some slack here.

Another problem (as noted by some already) is that the original
problem asked for an if statement, not a procedure, so we would
need to trim Robert's solution down to the if statement.  (The
solution offered by Daniel doesn't trim down as neatly, because
the Temp declaration gets lost.)

Otherwise, I certainly agree with Robert's critique of Daniel's
solution.

- Jim Hassett





^ permalink raw reply	[flat|nested] 168+ messages in thread
* Re: if statements
@ 2000-11-14  0:00 Duncan Sands
  2000-11-14  0:00 ` Ken Garlington
  0 siblings, 1 reply; 168+ messages in thread
From: Duncan Sands @ 2000-11-14  0:00 UTC (permalink / raw)
  To: comp.lang.ada

David Starner (speaking for IBM) writes:
>> On Fri, 03 Nov 2000 14:42:28 +0000, sc297 wrote:
>>>can anyone show me an if statement that will swap the values of A and B,
>>>if A is the greater value
>>
>> Yes. I can, and I bet your teacher can too.
>>
>Over in comp.arch they treat 'homework' questions a little differently
>than here. It seems to be an invitation to come up with the most
>reasonable, yet outlandish answers possible. Makes for fun reading.

That is an irresponsable attitude, and we should all blow our noses
at it.  We should instead show how the use of ADA allows us to write
a solution in a way that naturally reflects the problem domain.  Here
is a code snippet using the Beer oriented methodology (which naturally
reflects the life style of many students) that swaps the values of two
integers A and B if A is the greater value.  It uses an if statement.

if True then
   declare
      type Beer is new Integer;

      type Beer_Mug is record
         Refreshing_Fluid : Beer;
      end record;

      procedure Put_Beer_Into_Mug (
        Fluid : in     Beer;
        Mug   : in out Beer_Mug
      ) is
      begin
         Mug.Refreshing_Fluid := Fluid;
      end;

      function Make_Beer (X : Integer) return Beer is
      begin
         return Beer (X);
      end;

      procedure Burp_At_Wimpy_C_Programmers is
         task type Int_Swapper (X, Y : access Integer);
         task body Int_Swapper is
            type Int_Pointer is access all Integer;
            Z_Value : aliased Integer;
            Z : Int_Pointer := Z_Value'Access;
         begin
            Z.all := X.all;
            X.all := Y.all;
            Y.all := Z.all;
            A := X.all;
            B := Y.all;
         end;
         Swapper : Int_Swapper (new Integer' (A), new Integer' (B));
      begin
         null;
      end;

      procedure Drink_Beer (Fluid : in out Beer) is
      begin
         Fluid := 0;
         Burp_At_Wimpy_C_Programmers;
      end;

      type Hand is (Left_Hand, Right_Hand);

      Mugs : array (Hand) of Beer_Mug;

      Right_Hand_Mug : Beer_Mug renames Mugs (Left_Hand);
      Left_Hand_Mug  : Beer_Mug renames Mugs (Right_Hand);

      type Table is record
         Mug : Beer_Mug;
      end record;

      A_Convenient_Table : Table;

      procedure Move_Mug (From, To : Hand) is
      begin
         if From = To or Mugs (From).Refreshing_Fluid = -1 then
            raise Tasking_Error;
         else
            Mugs (To) := Mugs (From);
            Mugs (From).Refreshing_Fluid := -1; -- Null mug
         end if;
      end;

      procedure Move_Mug_To_Table (Member : Hand) is
      begin
         A_Convenient_Table.Mug := Mugs (Member);
         Mugs (Member).Refreshing_Fluid := -1;
      end;

      procedure Pick_Up_Mug (Member : Hand) is
      begin
         Mugs (Member) := A_Convenient_Table.Mug;
         A_Convenient_Table.Mug.Refreshing_Fluid := -1;
      end Pick_Up_Mug;

      procedure Pour_Beer (
        From : in out Beer_Mug;
        To   :    out Integer
      ) is
      begin
         To := Integer (From.Refreshing_Fluid);
         From.Refreshing_Fluid := 0;
      end;

      function Weight (Member : Hand) return Float is
         Density_Of_Beer : constant Float := 3.799237428472040235723029;
         Weight_Of_Hand  : constant Float := 2.369000038490274207765490;
      begin
         return Density_Of_Beer * Float (Mugs (Member).Refreshing_Fluid) +
           Weight_Of_Hand;
      end;
      
      subtype Excess is Float range 0.0 .. Float'Last;

      Excess_Weight : Excess;
   begin
      Put_Beer_Into_Mug (
        Fluid => Make_Beer (A),
        Mug   => Left_Hand_Mug
      );

      Put_Beer_Into_Mug (
        Fluid => Make_Beer (B),
        Mug   => Right_Hand_Mug
      );
      
      -- Round to one decimal place to compensate for experimental error
      -- in weighing the beer
      Excess_Weight := Float (
        Integer (100.0 * (Weight (Left_Hand) - Weight (Right_Hand)))
      ) / 100.0;
      Drink_Beer (Left_Hand_Mug.Refreshing_Fluid);
      Drink_Beer (Right_Hand_Mug.Refreshing_Fluid);
   exception
      when Constraint_Error => -- Beer off?  Swap mugs and try again
         Move_Mug_To_Table (Left_Hand);
         Move_Mug (
           From => Right_Hand,
           To   => Left_Hand
         );
         Pick_Up_Mug (Right_Hand);
         Excess_Weight := Float (
           Integer (100.0 * (Weight (Left_Hand) - Weight (Right_Hand)))
         ) / 100.0;
         Pour_Beer (
           From => Mugs (Right_Hand),
           To   => A
         );
         Pour_Beer (
           From => Mugs (Left_Hand),
           To   => B
         );
   end;
else
   -- Memory corruption due to solar radiation
   raise Storage_Error;
end if;





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

end of thread, other threads:[~2000-12-19 16:36 UTC | newest]

Thread overview: 168+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-03 14:42 if statements sc297
2000-11-03  0:00 ` David Starner
2000-11-07  0:00   ` John English
2000-11-13  0:00   ` Dale Pontius
2000-11-03 16:09 ` Larry Kilgallen
2000-11-03  0:00   ` tmoran
2000-11-03  0:00     ` Larry Kilgallen
2000-11-06  0:00       ` Brian Orpin
2000-11-03  0:00     ` David Starner
2000-11-05  0:00       ` tmoran
2000-11-06  0:00         ` David Starner
2000-11-08  0:14           ` Alejandro R. Mosteo
2000-11-08  0:00             ` Alejandro Villanueva
2000-11-08  0:00               ` Georg Bauhaus
2000-11-09  3:50               ` Ken Garlington
2000-11-07  0:00         ` John English
2000-11-07  0:00     ` John English
2000-11-08  5:22       ` Ken Garlington
2000-11-06  0:00 ` Daniel Allex
2000-11-07  0:00   ` John English
2000-11-07  0:00   ` Ken Garlington
2000-11-07  0:00     ` John English
2000-11-07  0:00       ` Robert Dewar
2000-11-08  0:00         ` John English
2000-11-08  0:00           ` Georg Bauhaus
2000-11-09  4:58           ` Robert Dewar
2000-11-09  0:00             ` Ken Garlington
2000-11-09  0:00               ` Designing test suites (was: if statements) Larry Kilgallen
2000-11-09  0:00             ` if statements John English
2000-11-08  5:29       ` Ken Garlington
2000-11-08  0:00         ` John English
2000-11-08  0:00           ` Ken Garlington
2000-11-07  0:00   ` Wes Groleau
2000-11-08  0:00     ` John English
2000-11-08  0:00       ` Wes Groleau
2000-11-09  3:43         ` Ken Garlington
2000-11-07  0:00   ` dmitry6243
2000-11-07  0:00     ` Jean-Pierre Rosen
2000-11-08  0:00       ` dmitry6243
2000-11-07  0:00   ` Robert Dewar
2000-11-07  0:00     ` Larry Kilgallen
2000-11-08  0:00     ` Ada student homework ? Nicolas Brunot
2000-11-08  0:00       ` Robert Dewar
2000-11-08  0:00         ` Nicolas Brunot
2000-11-08  0:00           ` Ted Dennison
2000-11-08  0:00             ` Nicolas Brunot
2000-11-08  0:00               ` Jerry Petrey
2000-11-08  0:00             ` Lao Xiao Hai
2000-11-09  0:00             ` Marin David Condic
2000-11-10  2:15               ` Ken Garlington
2000-11-10  0:00                 ` Marin David Condic
2000-11-18  0:00               ` John Magness
2000-11-19  0:00                 ` Robert Dewar
2000-11-19  0:00                   ` Larry Kilgallen
2000-11-20  0:00                 ` John English
     [not found]                 ` <3A2827A9.B54C260@ebox.tninet.se>
     [not found]                   ` <1dtW5.25958$6W1.1458704@news.flash.net>
2000-12-12 20:52                     ` Stefan Skoglund
     [not found]                       ` <dKo%5.144$PE5.16970@read2.inet.fi>
2000-12-19 16:36                         ` election chit-chat (was Re: Ada student homework ?) Robert Dewar
2000-11-08  0:00           ` Ada student homework ? Larry Kilgallen
2000-11-08  0:00             ` Nicolas Brunot
2000-11-14  0:00               ` Ronald Cole
2000-11-08  0:00       ` Larry Kilgallen
2000-11-08  0:00         ` Nicolas Brunot
2000-11-08  0:00           ` Lao Xiao Hai
2000-11-09  0:00             ` Nicolas Brunot
2000-11-09  0:00               ` Lao Xiao Hai
2000-11-10  0:00                 ` Nicolas Brunot
2000-11-10  0:00                   ` Larry Kilgallen
2000-11-10  0:00                     ` Nicolas Brunot
2000-11-10  0:00                       ` Dan Nagle
2000-11-10  0:00                         ` Nicolas Brunot
2000-11-10  0:00                           ` Larry Kilgallen
2000-11-10  0:00                             ` Nicolas Brunot
2000-11-11  0:00                               ` Robert Dewar
2000-11-12  6:54                                 ` Brian Rogoff
2000-11-12  0:00                                   ` Robert Dewar
2000-11-11  0:00                           ` Robert Dewar
2000-11-13  0:00                             ` Nicolas Brunot
2000-11-13  0:00                               ` Robert Dewar
2000-11-13  0:00                                 ` gdemont
2000-11-13  0:00                                 ` Ken Garlington
2000-11-13  0:00                                 ` Nicolas Brunot
2000-11-13  0:00                                   ` Pascal Obry
2000-11-13  0:00                                 ` gdemont
2000-11-13  0:00                                   ` Ada student homework ?[OS/2 Comments] tjerick
2000-11-14  0:00                                   ` Ada student homework ? Georg Bauhaus
2000-11-14  5:16                                   ` Robert Dewar
2000-11-14  0:00                                     ` John English
2000-11-14  0:00                                       ` gdemont
2000-11-14  0:00                                       ` Ole-Hjalmar Kristensen
2000-11-14  0:00                                     ` Nicolas Brunot
2000-11-15  0:00                                       ` Robert Dewar
2000-11-15  0:00                                         ` Ada student homework ? I give up Nicolas Brunot
2000-11-15  0:00                                           ` Robert Dewar
2000-11-14  0:00                                 ` Ada student homework ? dmitry6243
2000-11-15  0:00                             ` Larry Kilgallen
     [not found]                             ` <3A0FCBAD.824095C6@cadwin.cOrganization: LJK Software <5E93Ivxu6GwO@eisner.decus.org>
2000-11-18  0:00                               ` Robert Dewar
2000-11-20  0:00                     ` Wes Groleau
2000-11-20  0:00                       ` Larry Kilgallen
2000-11-10  0:00                 ` mjsilva
2000-11-10  0:00                 ` Nicolas Brunot
2000-11-10  0:00                   ` Marin David Condic
2000-11-10  0:00                     ` Randy Brukardt
2000-11-11  0:00                       ` Marin David Condic
2000-11-15  0:00                         ` Lao Xiao Hai
2000-11-18  0:00                           ` Robert Dewar
2000-11-18  0:00                             ` tmoran
2000-11-19  0:00                               ` Robert Dewar
2000-11-19  0:00                                 ` tmoran
2000-11-19  0:00                                   ` Robert Dewar
2000-11-20  0:16                                     ` tmoran
2000-11-20  0:00                                       ` Portability accross compilers Nicolas Brunot
2000-11-20  0:00                                         ` Robert Dewar
2000-11-20  3:18                                       ` Ada student homework ? Robert Dewar
2000-11-20  5:55                                         ` tmoran
2000-11-20  0:00                                           ` Robert Dewar
2000-11-20  0:00                                 ` Wes Groleau
2000-11-19  0:00                             ` Lao Xiao Hai
2000-11-10  0:00                     ` Ole-Hjalmar Kristensen
2000-11-10  0:00                     ` Nicolas Brunot
2000-11-10  0:00                       ` David Starner
2000-11-11  0:00                       ` Robert Dewar
2000-11-13  0:00                         ` Nicolas Brunot
2000-11-11  0:00                   ` Robert Dewar
2000-11-13  0:00                     ` gdemont
2000-11-08  0:00           ` Brian Rogoff
2000-11-09  0:00             ` Nicolas Brunot
2000-11-09  0:00               ` Robert Dewar
2000-11-10  0:08                 ` Ehud Lamm
2000-11-10  0:00               ` Brian Rogoff
2000-11-08  0:00           ` Chad R. Meiners
2000-11-08  0:00             ` Ted Dennison
2000-11-09  5:11               ` Robert Dewar
2000-11-18  0:00               ` John Magness
2000-11-09  5:04           ` Robert Dewar
2000-11-10  0:27             ` Subsetting Ada Ted Dennison
2000-11-08  0:00     ` if statements Alejandro Villanueva
2000-11-08  0:00       ` Daniel Allex
2000-11-09  3:54       ` Ken Garlington
2000-11-09  5:24       ` Robert Dewar
2000-11-09  0:00         ` constants v variables (was Re: if statements) Martin Dowie
2000-11-09  0:00           ` Robert Dewar
2000-11-09  0:00           ` Robert A Duff
2000-11-09  0:00             ` Florian Weimer
2000-11-10  2:13               ` Ken Garlington
2000-11-10  5:59                 ` Brian Rogoff
2000-11-09  0:00             ` Martin Dowie
2000-11-09  0:00             ` Robert Dewar
2000-11-09  0:00         ` if statements Ken Garlington
2000-11-09  0:00           ` Robert Dewar
2000-11-10  0:12             ` Ehud Lamm
2000-11-10  2:09             ` Ken Garlington
2000-11-20  0:00               ` Wes Groleau
2000-11-20  0:00             ` Wes Groleau
2000-11-21  0:00               ` Robert Dewar
2000-11-22  0:00                 ` Wes Groleau
2000-11-09  0:00         ` Marin David Condic
2000-11-09  0:00           ` Robert Dewar
2000-11-10  0:00             ` Larry Kilgallen
2000-11-10  1:58           ` Ken Garlington
2000-11-10  3:53             ` Robert Dewar
2000-11-07  0:00 ` John English
2000-11-07  0:00   ` Robert Dewar
2000-11-10  6:09 ` DJack143
2000-11-11  0:00   ` Here we go again (Was: if statements) Frank Christiny
  -- strict thread matches above, loose matches on Subject: below --
2000-11-09  0:00 if statements James Hassett
2000-11-10  0:02 ` Robert Dewar
2000-11-14  0:00 Duncan Sands
2000-11-14  0:00 ` Ken Garlington

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