explain 3d arrays

can someone please explain to me 3d arrays using words and examples

There is no such thing as 2D or 3D arrays, only 1D arrays. You can use math to treat an array as if it were 2D or 3D, but it is still actually 1D. You can also have an array of arrays, but each array does not have to be the same length - you have to enforce that yourself.
honestly that didn't help me at all
There was once 24 boys in a queue.
The teacher told the 1st four boys that they should call themselves "the first row of first layer".
The teacher told the 2nd four boys that they should call themselves "the second row of first layer".
The teacher told the 3rd four boys that they should call themselves "the third row of first layer".
The teacher told the 4th four boys that they should call themselves "the first row of second layer".
The teacher told the 5th four boys that they should call themselves "the second row of second layer".
The teacher told the last four boys that they should call themselves "the third row of second layer".

The teacher did remember that each layer has 12 boys and each row has 4 boys and therefore the second boy of the third row of the first layer is actually the (1 + 2*4 + 0*12) tenth boy in the queue.

That is how the plain arrays are, be it 1D, 2D, 3D, .. 42D, ..


However, if you do start making vectors of vectors of ...
you don't usually have nice, uniform, continuous queues any more.
The sea ferries. The ferries have cars. The cars have boxes and the boxes have candy, but each ferry, car, and box can be different.
ok that makes sense could you give me a program example or how this is program is written
Last edited on
thank you
for the help
just to be sure is this the right format for 3d array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;


int main()
{
	const int NUMROWS = 4;
	const int NUMCOLS = 3;
	const int NUM3D = 7;
	
	int array[NUM3D][NUMROWS][NUMCOLS]={{67,67,67,67,67,68},
	                                    {71,72,72,72,72,72},
	                                    {75,75,75,75,75,75}};
	

		for(int layer = 0; layer < NUM3D; layer ++)
	{
	cout<<endl;
		
		for(int rows = 0; rows < NUMROWS; rows++)
		{
			cout << endl; //ends the line for each row
			for(int cols = 0; cols < NUMCOLS; cols++)
			{
				cout << setw(4) << array[rows][cols][layer];
			}
		}
	}
	
	
	return 0;
}	
	
	
Last edited on
The data to the right of your assignment for your array is currently only 2D, and does not match your NUM(___) requirements.

A 2D array just means each element of the outer array has an inner array inside it.
Every element of a 3D array would be an inner array of inner-inner arrays

Also, your ordering is not exactly right
If you made your array in this order
1
2
3
4
const int a = 2;
const int b = 3;
const int c = 4;
int arr[a][b][c];


Then you should set up the for loop in the same order
1
2
3
4
for (int i = 0; i < a; i++)
    for (int j = 0; j < b; j++)
        for (int k = 0; k < c; k++)
            arr[i][j][k] = 4;



Furthermore, using multi-dimensional arrays becomes hard to keep track of. Consider using a 1D array of custom types over using multi-dim arrays
ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Point {
  double x;
  double y;
  double z;
};
// ...
Point points[100];

for (int i = 0; i < 100; i++)
{
    points[i].x = 1.1;
    points[i].y = 2.0;
    points[i].z = 3.5;
}


[/code]
Last edited on
so like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int min_array(int[], int);

int main()
{


const int NUMROWS = 7;
const int NUMCOLS = 3;
const int NUM3D = 7;
int array [NUMROWS][NUMCOLS][NUM3D] ={{{67,67,67,67,67,68,68},
				       {71,72,72,72,72,72,72},
				       {75,75,75,75,75,75,75}},
				      {{50,50,50,50,50,50,51},
				       {53,53,53,53,53,53,53},
				       {55,55,55,55,55,55,55}}}


	for(int i = 0; i < NUM3D; i ++)
	{
		
		
		for(int j = 0; j < NUMROWS; j++)
		{
			cout << endl; 
			for(int k = 0; k < NUMCOLS; k++)
			{
				cout << setw(4) << array[j][k][i];
			}
		}
	}



}
return 0;

}	



thank you for helping me
Last edited on
The ordering of your for loops is a bit unintuitive, but yes your code is working now (except that you're missing a semi-colon and have an extra curly brace before return 0)

Note that a lot of your values are going to be 0 because how how you declare your initial array values.
Last edited on
ok how can i fix my loops so it display

67 71 75
67 72 75
67 71 75
67 72 75

and not this


 67  71  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  67  72  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  67  72  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  67  72  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  67  72  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  68  72  75  50  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  68  72  75  51  53  55   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{


const int NUMROWS = 7;
const int NUMCOLS = 3;
const int NUM3D = 7;
int array [NUMROWS][NUMCOLS][NUM3D] ={{{67,67,67,67,67,68,68},
				       {71,72,72,72,72,72,72},
				       {75,75,75,75,75,75,75}},
				      {{50,50,50,50,50,50,51},
				       {53,53,53,53,53,53,53},
				       {55,55,55,55,55,55,55}}};


	for(int i = 0; i < NUM3D; i ++)
	{
		
		
		for(int j = 0; j < NUMROWS; j++)
		{
			
			for(int k = 0; k < NUMCOLS; k++)
			{
				cout << setw(4) << array[j][k][i];
			}
		}
	}




return 0;

}	
You obviously want to print newlines appropriately. Think and experiment.

It could be easier to test, if your data would have unique, identifiable values.


Your indexing is still a total mess, even more than before.
1
2
3
4
5
int     array[ LAYERS ][ ROWS ][ COLS ];

cout << array[ layer  ][ row  ][ col  ];

// &array[layer][row][col] == array[0][0] + COLS*(ROWS*layer + row) + col 
ok i fix it and thanks for the help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{


const int NUMROWS = 3;
const int NUMCOLS = 7;
const int NUM3D = 2;
int array [NUM3D][NUMROWS][NUMCOLS] ={{{67,67,67,67,67,68,68},
				       {71,72,72,72,72,72,72},
				       {75,75,75,75,75,75,75}},
				      {{50,50,50,50,50,50,51},
				       {53,53,53,53,53,53,53},
				       {55,55,55,55,55,55,55}}};




	for(int i = 0; i < NUM3D; i ++)
	{
		cout<<endl;
		
		for(int j = 0; j < NUMROWS; j++)
		{
			cout<<endl;
			for(int k = 0; k < NUMCOLS; k++)
			{
				cout << setw(4) << array[i][j][i];
			}
		}
	}




return 0;

}	
Last edited on
Topic archived. No new replies allowed.