1. ## 3+d array?

Is it possible to have a 3+ dimensional array?

I think a 3d array would work when working with 3d, but 4d+? Wouldnt that defy the laws of physics or something? What would it look like?

2. Code:
```int three_dimensional_array[x][y][z];
int four_dimensional_array[x][y][z][a];
int eight_dimensional_array[x][y][z][a][b][c][d][e];```
You'll run out of memory pretty quickly if you have lots of large arrays, though.

3. wait, how can you simulate a 4d+ array? Make a 4dimensional world?!

Isnt 4d time, 5d space, 6d amtter, 7d i have no clue... but im gonna google it.

4. What are you talking about? You're not creating physical dimensions here. Just a series of arrays of arrays of arrays ...

Quzah.

5. You are creating a huge mess. The most dimensions I'd ever use is 2, and I normally use 1D arrays or linear arrays.

6. I know, but arrays can be just like dimensions in real life. Isnt there a way to simulate it somehow? How 1d is like a string (a line) while 2d is like a square (down adn across) and 3d is like a 3d cube(detph/z axis). So what would 4d be?
Oh and this is just for informational purposes... Its not like im really gonan be using anything more then 3d tops.

7. Originally Posted by SG57
wait, how can you simulate a 4d+ array? Make a 4dimensional world?!

Isnt 4d time, 5d space, 6d amtter, 7d i have no clue... but im gonna google it.
The fact that humans perceive only 3 physical dimensions is coincidental to the fact that arrays can be multi-dimensional. Each dimension of an array represents whatever you are needing to represent. If you want to use a 4 dimension array to keep track of x,y and z coordinates for each time interval a then you could use a 4 dimension array.
Code:
`world[x][y][z][a]`
You could also use a larger dimension array something like
Code:
`data[row][column][sheet][workbook][folder][company][city][state][country][etc...]`
where the row and column are what is stored in the row and column of a spreadsheet that is in a particular workbook that is located in a particular folder that resides within a particular company that is based out of a certain city in a certain state in a particular country... This could be accomplished much more efficiently using other methods of course, but is an example of a 9+ dimension array.
Originally Posted by SG57
I know, but arrays can be just like dimensions in real life. Isnt there a way to simulate it somehow? How 1d is like a string (a line) while 2d is like a square (down adn across) and 3d is like a 3d cube(detph/z axis). So what would 4d be?
What do you want 4d to be? There are widely differing opinions of what the 4th dimension is, whether it is actually time, or it is some measurement humans are unable to perceive naturally, or whatever. If you are the program writer, you decide what 4d is. I like to think of the 4th dimension as time because it is conceptually easy for me to understand, but I have heard compelling arguments otherwise.

Either way, you should define your world (3d or 4d or 12d?) before you start trying to figure out how to represent it in a data structure.

8. you ready for a mess of uneccessary code?

Code:
``` int i = 3;
int* pi = &i;
int** p2i = &pi;
int*** p3i = &p2i;
int**** p4i = &p3i;
int***** p5i = &p4i;
int****** p6i = &p5i;
int******* p7i = &p6i;
int******** p8i = &p7i;
int********* p9i = &p8i;
int********** p10i = &p9i;
int*********** p11i = &p10i;
int************ p12i = &p11i;
int************* p13i = &p12i;
int************** p14i = &p13i;
int*************** p15i = &p14i;
int**************** p16i = &p15i;
int***************** p17i = &p16i;
int****************** p18i = &p17i;
int******************* p19i = &p18i;
int******************** p20i = &p19i;
int********************* p21i = &p20i;
int********************** p22i = &p21i;
int*********************** p23i = &p22i;
int************************ p24i = &p23i;
int************************* p25i = &p24i;
int************************** p26i = &p25i;
int*************************** p27i = &p26i;
int**************************** p28i = &p27i;
int***************************** p29i = &p28i;
int****************************** p30i = &p29i;
int******************************* p31i = &p30i;
int******************************** p32i = &p31i;
int********************************* p33i = &p32i;

cout << *********************************p33i << endl;```
This is an attempt to find out if my compiler (MSVC 2003) put a limit on the levels of indirection I can have. Looking at the asm, she accounted for all 33. I don't want to go any further.

9. My compiler (MinGW) supports over 3000 levels of indirection before it runs out of stack space (it must be recursive).

10. The impossibility of 4 dimensions in real life does not in any way affect the plausibility of having a multidimensional array in C. In any case, even a 2D array is stored one-dimensionally in your computer's RAM. In fact, most processors don't support 2+D arrays; they are only provided in C as a convenience.

11. A line of text. - 1 dimension.
A page of lines. - 2 dimensions.
A folder of papers. - 3 dimensions.
A box of folders. - 4 dimensions.
A truck of boxes. - 5 dimensions.
A warehouse of trucks. - 6 dimensions.
A field of warehouses. - 7 dimensions.

We could go on, but I'll assume you've grasped the concept.

Quzah.

12. This thread is a clear example of the complete abuse of arrays and their usefulness.

This should answer all your questions. Notice it's from webster.cs.ucr - the same site you can get Randall Hyde's Art of Assembly Language. Definitely not a coincedence.

http://webster.cs.ucr.edu/AoA/Window.../Arraysa2.html

Now you can see why using more than 1 dimension will severely impact the performance of the array, especially if does NOT have dimensions that are a power of 2. Even using bit shifting for multi-dimensional arrays with dimensions that are a power of 2 will still be slower than using a 1 dimensional array.

So how do you solve the problem? In C you use structs, and in C++ you use classes.

Code:
```struct Person
{
int iAge;
int iHeightInches;
int iShoeSize;
int iYearBorn;
int iMonthBorn;
int iDayBorn
};

Person Crowd[1000];```
Much better than:

Code:
`int Crowd[1000][6];`
To fill the array using structs watch:

Code:
```for (int i=0;i<1000;i++)
{
Crowd[i].iAge=0;
Crowd[i].iHeightInches=0;
Crowd[i].iShoeSize=0;
Crowd[i].iYearBorn=0;
Crowd[i].iMonthBorn=0;
Crowd[i].iDayBorn=0;
}```
1000 iterations, 6 variables set per iteration.

As opposed to the two dimensional method:

Code:
```for (int i=0;i<1000;i++)
{
for (int j=0;j<6;j++)
{
Crowd[i][j]=0;
}
}```
6000 iterations.

You could unroll the inner loop and still get 1000 iterations, but you still have the fact that in the array, none of the elements have any apparent meaning. They are just memory locations. With the structure you can tell exactly what each variable is representing.

The only reason I would ever allocate a huge linear array is if I wanted to do my own memory management for something like a game. Then I would make functions and sort of a small memory management API to manage that huge chunk of memory. This has the benefit of, hopefully, being faster, easier to use, etc.

Hope this helps.