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 array_name[rows][columns];
  • data_type → type of elements (int, float, char, etc.)

  • rows → number of rows

  • columns → number of columns

Examples:

 
int matrix[3][3]; // 3x3 integer matrix
float temperatures[2][5]; // 2x5 float matrix
char letters[2][4]; // 2x4 char matrix

Initializing 2D Arrays

 
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
  • First row → {1, 2, 3}

  • Second row → {4, 5, 6}

Accessing Elements

  • Use two indices: [row][column]

 
cout << matrix[0][1]; // Output: 2
matrix[1][2] = 10; // Change element at 2nd row, 3rd column

Example 1: Print 2D Array

 
#include <iostream>
using namespace std;
int main() {
int matrix[2][3] = {{1,2,3},{4,5,6}};
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout << matrix[i][j] << " ";
cout << endl;
}
return 0;
}

Output:

 
1 2 3
4 5 6

Example 2: Sum of All Elements

 
int sum = 0;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
sum += matrix[i][j];
cout << "Sum = " << sum; // Output: Sum = 21

Example 3: Row-wise Sum

 
for(int i=0;i<2;i++){
int rowSum = 0;
for(int j=0;j<3;j++)
rowSum += matrix[i][j];
cout << "Sum of row " << i+1 << " = " << rowSum << endl;
}

Output:

 
Sum of row 1 = 6

Sum of row 2 = 15

Example 4: Column-wise Sum

 
for(int j=0;j<3;j++){
int colSum = 0;
for(int i=0;i<2;i++)
colSum += matrix[i][j];
cout << "Sum of column " << j+1 << " = " << colSum << endl;
}

Output:

 
Sum of column 1 = 5

Sum of column 2 = 7
Sum of column 3 = 9

Example 5: Matrix Addition

 
int A[2][2] = {{1,2},{3,4}};
int B[2][2] = {{5,6},{7,8}};
int C[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
C[i][j] = A[i][j] + B[i][j];

C becomes:

 
6  8
10 12

Example 6: Matrix Subtraction

 
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
C[i][j] = B[i][j] - A[i][j];

C becomes:

 
4 4
4 4

Example 7: Matrix Multiplication (2×2)

 
int A[2][2]={{1,2},{3,4}};
int B[2][2]={{5,6},{7,8}};
int C[2][2] = {0};
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
C[i][j] += A[i][k]*B[k][j];

C becomes:

 
19 22
43 50

Example 8: Transpose of a Matrix

 
int rows=2, cols=3;
int matrix[2][3]={{1,2,3},{4,5,6}};
int transpose[3][2];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
transpose[j][i]=matrix[i][j];

Transpose:

 
1 4
2 5
3 6

Example 9: Diagonal Elements of a Square Matrix

 
int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<3;i++)
cout << matrix[i][i] << " "; // Output: 1 5 9

Example 10: Check Symmetric Matrix

  • A matrix is symmetric if matrix[i][j] == matrix[j][i]

 
int matrix[2][2]={{1,2},{2,1}};
bool symmetric=true;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
if(matrix[i][j]!=matrix[j][i])
symmetric=false;
cout << (symmetric ? "Symmetric" : "Not Symmetric"); // Output: Symmetric

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 0

  • Passing to Functions: Arrays can be passed by reference or pointer