Archived

This topic is now archived and is closed to further replies.

Swapping values

Recommended Posts


int myarray[3][3]={
{0,0,0},
{0,0,1},
{0,0,0}};
int x=1;
int y=1;
int first=myarray[y][x];
int second=myarray[y][x+1];
int temp=second;
myarray[y][x+1]=first;
myarray[y][x]=temp;

Is there any other way to swap array values? This way seems a bit unefficient...

Share on other sites
It is hard to make it simpler than this:
void swap(int* a, int* b){   int temp = *a;   *a = *b;   *b = temp;}swap(&myarray[y][x+1], &myarray[y][x]);

Share on other sites
Ok... thanks. Btw my friend asked me an interesting question today about arrays, and it got me wondering... Is it possible to just swap entire rows/columns with each other in an array?

Share on other sites
quote:
Original post by Zorbfish
Ok... thanks. Btw my friend asked me an interesting question today about arrays, and it got me wondering... Is it possible to just swap entire rows/columns with each other in an array?

Lets say you have an array like this:
int Array[3][3];

So a row is 3 cells, maby a temp array would solve the problem:
int Array[3][3];
int tmpArray[3][1];

This code is not compiled, but I think should work fine

      void Flip(int *Array, int Cols, int rowIndex1, int rowIndex2) {    int tmpArray[3][1];    int i;    for(i=0; i<Cols; i++) {        tmpArray[i][0] = (rowIndex1 + (rowIndex1 * Cols)) + i;    } // Put first row in tmpArray    for(i=0; i<Cols; i++) {        (rowIndex1 + (rowIndex1 * Cols)) = (rowIndex2 + (rowIndex2 * Cols))    } // Put second row in first row    for(i=0; i<Cols; i++) {        (rowIndex2 + (rowIndex2 * Cols)) = tmpArray[i][rowIndex1]    } // Put tmp row in second row}

Use it like:
int arrayX = 3, arrayY = 3;
int Array[arrayX][arrayY];
Flip(&Array[0][0], arrayY, 1, 3); // Flip row 1 and 3 in Array. It haves arrayY cols

First time I use pointers, I didn't know they were SO flexible

[edited by - the Chef on June 8, 2002 8:00:44 PM]

[edited by - the Chef on June 9, 2002 5:44:25 AM]

Share on other sites
/*How to swap two integers in just one line*/a^=b^=a^=b;

It''s cool, isn''t it?

theNestruo

Syntax error in 2410
Ok

Share on other sites
Use VB, it''s like this

myArray(2,5) = myArray(4,3)

I win, you lose. J/K

Horny Farmer (Jolly Rancher)

Share on other sites
quote:
Original post by theNestruo
/*How to swap two integers in just one line*/
a^=b^=a^=b;

It''s cool, isn''t it?

That is pretty CL, what exactly is going on there??

bangz.

Share on other sites
In binary (^ is the XOR exclusive-or operation)
     a = 00001111 b = 00111100a^=b a = 00110011 b = 00111100b^=a a = 00110011 b = 00001111a^=b a = 00111100 b = 00001111

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share on other sites
Check this out:

a += b; // a + b
b = -b;
b += a; // (a + b) - b = a
a -= b; // (a + b) - a = b

But I think assembly beats both of them:

xchg a, b

Give a man a fish and you feed him for a day; teach him to use the Net and he won't bother you for weeks.

[edited by - thooot on June 10, 2002 10:01:36 PM]

Share on other sites
quote:
Original post by theNestruo
/*How to swap two integers in just one line*/a^=b^=a^=b;

It''s cool, isn''t it?

Plain evil. It will fail if both a and b are the same variables. Don''t ever make this swap to a macro or inline function.

Share on other sites
quote:
Original post by theNestruo
It''s cool, isn''t it?

No, it''s undefined behaviour, since you are modifying a variable more than once without an intervening sequence point.

Share on other sites
This has been discussed at length before ( swapping variables )... Do a search, you''ll come across it.

I remember benchmarking various techniques for swapping variables, and the XOR version was the slowest. Four mov was the quickest, followed by the xchg instruction ( I think ), and then a temp variable... I can''t quite remember, look up the thread for more info.

Death of one is a tragedy, death of a million is just a statistic.

Share on other sites
Thanks... I have the idea of what I need to do... from the looks of things I did have the right idea about swapping... I needed it for a puzzle game Im making where you swap blocks around to score points... I asked about the moving rows/cols because I was thinking of adding a row of randomly chosen blocks to the bottom of the array everytime a set timer goes off. So I needed a way to shift all the existing blocks up the array to make an empty bottom row for the next set...

Share on other sites
quote:
Original post by python_regious
I remember benchmarking various techniques for swapping variables, and the XOR version was the slowest. Four mov was the quickest, followed by the xchg instruction ( I think ), and then a temp variable

Interesting, I was under the assumption that bitwise operations were fast, faster than using a temp var, especially when used to swap variables.

• Forum Statistics

• Total Topics
628354
• Total Posts
2982243

• 10
• 9
• 11
• 24
• 11