Public Group

#### Archived

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

# Swapping values

This topic is 5852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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]

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631380
• Total Posts
2999673
×