0

I have a 3d array which is basically 9 chunks of 2d arrays defined as 100x100 where they would be lined up in a 3x3 grid. I want to transform this 3D array into just a 2D array.

array[9][100][100] needs to be turned to array[900][900]

Any ideas on how I'd go about this?

[EDIT]

So I'm going to lower the counts to make it more manageable. I'll use array[9][5][5];

Using the answer below if I init the array and print out to file I don't get what I would expect. Given how I init, what I expect, and want, is a file that looks like:

000001111122222
000001111122222
000001111122222
000001111122222
000001111122222
333334444455555
333334444455555
333334444455555
333334444455555
333334444455555
666667777788888
666667777788888
666667777788888
666667777788888
666667777788888

So I made the value's tie to the first dimension. It gives me 9 quadrants of 2D arrays. This is the layout I want. So the first dimension is really defining the quadrant that the other 2 dimensions should be placed. So the point of this is that each quadrant 2D array will hold the tile id to draw our map. I want to do this because the player will start in the center (quadrant 4) but as he moves to say quadrant 5, the server (it's an mmo) will see that it moved to the right out of center quadrant, and so it'll need to send the entire right side quadrants of more map data.

Basically it's send a new 2, 5, & 8 quadrant of map data. I'll them shift the 3D array accordingly (it's easy at this point right. just move around entire quadrants), then convert to the 2D array on the client so that I can easily find what sub part of this 2D map I need to draw since I can find out the exact tile the player is on in the 2D array with simple math. This gives me a streaming map that can be massive on the server, but only chunks sent to the client when they need it.

for (j = 0; j < 5; j++) 
{
        for (k = 0; k < 5; k++) 
        {
            a[0][j][k] = 0;
            a[1][j][k] = 1;
            a[2][j][k] = 2;

            a[3][j][k] = 3;
            a[4][j][k] = 4;
            a[5][j][k] = 5;

            a[6][j][k] = 6;
            a[7][j][k] = 7;
            a[8][j][k] = 8;
        }
}
4
  • The first one has 9000 elements the second one 810000. Commented Dec 13, 2012 at 23:48
  • the task is incomplete. dimensions don't match, so you should give extra information Commented Dec 13, 2012 at 23:50
  • The row/col in the final should be equal. Picture each of the 100x100 2D arrays as a tile map for a game. Now I want 9 of these tile maps. Now picture these 9 tile maps in a 3x3 grid. So if 1 2D array of 100x100 is a chunk of the map, then I want 9 of those chunks. Of those chunks, elements 0-2 are at the top, then 3-5 in the middle, 6-8 on the bottom. Given that configuration I want to make it a 2D array. So I was incorrect it should be a 300x300 2D array I think. Commented Dec 14, 2012 at 1:39
  • if you are coding in C++ don't present code that is 100% C, I mean there are all the data structures that you need in C++ with a much more modern approach, typesafe, that provide useful methods. You are just coding in C using a C++ compiler. Commented Dec 14, 2012 at 4:42

1 Answer 1

1

You can represent any n-dimensional array as an m-dimensional array, as long as you have m <= n; m > 0.

The task would be simply to keep track of where is what, and how to reach it.

In your case, converting array[9][100][100] into a two dimensional array could be done as follows:

d1 = 0;
d2 = 0;

a = array[9][100][100];
b = array[2][9 * 100 * 100 / 2];

for (unsigned int i(0); i < 9; i++) {
    for (unsigned int j(0); j < 100; j++) {
        for (unsigned int k(0); k < 100; k++) {
            b[d1][d2] = a[i][j][k];
            d2++;
            d1 = (d2 == 9 * 100 * 100 / 2) ? 1 : 0;
            d2 = (d2 == 9 * 100 * 100 / 2) ? 0 : d2;
        }
    }
}

Anything other than this would require a special mapping, which you need to provide in order to say where will the value a[i][j][k] be stored in b[d1][d2].

Considering your answer, i guess this is what you're looking for:

int d1 = 0;
int d2 = 0;

int a[9][100][100];
int b[300][300];

unsigned int i(0), j(0), k(0);
for (i = 0; i < 9; i++) {

    for (j = 0; j < 100; j++) {

        for (k = 0; k < 100; k++) {
            b[d1 + j][d2 + k] = a[i][j][k];
        }

    }

    d1 = (d1 + j == 300) ? 0 : d1 + j;
    d2 = (d2 + k == 300) ? 0 : d2 + k;

}

Regards!

Sign up to request clarification or add additional context in comments.

6 Comments

hmm, that doesn't seem right to end up with a 2x9000 so I probably wasn't clear. The row/col in the final should be equal. Picture each of the 100x100 2D arrays as a tile map for a game. Now I want 9 of these tile maps. Now picture these 9 tile maps in a 3x3 grid. So if 1 2D array of 100x100 is a chunk of the map, then I want 9 of those chunks. Of those chunks elements 0-2 are at the top, then 3-5 in the middle, 6-8 on the bottom. If I really wanted to I could create 9 separate 2D arrays for this but thought it would be easier to make it a 3D array.
yep, i agree using a 3d array goes simpler, but, what is your question afterall? what do you want to have? do you want an array[300][300]?
So to add to this, I would loop through the first row in array[0][0][n], array[1][0][n], & array[2][0][n] and that would all go into final_array[0][n] where defined as final_array[300][300].
Yes you are correct. I was wrong by saying [900][900] it would be [300][300].
i've added the code considering your answer, please, check it out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.