
-----------------------------------
beard0
Tue Sep 20, 2005 10:55 pm

Swap values without temp
-----------------------------------
I just thought it would be neat to try.  Here are my results:
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.

-----------------------------------
jamonathin
Wed Sep 21, 2005 8:02 am

Re: Swap values without temp
-----------------------------------
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 . . .


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]
Wed Sep 21, 2005 4:06 pm


-----------------------------------
What?
proc swapNum (var arr : array 1 .. * of int, i, j : int)
    const temp := arr (i)
    arr (i) := arr (j)
    arr (j) := temp
end swapNum

What ever happened to that "summer long project"
Heh, I guess it was just 'spirited away'..

-----------------------------------
beard0
Wed Sep 21, 2005 4:09 pm


-----------------------------------
Ummm... did you read the title?

-----------------------------------
[Gandalf]
Wed Sep 21, 2005 4:30 pm


-----------------------------------
Right... Right...  :doh:
