Advertisement

Swapping values

Started by June 08, 2002 04:43 PM
13 comments, last by Zorbfish 22 years, 5 months ago

  
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...
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]); 
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Advertisement
You don''t even have to implement it, just use std::swap in <algorithm>

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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?
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]
No way! I will never write a profile signature! :p
/*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
theNestruoSyntax error in 2410Ok
Advertisement
Use VB, it''s like this

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

I win, you lose. J/K

Horny Farmer (Jolly Rancher)
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.
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]

This topic is closed to new replies.

Advertisement