Skip to main content
GameDev.net gamedev.net
🔒 Locked

Reverse 2D arrays

Started by OpenGL_Guru Aug 1, 2006 at 2:31 PM 20 replies 9.2k views
Original Post
OpenGL_Guru
OpenGL_Guru
Hi there.. i am wondering how (or what) is the best way to reverse a 2D array..not just reverse the numbers but reverse the rows: for instance lets say i have a 5 x 4 2D array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 the result i am looking for is: 16 17 18 19 20 11 12 13 14 15 6 7 8 9 10 1 2 3 4 5 i had thought about using 2d array through indexing but thought pointers might be easier.. any thoughts?
heh
Alpha_ProgDes
Alpha_ProgDes
for (int i = 0; i < array_length/2; ++i) {     int end = array_length - 1;   for (int j = 0; j < 5; ++j)   {      //do a xor swap on the two values:      //array[j] and array[end][j]   }   --end;}
Beginner in Game Development?  Read here. And read here.  
Zahlman
Zahlman
Option 1: use boost::array to represent arrays, then just std::reverse the 'outer' array:

boost::array<boost::array<int, 5>, 4> my_array;std::reverse(my_array.begin(), my_array.end());


Option 2: Write your own wrapper for arrays similar to boost::array (but why?).

Option 3: Implement the whole thing yourself:

// TODO... I will edit this from home
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Alpha_ProgDes
for (int i = 0; i < array_length/2; ++i) {     int end = array_length - 1;   for (int j = 0; j < 5; ++j)   {      //do a xor swap on the two values:      //array[j] and array[end][j]   }   --end;}


are you sure that not only reverses the rows but also the order of the numbers?

i want the numbers to stay in the right order, just swap the rows..essentially swapping the array upside down.
heh
Deyja
Deyja
std::vector<std::vector<int> > a_vector;populate a_vector;std::vector<std::vector<int> > reversed_vector(a_vector.rbegin(), a_vector.rend());
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Deyja
std::vector<std::vector<int> > a_vector;populate a_vector;std::vector<std::vector<int> > reversed_vector(a_vector.rbegin(), a_vector.rend());


assume i dont have std. thats what i am assuming :)
heh
Conner McCloud
Conner McCloud
Quote:

are you sure that not only reverses the rows but also the order of the numbers?

There's a sure fire way to find out.

CM
Conner McCloud
Conner McCloud
Quote:
Original post by OpenGL_Guru
Quote:
Original post by Deyja
std::vector<std::vector<int> > a_vector;populate a_vector;std::vector<std::vector<int> > reversed_vector(a_vector.rbegin(), a_vector.rend());


assume i dont have std. thats what i am assuming :)

Then specify what language you are writing this in, because it clearly isn't C++.

CM
Deyja
Deyja
Quote:
assume i dont have std. thats what i am assuming :)


When I write C++ code, I use the standard library. Without the standard library, C++ isn't C++. It's just C with Classes.
nicksterdomus
nicksterdomus
Quote:
Original post by Alpha_ProgDes
int end = array_length - 1;for (int i = 0; i < array_length/2; ++i) {     //int end = array_length - 1;     // end will never change with this line here   for (int j = 0; j < 5; ++j)   {      //do a xor swap on the two values:      //array[j] and array[end][j]   }   --end;}


Now it should work.
Rasmadrak
Rasmadrak
stupid question, but... can't you just read it from the back?
or do you need this array to be used in both directions "simultaniously"?
"Game Maker For Life, probably never professional thou." =)
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Rasmadrak
stupid question, but... can't you just read it from the back?
or do you need this array to be used in both directions "simultaniously"?


no because i want to switch columns and rows and not the numbers in the rows..see the orginal post...this does put a kink in it..
heh
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by nicksterdomus
Quote:
Original post by Alpha_ProgDes
int end = array_length - 1;for (int i = 0; i < array_length/2; ++i) {     //int end = array_length - 1;     // end will never change with this line here   for (int j = 0; j < 5; ++j)   {      //do a xor swap on the two values:      //array[j] and array[end][j]   }   --end;}


Now it should work.



yes but itseems still you are swapping fromthe wrong end of the end of the array. if the array length is 20 then you would swap array[0][0] and array [19][0] which is not right. what the first row should be would be to swap [0][0],[15][0] & [0][1],[16][1] & [0][2],[17][2] etc etc


heh
Conner McCloud
Conner McCloud
Quote:
Original post by OpenGL_Guru
[yes but itseems still you are swapping fromthe wrong end of the end of the array. if the array length is 20 then you would swap array[0][0] and array [19][0] which is not right. what the first row should be would be to swap [0][0],[15][0] & [0][1],[16][1] & [0][2],[17][2] etc etc

You aren't making any sense. If the array length is 20, then why would you swap row 0 with row 15? That isn't a reversal, that's just a shuffling.

CM
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Conner McCloud
Quote:
Original post by OpenGL_Guru
[yes but itseems still you are swapping fromthe wrong end of the end of the array. if the array length is 20 then you would swap array[0][0] and array [19][0] which is not right. what the first row should be would be to swap [0][0],[15][0] & [0][1],[16][1] & [0][2],[17][2] etc etc

You aren't making any sense. If the array length is 20, then why would you swap row 0 with row 15? That isn't a reversal, that's just a shuffling.

CM


sure its a reversal.. its a reverals of the ROWS with their columns..not the data itself..
heh
outRider
outRider
for r = 0; r < rows/2; r++	for c = 0; c < cols; c++		swap( array[r][c], array[rows - r - 1][c] )
Conner McCloud
Conner McCloud
Quote:
Original post by OpenGL_Guru
sure its a reversal.. its a reverals of the ROWS with their columns..not the data itself..

Show an example of what you mean. And no, your first one does not count. You've been given code that implements it, and rejected it.

CM
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Conner McCloud
Quote:
Original post by OpenGL_Guru
sure its a reversal.. its a reverals of the ROWS with their columns..not the data itself..

Show an example of what you mean. And no, your first one does not count. You've been given code that implements it, and rejected it.

CM


which code are you talking about..

int end = array_length - 1;for (int i = 0; i < array_length/2; ++i) {     //int end = array_length - 1;     // end will never change with this line here   for (int j = 0; j < 5; ++j)   {      //do a xor swap on the two values:      //array[j] and array[end][j]   }   --end;}


lets me give you the 2D array again:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
therefore array_length = 20

end = 19
so the first time this code wants to swap array[0][0] and array[19][0] which is the number 1 and 20 respectively. this is not what i am trying to do. i want to be able to swap 1 and 16, 2 and 17, 3 and 18, 4 and 19, 5 and 20 for the first row. the numbers stay in the same order i am just swapping the rows themselves.
then the next row i would want to swap 6 and 11, 7 and 12, 8 and 13, 9 and 14 and 10 and 15. again the numbers stay in their original sequence but the rows themselves are swapped.
heh
OpenGL_Guru
OpenGL_Guru
Quote:
Original post by Anonymous Poster
Quote:
Original post by OpenGL_Guru
Quote:
Original post by Conner McCloud
Quote:
Original post by OpenGL_Guru
sure its a reversal.. its a reverals of the ROWS with their columns..not the data itself..

Show an example of what you mean. And no, your first one does not count. You've been given code that implements it, and rejected it.

CM


which code are you talking about..

*** Source Snippet Removed ***

lets me give you the 2D array again:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
therefore array_length = 20

end = 19
so the first time this code wants to swap array[0][0] and array[19][0] which is the number 1 and 20 respectively. this is not what i am trying to do. i want to be able to swap 1 and 16, 2 and 17, 3 and 18, 4 and 19, 5 and 20 for the first row. the numbers stay in the same order i am just swapping the rows themselves.
then the next row i would want to swap 6 and 11, 7 and 12, 8 and 13, 9 and 14 and 10 and 15. again the numbers stay in their original sequence but the rows themselves are swapped.


uhm, if you got an array like that you should probably use
array[0][0] to array[4][4]. (makes things alot easier)

thus you would swap array[0][0] with array[4][0] , array[0][1] with array[4][1] etc.
*** Source Snippet Removed ***


yeah i was actually confusing myselfin my last post it should actually be [0,0] and [0,4], [1,0]and[1,4] etc but that helps.. thanks a bunch..i was confusing myself too much.. rating++ if you werent anon :)

[Edited by - OpenGL_Guru on August 3, 2006 9:12:28 AM]
heh
outRider
outRider
Good lord. All this confusion over a simple task.

You really should be able to work this out yourself. Get a piece of paper and a pencil if you have to and work out each step, it's not a difficult problem. I think most people assumed you wanted to reverse the array, i.e. rows and cols, which led to the confusion.

Anyhow, look at the pseudo snippet I posted, it does what you want and is as simple as it gets, no temp arrays, no hassle, and you should be able to follow it. If not, get a piece of paper and a pencil...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.