PDA

View Full Version : Swap integers in a 2 dimensional array


Morgoth
03-11-2005, 01:28 AM
This is how I swap integers in an array:

For I = 0 To 6
For J = 0 To I
If intCardCount(I) > intCardCount(J) Then
intTemp = intCardCount(J)
intCardCount(J) = intCardCount(I)
intCardCount(I) = intTemp
End If
Next J
Next I


I can use a mathematical method and not have to use a 3rd variable, but this is easier to read.

Now, I want to swap integers in a 2 dimensional array.
I want to to swap the location of the second dimention of the array. The first dimention is the location ID. I want the seond array dimention of the array to stay in perfect order.

I am going to test a few ideas out on my own, but if there is a proper method for this, I'd like to know please.


Edit: This is an example of what I need. And don't worry about the condition of why it changes, I just need to know how to swap them:

(1,1) = "a" (1,2) = "b" (1,3) = "c"
(2,1) = "d" (2,2) = "e" (2,3) = "f"
(3,1) = "g" (3,2) = "h" (3,3) = "i"

swap first and second

(1,1) = "s" (1,2) = "e" (1,3) = "f"
(2,1) = "a" (2,2) = "b" (2,3) = "c"
(3,1) = "g" (3,2) = "h" (3,3) = "i"

aman
03-11-2005, 03:35 AM
A couple of ideas come to mind, but it depends on what you need to do with the array.

You could do a value by value swap, you could do a memcpy swap of the whole array at once (using a temp), or you could just use pointers to the arrays, and simply swap the location that the pointers point to.

Morgoth
03-11-2005, 03:46 AM
I decided to go in a different direction.
But thanks for your input aman.