Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Swap values without temp
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
beard0




PostPosted: Tue Sep 20, 2005 10:55 pm   Post subject: Swap values without temp

I just thought it would be neat to try. Here are my results:
Turing:
proc swapString (var x, y : string)
    x := x + y
    y := x (1 .. length (x) - length (y))
    x := x (length (y) + 1 .. length (x))
end swapString

proc swapNum (var x, y : real)
    x := x + y
    y := x - y
    x := x - y
end swapNum


I started to do something similar for arrays, but realized that unless they had the same dimensions, they'd have to be flexible, and then by changing their size I'd be doing essentially the same thing as creating a temp.
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Wed Sep 21, 2005 8:02 am   Post subject: Re: Swap values without temp

beard0 wrote:
I started to do something similar for arrays, but realized that unless they had the same dimensions, they'd have to be flexible, and then by changing their size I'd be doing essentially the same thing as creating a temp.


Yes, but like many Turing commands, we can also play around with what we have. For example, trading one array's values for another would be like . . .

Turing:

var word : array 1 .. 5 of string := init ("a", "b", "c", "d", "e")
var numb : array 1 .. 5 of string := init ("1", "2", "3", "4", "5")

proc swapString (var x, y : string)
    x := x + y
    y := x (1 .. length (x) - length (y))
    x := x (length (y) + 1 .. length (x))
end swapString

for i : 1 .. upper (word)
    swapString (word (i), numb (i))
    put word (i), "   ", numb (i)
end for


And if one of the array's was longer than the other, and someone did this, they would know that ahead of time, but if they insisted for some reason, than it wouldn't even run. And that's their fault.

. . . What ever happened to that "summer long project" . . .
[Gandalf]




PostPosted: Wed Sep 21, 2005 4:06 pm   Post subject: (No subject)

What?
code:
proc swapNum (var arr : array 1 .. * of int, i, j : int)
    const temp := arr (i)
    arr (i) := arr (j)
    arr (j) := temp
end swapNum


Quote:
What ever happened to that "summer long project"

Heh, I guess it was just 'spirited away'..
beard0




PostPosted: Wed Sep 21, 2005 4:09 pm   Post subject: (No subject)

Ummm... did you read the title?
[Gandalf]




PostPosted: Wed Sep 21, 2005 4:30 pm   Post subject: (No subject)

Right... Right... Doh!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: