Introduction to Multi-Dimensional Arrays in C++
Multi-Dimensional Arrays in C++ arrays are 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. Single dimensional has one dimension whereas, a multidimensional array can be 2 dimensional, 3 dimensional, etc. We can think of the multidimensional array as an array of arrays. Here the data is stored in tabular form. In this article, we will see what is a multi-dimensional array, the use of a multi-dimensional array, how to access them and how to use effectively multi-dimensional array in our code.
An Element of Multidimensional Array in C++
- Let us consider the example of a matrix to understand the multidimensional array. In a 2D matrix, there will be rows and columns. In order to represent this, we use a 2D dimensional array.
- In multidimensional arrays data in the form of a table, that is in row-major order. The general syntax of a 2-dimensional array is as below.
data_type array_name[size1][size2];
- Remember that the size is always a positive integer value. Below is the example of a three-dimensional array.
int matrix[3][5];
Here matrix is a two-dimensional array, having a maximum of 15 elements.
- The maximum number of elements contained in an array is obtained by multiplying the size of all the dimensions. For example in 3DArray[2][3][4], the maximum element is obtained by multiplying 2, 3, 4, i.e. 24.
- Similarly matrix[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.
- We can create a multi-dimensional array by creating a simpler array first and then extending it to the required dimension.
Initialization of A Multidimensional Array
Lets us take 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 multidimensional 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. Below is an example of accessing an element in a 3D 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, the 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.
Examples of Multi-Dimensional Arrays in C++
Here are some examples of Multi-Dimensional Arrays in C++ given below with steps:
Example #1
Now we will use these 2D arrays to understand how the multi-dimensional arrays will work. We will write a C++ code that will take input from the user for two matrices, add them and display the result of the addition of the matrices. First, we will write the main program for the execution.
Inside the main function, we will declare two 2 dimensional arrays that can store up to 4 elements.
Now we will ask the user to enter 4 values for each array.
In order to store the values into the array we need two loops, i.e. each dimension uses one loop to traverse. We will take two indexes, i and j for the two 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. Inside the second for loop, we are taking the input from the user.
Now take input for the second array in a similar way.
Now since the values are stored in each array, it’s time for us to show the addition of two arrays to the user. For this, again we are using the two for loops for traversal and this time cout for printing the values.
Output:
Example #2
We declare an array of any number of elements and use them to perform different functions like addition, subtraction, multiplication, inverse, transform, etc. For subtraction below code can be used:
Chose different inputs and check the outputs and tally them mathematically.
Output:
Conclusion
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 Multi-Dimensional Arrays in C++. Here we discuss syntax, elements, and initialization of multidimensional array in C++ along with examples and steps. You may also look at the following articles to learn more-