Assist explain 3 dimentional arrays.

Can any one explain to me in some details about 3 dimensional arrays please, and show me how I can apply it to a problem like. A ship with 3 level and 52 seats per level.trying to make a reservation program.

I will be so greateful.
Actually, your example only requires a two dimensional array.

ship[level_number][seat_number] = isOccupied


I'm assuming you probably want to do something along those lines.

You could think of it multiplication. Using your example, we have 52 seats per level, and 3 levels per ship. So we have 3(levels/ship) * 52(seats/level) = 156(seats/ship), which could be thought of as a "2 dimensional multiplication"

Similarly, to find a specific seat on a ship (assuming there is only 1 ship to choose from), we pick a level first, and a seat in that level second.


To give a three dimensional example, lets say we have 5 ships per platoon.

5(ships/platoon) * 3(levels/ship) * 52(seats/level) = 780(seats/platoon)

And, given that we have 1 platoon, we can find a specific seat by getting the ship in the platoon, the level in that ship, and finally the seat in that level, which would look like

platoon[ship_number][level_number][seat_number] = isOccupied

Uhmm.....ships come in fleets, soldiers come in platoons...I think.
A simple 3 dimensional array could be written as int a [3][3];//3 cols and 3 rows
It could also be written as int a [][]={{3,2,7},{4,2,7},{6,5,9}};
or int a [3][3]={3,2,7};//where only the first 3 elements 0 - 2 are initialized with specific values and the remaining 6 elements 3 - 8 are initialized with default 0
or int a=(3,2,7,4,2,7,6,5,9};// so the simplest form of a multi-dimensional array is the single dimensional array
If you want to display the above use nested for loops:
1
2
3
for(int i=0;i<3;i++)//defines row elements
    for(Int j=0;j<3;j++)    //defines column elements
    cout<<a[i][j]<<" ";

prints
3 2 7
4 2 7
6 5 9
There are topics all over the forum that warn you for multidimensional arrays in C++. And THEY ALL ARE RIGHT! ;-). Don't do multidimensional arrays! Don't be a masochist. Use plain arrays or better std::vector. It will save a lot of pain later. You can easily "simulate" multidimensional arrays with a bit of calculation (index = y*columns+x).

Ciao, Imi.
I don't get this - What is wrong with multidimensional arrays?
Disch digged out a very nice thread about lots of the evil things of Multidimensional arrays.

Basically it's hard to use them correct. And you reach their limits very quickly. As example, if you want dynamically sizes (int[y][x] where x and y are determined at runtime), it's not really possible. You can only have the leftmost parameter dynamically. (Then, most people fall back to arrays of pointer to arrays, which get a mess very quickly.)

My advise is (for C++. C is something different ;): Everytime you declare an array, you should think "Is this necessary? Won't a std::vector do?". Everytime you are tempted to declare a multi-dimensional array, ask yourself: "How can I replace the MD-array as quick as possible?"


Ciao, Imi.
Topic archived. No new replies allowed.