Multi-dimensional Arrays
Explore the concept of multi-dimensional arrays in C++, including how to declare, access, and manipulate arrays within arrays. Learn practical examples such as matrices and exercises on summing rows and columns, enhancing your ability to handle complex data structures.
What is a multi-dimensional array
In C++, a multi-dimensional array is an array that contains other arrays as its values or members. The container array is termed an outer array, and the member array is termed an inner array. An array is said to be a multi-dimensional array if it has another array as its members. Can you think of some instances where we might have been using a structure same as a multi-dimensional array? One such example is a crossword puzzle!
Individual values in multi-dimensional arrays
The individual values in a multi-dimensional array are accessed through multiple index numbers used in the following format:
arrayname[row number][column number];
Let’s take the example of the following array:
int array1[2][3]= {
{10,12,14},
{16,18,20}
};
-
The
array1is a two-dimensional array. -
The value
20can be accessed asarray1 [ 1 ] [ 2 ]. -
The number of indexes depends on the level of dimensions.
Note: The size of the outer array is optional while declaring a two-dimensional array. The size of the inner array is mandatory.
The general concepts of two-dimensional arrays, or n-dimensional arrays are implemented as follows in C++:
The following illustration shows the structure of the array above:
And here’s another example of the structure of a two-dimensional array:
The following illustration shows the structure of the array above:
And here’s another example of a two-dimensional array with character elements:
The following illustration shows the structure of the above array.
A mathematical matrix is implemented as a two-dimensional array in C++, as shown in the following example:
The variable matrixA is an array of three elements, and each element is itself an array of four elements.
The following code demonstrates a way to write values in the forms of rows and columns. It only adds convenience to the reader of the program. The output will remain the same.
The result of the second program is exactly the same as the first program. Inside the computer’s memory, both variables (matrixA and matrixB) are stored in the same way as linear sequences. Now, say we want to add these two matrices and store them into another matrix (matrixC).
The resulting matrix isn’t a sum of values of operands. We need to access the individual integer values in each matrix to produce their sum.
Practice multi-dimensional arrays
The following are a few example programs to practice using multi-dimensional arrays in C++. By clicking the “Show Solution” button, you can find a program that solves the respective problem. You may copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.
Calculate the sum of each row in a matrix
Write a program to show the sum of each row of the matrix. The matrix must have two rows and three columns.
Sample input 1
{{10,20,30},
{40,50,60}}
Sample output 1
Matrix:
10 20 30
40 50 60
Sum of Row 1:60
Sum of Row 2:150
Sample input 2
{{1,2,3},
{4,5,6}}
Sample output 2
Matrix:
1 2 3
4 5 6
Sum of Row 1:6
Sum of Row 2:15
Calculate the sum of each column in a matrix
Write a program to show a matrix in the form of rows and columns, then show the sum of each column. The matrix must have five rows and ten columns.
Sample input
{{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,43,44,45,46,47,48,49}}
Sample output
Displaying in matrix form:
row# 0 ==> 0 1 2 3 4 5 6 7 8 9
row# 1 ==> 10 11 12 13 14 15 16 17 18 19
row# 2 ==> 20 21 22 23 24 25 26 27 28 29
row# 3 ==> 30 31 32 33 34 35 36 37 38 39
row# 4 ==> 40 41 42 43 44 45 46 47 48 49
Column Sums are :100 105 110 115 120 125 130 135 140 145