From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MAILING_LIST_MULTI autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b50bc6538a649497 X-Google-Attributes: gid103376,public From: James Hassett Subject: Re: if statements Date: 2000/11/09 Message-ID: <200011091641.KAA05352@alonzo.tds-eagan.lmco.com>#1/1 X-Deja-AN: 691726102 Content-Transfer-Encoding: 7BIT To: comp.lang.ada@ada.eu.org Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: usenet@enst.fr X-Sun-Charset: US-ASCII X-Trace: menuisier.enst.fr 973797130 27717 137.194.161.2 (9 Nov 2000 19:12:10 GMT) Organization: ENST, France List-Id: comp.lang.ada mail<->news gateway X-Mailman-Version: 2.0beta5 X-BeenThere: comp.lang.ada@ada.eu.org Mime-Version: 1.0 Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Date: 9 Nov 2000 19:12:10 GMT Newsgroups: comp.lang.ada Date: 2000-11-09T19:12:10+00:00 Ken Garlington wrote: > "Robert Dewar" 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