Jump to content
  • Advertisement

Archived

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

Zorbfish

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!