moving 2d array to 3d array

Started by
2 comments, last by pressgreen 12 years, 9 months ago
if i have an array of floats how can i turn that array into a multidimensional array. ex: [color="#8b0000"]sector[9] = {1,2,3,4,5,6,7,8,9} what i want is to change "sector" into "sector2"......[color="#8b0000"]sector2[3][3]={{1,2,3},{4,5,6},{7,8,9}};

So if you under stand the function required and logic an explanation would be awesome.[color="#8b0000"]:)

[color="#000000"]Im using c++

thanks


Green
J-GREEN

Greenpanoply
Advertisement
You will need two identifiers. Then, the simplest, lowest-level solution is to just use [font="'Courier New"]memcpy [/font]and take advantage of C definition of arrays. This will possibly wreak havoc when dealing with arrays of objects.
A more C++ solution would be to have two loops on row and column respectively... try sketching it first.

Previously "Krohm"


if i have an array of floats how can i turn that array into a multidimensional array. ex: [color="#8b0000"]sector[9] = {1,2,3,4,5,6,7,8,9} what i want is to change "sector" into "sector2"......[color="#8b0000"]sector2[3][3]={{1,2,3},{4,5,6},{7,8,9}};

So if you under stand the function required and logic an explanation would be awesome.[color="#8b0000"]:)

[color="#000000"]Im using c++

thanks


Green



It sounds like you want to just put the information from one array into another array, correct?

If so this sounds like you just need a nested loop to solve your re-locating problem.


int first=3;
int second = 3;

int sector[9] = {1,2,3,4,5,6,7,8,9};

int sector2[first][second];

for(int i = 0, s_count = 0; i<first; i++)
for(int j = 0; j<second; j++, s_count++) //update s_count here so that it can update as the j position updates.
sector2[j] = sector[s_count];

for(int i = 0, s_count = 0; i<first; i++) //this is for displaying the results and not part of the transfer process
for(int j = 0; j<second; j++)
cout<<sector2[j];




In this code s_count is kept track of so that you can add to each new array section first run through it starts at 0, the second at 3, third at 6...etc

EDIT:

I suggest doing as Krohm suggested, sketch this out, it'll give you a better understanding than someone just explaining it to you would.
Hey thanks, that's exactly what i am looking for thanks :D
J-GREEN

Greenpanoply

This topic is closed to new replies.

Advertisement