Updated March 17, 2023
Introduction to 3D Arrays in C++
C++ array is used to store the data in the form of a table of rows and columns. Here we can create single or multidimensional arrays to hold values in different scenarios. In C++, a 3d array is a multidimensional array used to store 3-dimensional information. In simple words, a three-dimensional array is an array of arrays. In three dimensional array, we have three rows and three columns. In this article, we will see what is three-dimensional array, the use of a three-dimensional array, how to access them and how to use effectively three-dimensional array in our code.
Working of 3D Arrays in C++
1. Usage of 3d array can be understood by taking the example of searching the word inside the book. We need three pieces of information to search for a word in a book.
- Page number.
- Line number.
- Word index or column in which word belongs.
2. In multidimensional arrays data in the form of a table, that is in row-major order. The general syntax of a 3-dimensional array is as below.
Syntax:
data_type array_name[size1][size2][size3];
3. Remember that the size is always a positive integer Below is the example of a three-dimensional array.
- Example: Here 3DArray is a three-dimensional array, having a maximum of 24 elements.
int 3DArray[2][3][4];
4. The maximum number of elements contained in an array is obtained by multiplying the size of all the dimensions.
- Example: In 3DArray[2][3][4], The maximum element is obtained by multiplying 2, 3, 4, i.e. 24.
5. Similarly 3DArray[10][10][10], can hold 1000 elements. We can visualize this as each of the 10 elements can hold 10 elements, which makes a total of 100 elements. Every 100 elements can hold another 10 elements, which makes the final count as 1000.
6. We can create a 3-dimensional array by creating a 2D array first and then extending it to the required dimension.
Initialization of a 3D Array
We Can Initialize a 3-Dimensional Array in Many Ways. Below Are the Examples for Reference.
int 3DArray[2][2][4] = {1, 3, 6, 5, 8, 9, -2, 4, 5, 10, 34, 56, 23, -56, 10, 37};
The values in the flower braces from left to right are stored inside the array as a table from left to right. The values will be filled in the array in the following order. First 4 elements from the left in the first row, next 4 elements in the second row and so on.
The above initialization won’t give us a clear picture of the array. For better visualization, we can initialize the same array as below.
int 3DArray[2][2][4] =
{
{ {1, 3, 6, 5}, {8, 9, -2, 4} },
{ {5, 10, 34, 56}, {23, -56, 10, 37} }
};
- Accessing elements in the 3D array is similar to any other array, by using the index of the element. We have to use three loops to access all the elements inside the array x[2][1][0].
- For higher dimension arrays like 4, 5, 6, etc., the concept is quite similar, but the complexity of handling the things increases. For example, the number of loops used, a number of element searches, accessing the particular element, etc.
- Elements of 3 dimensional or higher dimensional arrays can be moved around in different ways. This operation is similar to vectors and matrices. Different techniques like reshape, permute, and squeeze are used for the purpose of rearranging elements inside the array. These are the complex techniques which we need not worry for now.
Example with Steps
Now we will use these 3D arrays to understand how the arrays will work.
We will write a C++ code that will take input from the user and display the elements present in the 3-dimensional array.
1. First, we will write the main program for the execution.
#include <iostream>
using namespace std;
int main( )
{
}
2. Inside the main function, we will declare a 3-dimensional array which can store up to 16 elements.
int Array[2][2][4];
3. Now we will ask the user to enter 16 values he wants to store in the array.
cout << "Please enter 16 values of your choice: \n";
4. In order to store the values into the array we need three loops, i.e. each dimension uses one loop to traverse. We will take three indexes, i, j and k for the three dimensions. For a better understanding of the code, we will use for loop. First for loop represents the first dimension, second for loop for the second dimension and third for loop for the third dimension. Inside the third for loop, we are taking the input from the user.
for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for(int k = 0; k < 4; k++ )
{
cin >> Array[i][j][k];
}
}
}
5. Now since the values are stored in the array, it’s time for us to show the stored values to the user.
6. For this, again we are using the three for loops for traversal and this time cout for printing the values.
cout<<"\n Below are the values you have stored in the array"<< endl;
for(int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for(int k = 0; k < 4; k++)
{
cout << "[" << i << "][" << j << "][" << k << "] =" <<
Array[i][j][k] << endl;
}
}
}
Output:
Conclusion – 3D Arrays in C++
In this article, we have learned what is an array, what is single and multidimensional array, the significance of multidimensional array, how to initialize the array and using the multidimensional array in the program based on our needs.
Recommended Articles
This is a guide to 3D Arrays in C++. Here we discuss the introduction and working of 3D arrays in C++ along with example and steps. You may also look at the following articles to learn more –