TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Classic View

Use Proportional Font
Show Text Part by Default
Condense Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Sender: "Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Date: Sat, 26 Jun 1999 02:52:11 +0200
Reply-To: Jerry van Dijk <[log in to unmask]>
From: Jerry van Dijk <[log in to unmask]>
Content-Transfer-Encoding: 7bit
In-Reply-To: <[log in to unmask]> from "W. Wesley Groleau x4923" at Jun 25, 99 04:30:06 pm
Content-Type: text/plain; charset=US-ASCII
MIME-Version: 1.0
Parts/Attachments: text/plain (47 lines)
W. Wesley Groleau x4923 wrote:

> > How do you swap two values of two seperate variables without declaring a
> >  temp variable?
>
> 1. Write a Stack package.
>
> 2. Push first variable on the stack.
>
> 3. Assign second variable to first variable.
>
> 4. Pop first variable off the stack.

Nah, that's cheating. If the variables are modular or unsigned, use the
the very old fashioned:

        with Ada.Text_IO;

        procedure Show_It is

           type DWORD is mod 2 ** 32;

           procedure Swap (A, B : in out DWORD) is
           begin
              A := A xor B;
              B := B xor A;
              A := A xor B;
           end Swap;

           A : DWORD :=  5;
           B : DWORD := 10;

        begin
           Ada.Text_IO.Put_Line (  "A =" & DWORD'Image (A) &
                                 ", B =" & DWORD'Image (B));
           Swap (A, B);
           Ada.Text_IO.Put_Line (  "A =" & DWORD'Image (A) &
                                 ", B =" & DWORD'Image (B));
        end Show_It;

:-) :-)

--
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | [log in to unmask]
-- see http://stad.dsl.nl/~jvandyk

ATOM RSS1 RSS2