Sign in to follow this  
pressgreen

moving 2d array to 3d array

Recommended Posts

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}[/color] what i want is to change "sector" into "sector2"......[color="#8b0000"]sector2[3][3]={{1,2,3},{4,5,6},{7,8,9}};[/color]

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

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

thanks


Green
[/color]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='greenzone' timestamp='1311065135' post='4837264']
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}[/color] what i want is to change "sector" into "sector2"......[color="#8b0000"]sector2[3][3]={{1,2,3},{4,5,6},{7,8,9}};[/color]

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

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

thanks


Green
[/color]
[/quote]

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.

[code]
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[i][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[i][j];


[/code]

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. Edited by IggyT

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this