### Is it possible to convert(copy) a 3D array to a 2D array?

closed account (yqD8vCM9)
The 3D array contains strings. And the strings must be copied into a 2D array in order.
Is this possible?
How?
Possible, if ...

please, give a more detailed example.
closed account (yqD8vCM9)
@keskiverto. If my 3d has 200 strings in it and I want to store all the strings into a 2d array.
myArray[x][y][z] = myArray2[x][y];
Can this be done.

Something like:

I don't know if this is the correct way to do it.
myArray2 would look like

0 1
0 | name1 | name2 |
1 | name3 | name4 |
[/code]

and so on with 100 rows
Last edited on
If you do have a 3D array `std::string foo[X][Y][Z];` you obviously have `N == X*Y*Z` elements.
If you have also a 2D array `std::string bar[R][S]; // where R*S >= N `, then you have room for all elements of foo in it.

Now, the task is to iterate over one array, and copy each element to the other array. For example:
 ``1234`` ``````size_t i = x*Y*Z + y*Z + z; size_t r = i / S; size_t s = i % S; bar[r][s] = foo[x][y][z];``````

The more you know about your system, the better you can optimize it.
 ``123`` ``````std::copy( reinterpret_cast( source ), reinterpret_cast( source ) + X * Y * Z, reinterpret_cast( dest ) );``````
Last edited on
closed account (yqD8vCM9)
I not familiar with the recent codes you guys have provided. Could you simplify it down to basic c level.
Last edited on
From your original post it is totally unclear how you are going to copy strings.
closed account (yqD8vCM9)
@ vlad from moscow I'm basically trying to write my own strcpy function for multidimensional arrays.
@defjamvan123

Just to clarify...

When you say "3D array contains strings", are you talking about a 3D array of chars (which contains strings in C-style buffers) like I thought you were talking about in this earlier thread of yours:

How do you read from file to a 3D array?
http://www.cplusplus.com/forum/beginner/109726/

i.e. this kind of thing

`char myArray[x][y][z];`

(Which can be seen as a 3D array of chars or as a 2D array of char buffers. Plus a few more interpretations.)

Andy
Last edited on
closed account (yqD8vCM9)
@andywestken

Yest a 3D array of chars. You helped me read it from the file. Now i'm trying to copy the data in the same 3d array into a 2d arrray.
In this case you should think of the arrays as being 2D and 1D arrays (of char buffers)

 ``123456789101112131415161718`` `````` // where x, y, z are all const integral values char myArray[x][y][z]; char myOtherArray[x * y][z]; // target array must be at least x * y // etc for ( int i = 0; i < x; i++ ) { for ( int j = 0; j < y; j++ ) { // or you could use a running index strcpy(myOtherArray[i*y + j], myArray[i][j]); } } // etc ``````

Andy
Last edited on
closed account (yqD8vCM9)
@andywestken

I got lost at line 14. You are using strcpy. The is basically the function im trying to recreate.
Can I replace line 14 with this or something similar.
 `` `` ``myOtherArrray[i][j] = myArray[0][i][j];``
Last edited on
Are you not allowed to use the normal strcpy() to implement the array copy?

If not, add another loop, replacing

` strcpy(myOtherArray[i*y + j], myArray[i][j]);`

with

 ``1234`` `````` for ( int k = 0; k < z; k++ ) { myOtherArray[i*y + j][k] = myArray[i][j][k]; }``````

which copies all chars, not just the strings, and assumes the last dimenson of the two arrays is z.

Or you can adjust the loop so it only copies the string as far as the terminating null char, etc.

Andy
Last edited on
closed account (yqD8vCM9)
I can't use strcpy(). But one more thing. If I wanted to output the myOtherArray, what would be in the square brackets.
 `` `` ``cout << myOtherArray[?][?]``
Last edited on
Just

`cout << myOtherArray[k];`

Do you find it hard to think of

`char myOtherArray[x][y];`

as a 1D array of these

`char buffer[y];`

Once you get that, the code above will be trivial.

If you use

`char msg[32] = "Hello!";`

then I would hope you already knew that you output msg like

`cout << msg;`

i.e. one less [] than in the declaration.

Andy

Last edited on
closed account (yqD8vCM9)
Thank you Andy.
Topic archived. No new replies allowed.