Two-Dimensional Arrays in C (2D Arrays)
A 2D Array stores data in rows and columns, similar to a matrix or table.
It is essentially an array of 1D arrays.
Declaration of 2D Array
data_type array_name[rows][cols];Example:
int matrix[3][3];
Initialization of 2D Array
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Or continuous:
int a[2][3] = {1, 2, 3, 4, 5, 6};
Practical Example 1 – Print Matrix
#include <stdio.h>
int main() {
int a[2][3] = { {1,2,3}, {4,5,6} };
for(int i = 0; i < 2; i++){
for(int j = 0; j < 3; j++){
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Practical Example 2 – Sum of All Elements
#include <stdio.h>
int main() {
int m[3][3] = {
{2, 4, 6},
{1, 3, 5},
{7, 9, 8}
};
int sum = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
sum += m[i][j];
}
}
printf("Total Sum = %d", sum);
return 0;
}
Practical Example 3 – Sum of Each Row
#include <stdio.h>
int main() {
int a[3][3] = {
{5,1,3},
{2,7,4},
{6,9,8}
};
for(int i = 0; i < 3; i++){
int sum = 0;
for(int j = 0; j < 3; j++){
sum += a[i][j];
}
printf("Row %d sum = %d\n", i+1, sum);
}
return 0;
}
Practical Example 4 – Sum of Each Column
#include <stdio.h>
int main() {
int a[3][3] = {
{3,5,7},
{1,4,6},
{8,2,9}
};
for(int col = 0; col < 3; col++){
int sum = 0;
for(int row = 0; row < 3; row++){
sum += a[row][col];
}
printf("Column %d sum = %d\n", col+1, sum);
}
return 0;
}
Practical Example 5 – Print Diagonal Elements
#include <stdio.h>
int main() {
int m[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
printf("Main diagonal: ");
for(int i = 0; i < 3; i++){
printf("%d ", m[i][i]);
}
printf("\nSecondary diagonal: ");
for(int i = 0; i < 3; i++){
printf("%d ", m[i][2-i]);
}
return 0;
}
Practical Example 6 – Transpose of Matrix
#include <stdio.h>
int main() {
int m[2][3] = {
{1,2,3},
{4,5,6}
};
printf("Transpose:\n");
for(int col = 0; col < 3; col++){
for(int row = 0; row < 2; row++){
printf("%d ", m[row][col]);
}
printf("\n");
}
return 0;
}
Practical Example 7 – Matrix Addition
#include <stdio.h>
int main() {
int a[2][2] = { {1,2}, {3,4} };
int b[2][2] = { {5,6}, {7,8} };
int sum[2][2];
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum matrix:\n");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Practical Example 8 – Matrix Multiplication (2x2)
#include <stdio.h>
int main() {
int a[2][2] = { {1,2}, {3,4} };
int b[2][2] = { {2,0}, {1,2} };
int res[2][2];
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
res[i][j] = 0;
for(int k = 0; k < 2; k++){
res[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Result matrix:\n");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
printf("%d ", res[i][j]);
}
printf("\n");
}
return 0;
}
Practical Example 9 – Count Even Numbers in Matrix
#include <stdio.h>
int main() {
int m[3][3] = {
{2,3,4},
{5,6,7},
{8,9,10}
};
int count = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(m[i][j] % 2 == 0)
count++;
}
}
printf("Even count = %d", count);
return 0;
}
Practical Example 10 – Search Element in 2D Array
#include <stdio.h>
int main() {
int m[3][3] = {
{4,7,1},
{3,9,2},
{8,6,5}
};
int key = 9, found = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(m[i][j] == key){
found = 1;
break;
}
}
}
if(found)
printf("Found");
else
printf("Not Found");
return 0;
}
Practice Questions
- Create a 3×3 matrix and print it.
- Find the largest element in a 2D array.
- Calculate row-wise and column-wise sums.
- Transpose a 4×4 matrix.
- Search an element in a matrix.
Practice Task
Create two 3×3 matrices and perform:
✔ Addition
✔ Subtraction
✔ Multiplication
Print all results clearly.