Definition: A two-dimensional array is an array arranged in the form of rows and columns. It is commonly used to represent tables and matrices.
Definition: A two-dimensional array is an array arranged in the form of rows and columns. It is commonly used to represent tables and matrices.
int marks[3][4];
creates a 2-D integer array with 3 rows and
4 columns.
A classroom marks table can be represented using a 2-D array, where rows represent students and columns represent subjects.
Column
0 1 2
┌────┬────┬────┐
Row 0 │ 10 │ 20 │ 30 │
├────┼────┼────┤
Row 1 │ 40 │ 50 │ 60 │
├────┼────┼────┤
Row 2 │ 70 │ 80 │ 90 │
└────┴────┴────┘
Rows represent the horizontal entries of the array, while columns represent the vertical entries.
int matrix[3][4]:Definition: A 2-D array is declared by specifying the data type, array name, number of rows and number of columns.
data_type array_name[rows][columns];
int matrix[2][3];
declares an integer array with 2 rows and 3 columns.
Definition: Initialization means assigning values to the elements of a 2-D array when it is declared.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
Definition: An element of a 2-D array is accessed using two indexes: one for the row and one for the column.
array_name[row][column]
int matrix[2][3] = {{10,20,30},{40,50,60}};matrix[0][0] → 10matrix[0][2] → 30matrix[1][1] → 50
Column Index
0 1 2
┌────┬────┬────┐
Row 0 │ 10 │ 20 │ 30 │
├────┼────┼────┤
Row 1 │ 40 │ 50 │ 60 │
└────┴────┴────┘
matrix[0][0] = 10
matrix[0][1] = 20
matrix[0][2] = 30
matrix[1][0] = 40
matrix[1][1] = 50
matrix[1][2] = 60
A 2-D array is commonly processed using nested loops. The outer loop controls rows and the inner loop controls columns.
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
printf("%d ", matrix[i][j]);
}
}
Definition: Input for a 2-D array can be taken element by element using nested loops.
scanf("%d", &matrix[i][j]);
stores the entered value at row i and column
j.
Definition: A 2-D array can be displayed row by row by using nested loops.
printf("
"); moves to the
next row.
Write a C program to accept a 2 × 3 matrix from the user and display the matrix in row and column form.
Use two nested loops. The outer loop should handle rows and the inner loop should handle columns.
A 2-D array stores data in rows and columns. Nested loops provide a convenient way to process every element of the matrix.
#include <stdio.h>
int main()
{
int matrix[2][3];
int i, j;
printf("Enter 6 elements: ");
// read matrix elements row by row
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
scanf("%d", &matrix[i][j]);
}
printf("Matrix:
");
// display matrix row by row
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
printf("%d ", matrix[i][j]);
printf("
");
}
return 0;
}
Enter 6 elements: 10 20 30 40 50 60 Matrix: 10 20 30 40 50 60
Definition: A particular element of a 2-D array can be changed by assigning a new value to its row and column index.
matrix[1][2] = 100;
matrix[1][2] originally contains 60, the above
statement changes it to 100.
Definition: Traversing a 2-D array means visiting every element row by row and column by column.
Definition: Matrix addition is performed by adding corresponding elements of two matrices having the same dimensions.
Write a C program to accept two 2 × 2 matrices and calculate their sum.
Read both matrices using nested loops. Add corresponding elements and store the result in a third matrix.
Two matrices can be added when they have the same number of rows and columns. Each element of the first matrix is added to the element at the same position in the second matrix.
#include <stdio.h>
int main()
{
int a[2][2], b[2][2], sum[2][2];
int i, j;
printf("Enter elements of first matrix: ");
// read first matrix
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
}
printf("Enter elements of second matrix: ");
// read second matrix
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
scanf("%d", &b[i][j]);
}
// add corresponding elements
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
sum[i][j] = a[i][j] + b[i][j];
}
printf("Sum of matrices:
");
// display the result matrix
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
printf("%d ", sum[i][j]);
printf("
");
}
return 0;
}
Enter elements of first matrix: 1 2 3 4 Enter elements of second matrix: 5 6 7 8 Sum of matrices: 6 8 10 12
| 1-D Array | 2-D Array |
|---|---|
| Uses one index. | Uses two indexes. |
| Usually represents a linear list. | Usually represents rows and columns. |
Example: int a[5]; |
Example: int a[3][4]; |
| Processed using one loop. | Usually processed using nested loops. |
| Mistake | Correct Understanding |
|---|---|
| Starting indexes from 1 | Both row and column indexes start from 0. |
| Using wrong row/column limit | For [3][4], rows are 0–2 and columns are 0–3. |
| Using one loop only | Nested loops are normally used for complete traversal. |
Confusing matrix[i][j] |
First index represents row, second represents column. |
matrix[rows][columns], valid row indexes are
0 to rows - 1 and column indexes are
0 to columns - 1.| Concept | Remember |
|---|---|
| 2-D Array | Data arranged in rows and columns. |
| Declaration | data_type name[rows][columns]; |
| Access | array[row][column] |
| Indexing | Starts from 0 for both dimensions. |
| Traversal | Usually done with nested loops. |
| Matrix Addition | Add corresponding elements. |
int a[3][4]?int a[3][4]?Watch a beginner-friendly explanation of 2-D arrays, rows, columns and matrix operations in C.
A short handwritten-style revision sheet for 2-D arrays, indexing and matrix operations will be provided here.
Use the mind map for quick revision of rows, columns, indexing, traversal and matrix addition.