A multidimensional array is an array of arrays.
The most common type is a 2D array.
Think of a 2D array as a table with rows and columns.
Each element is accessed by two indices:
[row][column].
Analogy:
Imagine a chessboard. Each cell has a row and column. That’s exactly how a 2D array works.
Declaration of 2D Arrays
Syntax:
data_type→ type of elements (int, float, char, etc.)rows→ number of rowscolumns→ number of columns
Examples:
Initializing 2D Arrays
First row →
{1, 2, 3}Second row →
{4, 5, 6}
Accessing Elements
Use two indices:
[row][column]
Example 1: Print 2D Array
Output:
Example 2: Sum of All Elements
Example 3: Row-wise Sum
Output:
Example 4: Column-wise Sum
Output:
Example 5: Matrix Addition
C becomes:
Example 6: Matrix Subtraction
C becomes:
Example 7: Matrix Multiplication (2×2)
C becomes:
Example 8: Transpose of a Matrix
Transpose:
Example 9: Diagonal Elements of a Square Matrix
Example 10: Check Symmetric Matrix
A matrix is symmetric if
matrix[i][j] == matrix[j][i]
Extra Related Points About 2D Arrays
Accessing Elements:
[row][column]Memory Storage: Stored row-wise in memory (contiguous)
Looping: Nested loops are commonly used to process rows and columns
Initialization: Can partially initialize; others default to
0Passing to Functions: Arrays can be passed by reference or pointer