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

Sep 2, 2013 at 3:44pm
closed account (yqD8vCM9)
The 3D array contains strings. And the strings must be copied into a 2D array in order.
Is this possible?
How?
Sep 2, 2013 at 4:04pm
Possible, if ...

please, give a more detailed example.
Sep 2, 2013 at 4:08pm
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 Sep 3, 2013 at 10:43pm
Sep 2, 2013 at 5:03pm
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:
1
2
3
4
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.
Sep 2, 2013 at 5:42pm
1
2
3
std::copy( reinterpret_cast<std::string *>( source ),
           reinterpret_cast<std:;string *>( source ) + X * Y * Z,
           reinterpret_cast<std:;ctring *>( dest ) );
Last edited on Sep 2, 2013 at 5:51pm
Sep 2, 2013 at 6:05pm
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 Sep 2, 2013 at 6:07pm
Sep 2, 2013 at 6:12pm
From your original post it is totally unclear how you are going to copy strings.
Sep 2, 2013 at 6:26pm
closed account (yqD8vCM9)
@ vlad from moscow I'm basically trying to write my own strcpy function for multidimensional arrays.
Sep 2, 2013 at 7:07pm
@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 Sep 2, 2013 at 7:11pm
Sep 2, 2013 at 7:32pm
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.
Sep 2, 2013 at 7:53pm
In this case you should think of the arrays as being 2D and 1D arrays (of char buffers)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // 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 Sep 2, 2013 at 8:07pm
Sep 2, 2013 at 8:25pm
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 Sep 2, 2013 at 8:25pm
Sep 2, 2013 at 9:05pm
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

1
2
3
4
  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 Sep 2, 2013 at 9:05pm
Sep 2, 2013 at 9:28pm
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 Sep 2, 2013 at 9:29pm
Sep 2, 2013 at 9:48pm
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 Sep 2, 2013 at 9:48pm
Sep 2, 2013 at 10:20pm
closed account (yqD8vCM9)
Thank you Andy.
Topic archived. No new replies allowed.