Advertisement

Yet another dynamic multi dimensional question

Started by February 17, 2003 08:02 PM
2 comments, last by evillive2 21 years, 9 months ago
C++ I have been using a single dimensional array to "simulate" a multidimensional array by using math to get the correct index. For example:

int get_array_value( int x, int y, int z )
{
    return global_array[x+(y*array_width)+(z*array_height*array_width)];
}
 
The reason I chose this way was because I read that C does not really support true multidimensional arrays and this option was the only one I understood. I would like to know how to do it without having to do all the calculations each time I want to access it. My main question is can someone show me how to, or point me in the right direction to create a 3d array so I can access it: array[width][height][depth] and how do I delete it?(in what order) Please don''t point me to std::vector. I would like to know how to do it the long way before the short way. Thanks.
Evillive2
How to create a dynamic 100x100 2D array of integers:


  int** array;array = new int*[100];for (int i = 0; i < 100; i++) {   array[i] = new int[100];}// Delete itfor (int i = 0; i < 100; i++) {   delete[] array[i];}delete[] array;  


I''m sure you can figure out how to do the 3D array.

Firebird Entertainment
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Advertisement
Thanks Tron3k... which order do I do it in though...
could someone replace the 100''s with a variable name?
int rows = 100;int cols = 100;int** array;array = new int*[100/*rows or cols?*/];for (int i = 0; i < 100/*rows or cols?*/; i++){   array = new int[100/*rows or cols?*/];}// Delete itfor (int i = 0; i < 100/*rows or cols?*/; i++){   delete[] array;<br>}<br>delete[] array;<br> </pre> <br><br>I can figure out how to do it in 3d from the above post if someone will help me out with what order to do it in so I get an array like array[cols][rows][depth]?<br><br>Thanks in advance.  </i>   
Evillive2
It really doesn''t matter, as long as you use it the same way consistently. I think most people stick the rows first, then the columns, but it is mainly personal preference. The way the array is stored does not really affect the way you use it in this case.


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement