C++ Three Dimensional Array Program

« Previous Program Next Program »

Three Dimensional Array Program in C++

Three dimensional (3D) array contains three for loops in C++ programming. So, to initialize and print three dimensional array, you have to use three for loops. Third for loop (the innermost loop) forms 1D array, Second for loop forms 2D array and the third for loop (the outermost loop) forms 3D array, as shown here in the following program.

C++ Programming Code for Three Dimensional (3D) Array

A three dimensional (3D) array can be thought of as an array of arrays of arrays. Following is a simple C++ program to initialize three-dimensional (3D) array of dimensions 3*4*2, then it will access some elements present in the array and display the element on the screen :

/* C++ Program - Three Dimensional Array Program */
void main()
	int arr[3][4][2] = {
				 {2, 4},
				 {7, 8},
				 {3, 4},
				 {5, 6}
				 {7, 6},
				 {3, 4},
				 {5, 3},
				 {2, 3}
				 {8, 9},
				 {7, 2},
				 {3, 4},
				 {5, 1}
	cout<<"arr[0][0][0] = "<<arr[0][0][0]<<"\n";
	cout<<"arr[0][2][1] = "<<arr[0][2][1]<<"\n";
	cout<<"arr[2][3][1] = "<<arr[2][3][1]<<"\n";

When the above C++ program is compile and executed, it will produce the following result:

c++ three dimensional array program

Here, the outer array has three elements, each of which is a 2-D array of four 1-D arrays, each of which contains two integers. It means, a 1-D array of two elements is constructed first. Then four such 1-D arrays are placed one below the other to give a 2-D array containing four rows. Then, three such 2-D arrays are placed one behind the other to yield a 3-D array containing three 2-D arrays.

Same Program in Other Language

You may like the same program in other programming languages:

« Previous Program Next Program »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test