Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

evilclown

multidimensional arrays

This topic is 5933 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

How do I make multidimensioned arrays? I know how to do 1 dimension
  
//make an array that is array[sizex][sizey]

int* array = new int[sizex];

//now how make the sizey part?


//and when done, delete

delete [] array;
  

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Jason Zelos
A 3 dimensional array.

      
int* array = new int[sizex][sizey][sizez];



You might like to consider checking this code.

--

It is against the law to stare at the mayor of Paris.


Edited by - SabreMan on February 25, 2002 3:54:00 PM

Share this post


Link to post
Share on other sites
you could

  
// the linear 2d array


int *array = new int[sizex*sizey];
// access by doing

array[x+y*sizex];
delete [] array;

//

// for a planar multidimensional array

//

int **array = (int**)(new int[sizey]);
for(i=0; i<sizey; i++)
array[i] = new int[sizex];

// access by

array[y][x];

// deletion

for(i=0; i<sizey; i++)
delete [] array[i];
delete [] array;


// the c way

// linear 2d array

int *array = (int*)malloc(sizex*sizey);

//deletion

free(array);

// planar 2d array

int **array = (int**)(malloc(sizey));
for(i=0; i<sizey; i++)
array[i] = (int*)(malloc(sizex));

// access by

array[y][x];

// deletion

for(i=0; i<sizey; i++)
free(array[i]);
free(array);



saberman, how would you do such a thing, since you obviously know (by your comment about the code being wrong). since either one is a mulitdimensional array. there may be a better way to allocate planar arrays in c++, i am not sure since i rarly use planar arrays and tend to use c methods of allocation (except for objects).

Share this post


Link to post
Share on other sites
So, is this right?
    
vector< vector<int> > array;

Because I want to use a multidimensioned container of tiles

struct tile{
int id;
int walk;
};

But don't understand how. If the above is right I think I would do

for(i = 0; i < map_width; i++) {
for(j = 0; j < map_height; j++) {
//initialize each tile so I have an

//array of tiles WIDTHxHEIGHT

tile tmp;
tmp.id = 0;
tmp.walk = 1;
map[i].push_back(tmp);
}
}

Do you have any links where I can learn about STL? I don't know anything about it. [/source]

Edited by - evilclown on February 26, 2002 3:33:06 PM

Share this post


Link to post
Share on other sites
Something like this...
  
int ***number_cube = new (int**)[5];
for(int i = 0; i < 5; ++i) {
number_cube[i] = new (int*)[5];
for(int j = 0; j < 5; ++j) {
number_cube[i][j] = new int[5];
}
}

...

for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 5; ++j) {
delete[] number_cube[i][j];
}
delete[] number_cube[i];
}
delete[] number_cube;

Share this post


Link to post
Share on other sites
I''d do it like this using C style syntax.

  
struct tile {
int id;
int walk;
};


tile TileMap[map_width][map_height];


for(i = 0; i < map_width; i++) {
for(j = 0; j < map_height; j++) {
TileMap[i][j].id = 0;
TileMap[i][j].walk = 1;
}
}



I think this is a more conventional way of doing it, if not easier to think about.

I''m going to have to talk to my eyes about reading posts all the way through before replying .

,Jay

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!