Multidimensional Arrays

Multi-dimensional arrays in C++ are arrays with more than one dimension. They allow you to store and access data in a tabular or matrix-like format. Common examples of multi-dimensional arrays include 2D arrays (matrices) and 3D arrays (cubes or grids).

It's important to note that the size of each dimension must be specified at the time of declaration. Multi-dimensional arrays are stored in contiguous memory locations, with the elements arranged row by row in row-major order.

Multi-dimensional arrays can be useful when dealing with structured data or when representing concepts that have multiple dimensions, such as matrices, grids, images, and more.

A multi-dimensional array can be understood as an array of arrays. To simplify, let's consider it in terms of a two-dimensional array, which is the most common type:

  1. Grid or Table: The easiest way to imagine a two-dimensional array is as a grid or table. Think of it like a chessboard or a spreadsheet with rows and columns. Each spot on the grid (like each cell in a spreadsheet) can hold a value.

  2. Array of Arrays: In a two-dimensional array, each element in the main array is itself an array. So, if you have an array of 3 elements, and each of these elements is an array of 4 elements, you can think of it as a table with 3 rows and 4 columns.

  3. Accessing Elements: To access an element in this array, you specify two indexes: one for the row and one for the column. For example, array[2][3] would refer to the element in the 3rd row and 4th column.

  4. Beyond Two Dimensions: While two-dimensional arrays (like tables) are common, you can have arrays with more dimensions. For instance, a three-dimensional array might be visualized as a cube, where you need three coordinates to access a specific element.

  5. Applications: Multi-dimensional arrays are used in various applications, like representing matrices in mathematics, storing data in tabular form, or even in computer graphics for pixel representation.

Multi-dimensional arrays are like tables or grids where you need multiple pieces of information (like row and column) to locate an item within them. They are arrays that contain other arrays as their elements.

The basic form of declaring a two-dimensional array of size x, y: Syntax: 

data_type array_name[x][y];

 Here, data_type is the type of data to be stored.

We can declare a two-dimensional integer array say ‘x’ of size 10,20 as: 

int x[10][20];

Elements in two-dimensional arrays are commonly referred to by x[i][j] where i is the row number and ‘j’ is the column number.

A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1) and the column number ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:

Initializing Two – Dimensional Arrays: There are various ways in which a Two-Dimensional array can be initialized. 

First Method

int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

The above array has 3 rows and 4 columns. The elements in the braces from left to right are stored in the table also from left to right. The elements will be filled in the array in order, the first 4 elements from the left in the first row, the next 4 elements in the second row, and so on.

Second Method

Third Method:

Example:

  1. The code begins by including the necessary header file <iostream> and using the std namespace for convenience.

  2. The main() function serves as the entry point of the program.

  3. The two-dimensional array myMultiArray is declared and initialized with values. It has 3 rows and 2 columns. The values inside the curly braces represent the elements of the array.

  4. A nested for loop is used to iterate over each element of the two-dimensional array. The outer loop controls the rows, and the inner loop controls the columns.

  5. Inside the loop, the program prints the element's value by accessing it using the indices myMultiArray[row][col]. The row and column indices are also printed to indicate the position of each element.

  6. After printing the value of each element, the program exits the loop and returns 0 to indicate successful execution.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark