What are 3D Arrays in C++, and why are they used?
An array in C++ can have “n” different dimensions. For 3D arrays, this “n” is replaced with a “3” i.e.; a 3D array has three different dimensions in which it stores the elements. This can be represented by the following syntax:
Here, “D1, D2, and D3” represent the size of the three dimensions of a 3D array.
Now, coming towards the question of why the 3D arrays are used in C++? Well, the concept of 3D arrays proves to be useful when you want to have three different pieces of information to access your particular block of data. You will be able to understand this statement in a better way by going through the analogy discussed in the section below.
The Analogy of Searching a Word within a Book or a Dictionary
Whenever you want to search for a word within a book or a dictionary, you always require three different parameters, i.e., the exact page to which that word belongs, the row or line to which that word belongs, and the column to which that word belongs. If you have all of these three parameters in hand, only then will you be able to access that particular word. You can map the three dimensions of a 3D array onto these three parameters, i.e., you can think as if each of these three parameters corresponds to a particular dimension of a 3D array. Therefore, you need 3D arrays in C++ whenever you encounter situations similar to this.
What is the size of a 3D Array in C++?
It is very simple to calculate the total size of a 3D array in C++; however, before doing so, we would like to elaborate on the meaning of “total size”. Here, the size represents the capacity of the 3D array to hold the elements in it, or in other words, you can say that the total elements of a 3D array refer to the total size of that 3D array. Now, to calculate the total size of a 3D array, you simply need to multiply the individual sizes of all three of its dimensions. You will get the total number of elements that this 3D array is capable of holding. For example, if we have an array “array”, then the size of this array will be “24” because 2 x 3 x 4 = 24. In the same manner, you can find out the size of any of your 3D arrays.
Using the 3D Arrays in C++
To learn the usage of 3D arrays in C++ in Ubuntu 20.04, you will have to go through the following sample C++ program we have implemented for you. In this sample program, we will teach you the method of declaring a 3D array in C++, taking the values of this array as an input from the user at runtime and then displaying those values index-wise on the terminal.
We will try to explain this whole code to you while breaking it down into smaller chunks to make it easier for you to understand. First, we have included the necessary library and the namespace, after which we have our “main()” function. Then, within the body of this driver function, we have declared a 3D array named “arr”. According to this declaration, the sizes of the first and second dimensions of our array are “2” and that of the third dimension is “4”. It means that the array thus declared will be able to store a total of 16 elements in it or in other words, the size of our declared array is 16 since 2 x 2 x 4 = 16.
After declaring this array, we wanted to take its elements as input from the user for which we have first displayed a message. Then, we have a nested “for” loop in which the outer-most loop is for the first dimension, the middle loop is for the second dimension, and the inner-most loop is for the third dimension of the array. We have used three different iterators for all three of these loops, and the terminating conditions for each loop depend upon the size of each particular dimension of the array.
Then, within these nested “for” loops, we have taken the values as input from the user by using the “cin>>arr[i][j][k]” statement. After that, we have again printed a message on the terminal for displaying the elements of the array on the terminal. Again, we have a nested “for” loop and use it the same way we did for taking the values as input from the user. The only difference is that we use a “cout” statement to display these values on the terminal within our nested “for” loop this time. The whole program terminates with a “return 0” statement.
Then, for the compilation of this C++ code, we have executed the command shown below after saving it:
To execute this compiled program, we ran the following command in the terminal:
Upon executing this code, we were asked to enter the 16 elements of this array as shown in the image below:
We entered 16 different values as the elements of this array, as shown in the following image:
As soon as we pressed the Enter key after entering these 16 elements, all the 16 values were displayed on the terminal index-wise as shown in the image below:
This article was all about the 3D arrays in C++ in Ubuntu 20.04. We began with a brief discussion on the importance of arrays in C++, followed by the significance of 3D arrays in particular. Then, to explain the importance of 3D arrays in a better way, we discussed a real-life analogy with you. After that, we elaborated on the way of calculating the size of 3D arrays in C++. Finally, we shared a basic example of declaring and using a 3D array in C++ with you. Once you understand this example and all the associated concepts explained in this article, you will surely get a good command of the usage of the 3D arrays in C++ in Ubuntu 20.04.