C++ Multidimensional Arrays

Arrays are a collection of elements of the same data type. In single-dimensional arrays, all elements are of the simple type like int, char, float, etc. But a two-dimensional array is a list of other arrays. Each element of a two-dimensional array is an array itself.

We can have two, three, four-dimensional arrays, and so on. They all belong to the family of multidimensional arrays.

Concept of Multidimensional Arrays

In simple words, multidimensional arrays are nested arrays. These are arrays that have arrays inside them. The simplest one of these types of arrays is a 2D (two-dimensional) array.


Single Dimensional Array

A 1D array is also called a single-dimensional array that is mostly used. Example representation:

[ 1, 2, 3, 4, 5, 6 ]

Multidimensional arrays explained

2D Array

A 2D array is an array where each element is a 1D (single-dimensional) array. Example representation:

[
    [1, 2, 3], [4, 5, 6],
]

3D Array

A 3D (three-dimensional) array represents an array of elements where each element is a 2D array. Example representation:

[
  [
    [1, 2, 3],
    [4, 5, 6]
  ],
  [
    [7, 8, 9],
    [10, 11, 12]
  ]
]

4D Array

A 4D (four-dimensional) array contains an array of 3D arrays.

This can be continued to any number of dimensions, and they all belong to the class of multidimensional arrays. Arrays of dimensions higher than 2D arrays are rarely used as it gets harder to work with and implement.


Representation of multidimensional arrays in the Memory

Inside the memory, there is no concept of 2D or 3D arrays. Multidimensional arrays are stored linearly, one element after the other, similar to single-dimensional arrays.


Introduction to 2D arrays

In this lesson, we will mostly discuss 2D arrays. They are very common and required in many cases.

Declaration of 2D Array

We can declare a 2D array as shown below:

datatype arrayname[rows][columns];

Two brackets are required to specify the number of rows and columns. The number of columns represents the size of each array that is inside the 2D array.

Example of a 2D Array declaration

int array[10][5];

The total size of the above array is

rows x columns = 10 x 5 = 50.

So it can hold a maximum of 50 integers.

Initialization of 2D Array

We can assign initial values to the 2D array similar to a 1D array:

datatype arrayname[rows][columns] = {
  /* values for nested arrays are inside individual braces */
  {
    /* column values 1 */ },
  {
    /* column values 2 */ }
};

Example of 2D Array Initialization

int array[3][2] = { {1, 2}, {3, 4}, {5, 6} };

Usage of 2D Array

We can access the values of the 2D array by specifying the value for the row index and the column index with the array name. The index value always starts at 0, so the second row will have an index of 1, and the third row will have index 2, etc. It follows the same for columns.

In the matrix below:

int array[3][2] = { {1, 12}, {3, 24}, {15, 6} };

The values of each of the elements are accessed as:

/* row one */
array[0][0] == 1;
array[0][1] == 12;
/* row two */
array[1][0] == 3;
array[1][1] == 24;
/* row three */
array[2][0] == 15;
array[2][1] == 6;

Matrices as 2D arrays

A matrix is a rectangular representation of some data in a tabular format(rows and columns). It’s widely used in Science, Research, Mathematics, and Data Structure.

by Using 2D arrays, we can represent matrices where each element of a 2D array is a row. Each row contains multiple elements that represent the columns of data in the matrix.


Traversing a 2D array

Traversing an array is necessary to perform processing on an array. It can be reading values, printing values, or updating the values. To traverse a 2D array, we need to use two loops. This is because each element is again another 1D array that needs to be traversed to access its values.

For 3D arrays, a total of three nested loops will be required. The complexity increases with the higher dimension of arrays.


Reading values for a matrix

To read values to a matrix (2D array), we need to use two loops – one nested inside the other.

int rows = 3, columns = 4;
int matrix[rows][columns];
for (int i = 0; i < rows; i++) {
  /* outer loop traverses row by row */
  for (int j = 0; j < columns; j++) {
    /* for each row, this inner loop traverses the nested array*/
    cout << "Enter value
    for row“ << i << ", column" << j;
    /* store the value in that array position */
    cin >> matrix[i][j];
  }
}

Displaying values of a matrix

We can display the values of a matrix, in the same way, using two loops and index pointers: i and j.

for (int i = 0; i < rows; i++)
  for (int j = 0; j < columns; j++) {
    cout << matrix[i][j] << ”“;
  }
cout << endl;
}

Example output of 3 x 4 (three by four/ 3 row, 4 column) matrix:

1 4 6 7
8 2 3 1
0 2 7 9

Scope of 2D arrays (matrices)

2D arrays are used as commonly as 1D arrays. There are a lot of applications in different cases. Some of them are:

Coordinate system

A coordinate system contains the x and y positions of a plane. It is used to store graph points [x,y] and also in image processing.

Games

We can represent the state of a game board in 2D arrays. A chessboard, tic tac toe board, etc, are all matrices in a row and column format.

Social Media Posts

All the posts of a particular user are stored inside one array in the database. Now each post, in turn, stores an array of comments, an array of people who liked the post, etc.


Points to Remember

  • Arrays can be classified into two types – single-dimensional (1D) arrays and multidimensional arrays (2D, 3D,…).
  • Multidimensional arrays are nested arrays. It contains an array which in turn contains other arrays.
  • A 2D(two-dimensional) array is the simplest form of a multidimensional array and is used widely.
  • To traverse a 2D array, we need to use two loops, one inside the other.
  • A 2D array represents a matrix of rows and columns.
  • Arrays of higher dimensions(3D, 4D, 5D,…) have fewer applications but follow the same structure as 2D arrays.
Back to: Learn C++ Programming > Array in C++

Leave a Reply

Your email address will not be published. Required fields are marked *