Multidimensional Arrays in c++ language

Multidimensional Arrays in c++ language

Just as C++ supports higher-dimensional vectors, it also supports multidimensional arrays. The following statement:

int a[2][3];

declares a to be a two-dimensional (2D) array of integers. In this case, the declaration specifies that array a contains two rows and three columns. shows the logical structure of the array created by the following sequence of code:

int a[2][3]; // a is a 2D array
a[0][0] = 5;
a[0][1] = 19;
a[0][2] = 3;
a[1][0] = 22;
a[1][1] = -8;
a[1][2] = 10;

The two-dimensional array a is said to be a 2×3 array, meaning it has two rows and three columns. Rows are arranged horizontally, and the values in columns are arranged vertically. In each of the assignment statements above, for example

a[1][0] = 22;

Using a syntax similar to 2D vectors, we could have declared and created the 2D array above as:

int a[2][3] = { { 5, 19, 3 },
{ 22, -8, 10 } };

To access an element of a 2D array, use two subscripts:

a[r] = 4; // Assign element at row r, column c
std::cout << a[m][n] << '\n'; // Display element at row m, column n

The following function prints the contents of a ROWS × COLUMNS 2D array of doubles, where both ROWS and COLUMNS are constants:

void print(const double m[ROWS][COLUMNS]) 
{
    for (int row = 0; row < ROWS; row++) 
      {
        for (int col = 0; col < COLUMNS; col++)
            std::cout << std::setw(5) << m[row][col];
            std::cout << '\n';
      }
}

We can omit the ROW size in the parameter declaration, but second set of square brackets must constant a constant integral expression. The declaration of the parameter m is quite complicated, and, as we did for 2D vectors, we can simplify the syntax by using a C++ type aliasing statement. statement

using Matrix = double[ROWS][COLUMNS];
#include <iostream>

#include <iomanip>

const int ROWS = 3,
    COLUMNS = 5;
// The name Matrix represents a new type
// that means a ROWS x COLUMNS
// two-dimensional array of double-precision
// floating-point numbers.
using Matrix = double[ROWS][COLUMNS];
// Allow the user to enter the elements of a matrix
void populate_matrix(Matrix m) {
    std::cout << "Enter the " << ROWS << " rows of the matrix.\n";
    for (int row = 0; row < ROWS; row++) {
        std::cout << "Row #" << row << " (enter " << COLUMNS << " values):";
        for (int col = 0; col < COLUMNS; col++)
            std::cin >> m[row][col];
    }
}
// We declare m constant because printing a matrix should not
// change it.

void print_matrix(const Matrix m) {
    for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLUMNS; col++)
            std::cout << std::setw(5) << m[row][col];
        std::cout << '\n';
    }
}

int main() {
    // Declare the 2D array
    Matrix mat;
    // Populate the array
    populate_matrix(mat);
    // Print the array
    print_matrix(mat);
}

An expression that uses just one index with a 2D array represents a single row within the 2D array. This row is itself a 1D array. Thus, if a is a 2D array and i is an integer, then the expression

a[i]