C++ Multidimensional Arrays

ht‮pt‬s://www.theitroad.com

C++ allows you to create multidimensional arrays, which are essentially arrays of arrays. Here's an example of creating a 2D array:

int myArray[3][4] = {
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10, 11, 12 }
};

In this example, we're creating a 2D array of integers with 3 rows and 4 columns. The array is initialized with values using a nested set of braces. The first set of braces contains the values for the first row, the second set of braces contains the values for the second row, and so on.

To access an element of a 2D array, you use two sets of square brackets: one for the row index and one for the column index. Here's an example of accessing an element of a 2D array:

int myArray[3][4] = {
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10, 11, 12 }
};

int value = myArray[1][2]; // value is now 7

In this example, we're accessing the element in the second row (index 1) and third column (index 2) of the array, which has the value 7.

You can also modify elements of a 2D array using the same syntax:

int myArray[3][4] = {
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10, 11, 12 }
};

myArray[1][2] = 20; // the element at row 1, column 2 is now 20

In this example, we're modifying the element in the second row (index 1) and third column (index 2) of the array, changing its value from 7 to 20.