Week 8 Multidimensional Array
Week 8 Multidimensional Array
A multidimensional array is an array of arrays. It allows you to store data in a tabular or matrix-like
structure. The most common type of multidimensional array is the 2-dimensional (2D) array, which can be
visualized as a table with rows and columns.
Example of a 2D Array:
A 2D array with 3 rows and 3 columns:
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
In this case, the 2D array matrix can be visualized as:
1 2 3
4 5 6
7 8 9
Each element is accessed using two indices: one for the row and one for the column.
Syntax:
data_type array_name[size1][size2]...[sizeN];
For a 2D array (matrix) with 3 rows and 4 columns:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
This creates a 2x3 matrix:
1 2 3
4 5 6
You can also initialize a 2D array without specifying all the elements:
int matrix[2][3] = {1, 2, 3}; // First row: {1, 2, 3}, Second row: {0, 0, 0}
Syntax:
array_name[row_index][column_index];
For example, to access the element in the 2nd row and 3rd column of the following array:
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
You would use:
int value = matrix[1][2]; // Accessing the value in the 2nd row and 3rd column (value = 6)
123
456
789
1 2 3 4 5 6