Swap values without temp
Author |
Message |
beard0
![](http://ugray.be/compsci/silly.gif)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: 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" . . . |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: 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'.. |
|
|
|
|
![](images/spacer.gif) |
beard0
![](http://ugray.be/compsci/silly.gif)
|
Posted: Wed Sep 21, 2005 4:09 pm Post subject: (No subject) |
|
|
Ummm... did you read the title? |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Wed Sep 21, 2005 4:30 pm Post subject: (No subject) |
|
|
Right... Right... ![Doh! Doh!](http://compsci.ca/v3/images/smiles/eusa_doh.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|