Jump to content
  • Advertisement
Sign in to follow this  
pressgreen

moving 2d array to 3d array

This topic is 2557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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} 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

Share this post


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!