comp.lang.ada
 help / color / mirror / Atom feed
From: James Hassett <hassett@planet8.tds-eagan.lmco.com>
To: comp.lang.ada@ada.eu.org
Subject: Re: if statements
Date: 2000/11/09
Date: 2000-11-09T19:12:10+00:00	[thread overview]
Message-ID: <200011091641.KAA05352@alonzo.tds-eagan.lmco.com> (raw)

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





             reply	other threads:[~2000-11-09  0:00 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-09  0:00 James Hassett [this message]
2000-11-10  0:02 ` if statements Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
2000-11-14  0:00 Duncan Sands
2000-11-14  0:00 ` Ken Garlington
2000-11-03 14:42 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     ` 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-03  0:00     ` Larry Kilgallen
2000-11-06  0:00       ` Brian Orpin
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   ` 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     ` 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         ` 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   ` 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             ` 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 ` John English
2000-11-07  0:00   ` Robert Dewar
2000-11-10  6:09 ` DJack143
replies disabled

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