A two-dimensional array stores data in rows and columns. It is commonly used to represent tables, matrices, marksheets, and other grid-like data.
A two-dimensional array is an array arranged in rows and columns.
int matrix[2][3];
This array has 2 rows and 3 columns.
Column
0 1 2
Row 0 [ ] [ ] [ ]
Row 1 [ ] [ ] [ ]
The basic syntax is:
data_type array_name[rows][columns];
Example:
int matrix[3][4];
int is the data type.matrix is the array name.3 represents rows.4 represents columns.Consider this array:
int matrix[2][3];
It contains:
Total elements can be calculated as:
rows × columns
2 × 3 = 6
Both row and column indexes start from 0.
matrix[0][0]
matrix[0][1]
matrix[0][2]
matrix[1][0]
matrix[1][1]
matrix[1][2]
The first index identifies the row and the second identifies the column.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
The first group represents the first row and the second group represents the second row.
Column
0 1 2
Row 0 10 20 30
Row 1 40 50 60
For example, matrix[1][2] contains 60.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
printf("%d", matrix[0][1]);
Output:
20
The first index selects row 0 and the second selects column 1.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
printf("%d", matrix[1][2]);
Output:
60
For a 2 × 3 array, the last valid index pair is
[1][2].
int matrix[2][2] = {
{10, 20},
{30, 40}
};
matrix[1][0] = 100;
printf("%d", matrix[1][0]);
Output:
100
A specific element can be modified using its row and column indexes.
Nested loops are commonly used to display all elements of a 2D array.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
A 2D array usually needs two loops:
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
printf("%d ", matrix[i][j]);
}
}
int matrix[2][3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &matrix[i][j]);
}
}
Each input value is stored at its corresponding row and column.
#include <stdio.h>
int main()
{
int matrix[2][3];
printf("Enter 6 numbers:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("Matrix:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
int sum = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
sum += matrix[i][j];
}
}
printf("Sum = %d", sum);
Output:
Sum = 210
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int i = 0; i < 2; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += matrix[i][j];
}
printf("Row %d Sum = %d\n", i, sum);
}
Output:
Row 0 Sum = 60
Row 1 Sum = 150
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int j = 0; j < 3; j++)
{
int sum = 0;
for (int i = 0; i < 2; i++)
{
sum += matrix[i][j];
}
printf("Column %d Sum = %d\n", j, sum);
}
Output:
Column 0 Sum = 50
Column 1 Sum = 70
Column 2 Sum = 90
int matrix[2][3] = {
{10, 70, 30},
{40, 50, 20}
};
int largest = matrix[0][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] > largest)
{
largest = matrix[i][j];
}
}
}
printf("Largest = %d", largest);
Output:
Largest = 70
int matrix[2][3] = {
{10, 70, 30},
{40, 50, 20}
};
int smallest = matrix[0][0];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] < smallest)
{
smallest = matrix[i][j];
}
}
}
printf("Smallest = %d", smallest);
Output:
Smallest = 10
Two matrices of the same dimensions can be added element by element.
int a[2][2] = {
{1, 2},
{3, 4}
};
int b[2][2] = {
{5, 6},
{7, 8}
};
int result[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
Consider:
A = 1 2
3 4
B = 5 6
7 8
The result is:
R = 6 8
10 12
Each corresponding pair of elements is added.
The transpose of a matrix changes rows into columns and columns into rows.
Original:
1 2 3
4 5 6
Transpose:
1 4
2 5
3 6
The element at matrix[i][j] becomes
transpose[j][i].
#include <stdio.h>
int main()
{
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int transpose[3][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
transpose[j][i] = matrix[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
A two-dimensional array can be passed to a function. For a fixed column size, the column dimension must be known in the parameter declaration.
void display(int matrix[][3], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
void display(int matrix[][3], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
display(matrix, 2);
return 0;
}
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
int search = 50;
int found = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (matrix[i][j] == search)
{
found = 1;
printf("Found at row %d, column %d",
i, j);
break;
}
}
if (found)
{
break;
}
}
For an array declared as:
int matrix[3][4];
The valid row indexes are:
0, 1, 2
The valid column indexes are:
0, 1, 2, 3
Therefore, the last valid element is:
matrix[2][3]
int matrix[2][3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
}
#include <stdio.h>
int main()
{
int a[2][2];
int b[2][2];
int result[2][2];
printf("Enter first matrix:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter second matrix:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
scanf("%d", &b[i][j]);
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
printf("Result:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Two-dimensional arrays are useful for many types of data:
For example, a marksheet can store students in rows and subjects in columns.
Practice the following programs:
Question: How many indexes are required to access an element of a two-dimensional array?