Learn to use arrays of arrays in C++.
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
array1
is a two-dimensional array. -
The value
20
can 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++:
#include <iostream>using namespace std;int main(){int array[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}}; // Declaring a 2-dimensional arrayfor (int i = 0; i < 3; i++) // Printing a 2-dimensional array{for (int j = 0; j < 3; j++){cout << array[i][j] << " ";}cout << endl;}return 0;}
The following illustration shows the structure of the array above:
And here’s another example of the structure of a two-dimensional array:
#include <iostream>using namespace std;int main(){int array[3][2] = {{1,2}, {4,5}, {7,8}};for (int i = 0; i < 3; i++){for (int j = 0; j < 2; j++){cout << array[i][j] << " ";}cout << endl;}return 0;}
The following illustration shows the structure of the array above:
And here’s another example of a two-dimensional array with character elements:
#include <iostream>using namespace std;int main(){char array[3][2] = {{'A','B'}, {'C','D'}, {'E','F'}};for (int i = 0; i < 3; i++){for (int j = 0; j < 2; j++){cout << array[i][j] << " ";}cout << endl;}return 0;}
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:
#include <iostream>using namespace std;int main(){int matrixA[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; // Declaring a matrixcout << "Matrix A: " << endl;for (int i = 0; i < 3; i++) // Printing a matrix{for (int j = 0; j < 4; j++){cout << matrixA[i][j] << "\t";}cout << endl;}return 0;}
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.
#include <iostream>#include <string>using namespace std;int main(){int rows = 3;int cols = 4;int matrixB[rows][cols] = {{10,12,13,14},{15,16,17,18},{19,20,21,22}}; // Declaring a matrixcout << "Matrix B: " << endl;for (int i = 0; i < 3; i++) // Printing a matrix{for (int j = 0; j < 4; j++){cout << matrixB[i][j] << "\t";}cout << endl;}return 0;}
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
).
#include <iostream>#include <string>using namespace std;int main(){int matrixA[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; // Declaring a matrix Aint matrixB[3][4] = {{10,12,13,14},{15,16,17,18},{19,20,21,22}}; // Declaring a matrix Bint matrixC[3][4]; // Declaring a matrix Ccout << "Matrix A: " << endl;for (int i = 0; i < 3; i++) // Printing a matrix{for (int j = 0; j < 4; j++){cout << matrixA[i][j] << "\t";}cout << endl;}cout << "Matrix B: " << endl;for (int i = 0; i < 3; i++) // Printing a matrix{for (int j = 0; j < 4; j++){cout << matrixB[i][j] << "\t";}cout << endl;}for (int i = 0; i < 3; i++) // adding two matrices{for (int j = 0; j < 4; j++){matrixC[i][j] = matrixA[i][j] + matrixB[i][j];}}// Printing the resultant matrixcout << "Matrix C: " << endl;for (int i = 0; i < 3; i++){for (int j = 0; j < 4; j++){cout << matrixC[i][j] << "\t";}cout << endl;}return 0;}
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
#include <iostream>using namespace std;int main(){// Write your code herereturn 0;}
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
#include <iostream>using namespace std;int main(){// Write your code herereturn 0;}