Date: 10-11-2025
Programming for Problem Solving(BE01000121)
[AY: 2025-26] [1st Semester]
Two –Dimensional Arrays and Matrix
Operations (matrix addition ,
subtraction , and multiplication)
1
Introduction to 2D Arrays
•Definition: A 2D array (two- dimensional array) is an array of arrays,
organized in rows and columns like a matrix.
•Syntax: arr[row][column]
•Declaration Example:
int arr[3][4]; // 3 rows, 4 columns
•Uses:
-Representing tables or grids
-Storing images (pixels)
-Handling data in rows and columns
•Example:
arr[0][2] = 5; // Sets the element in row 0, column 2 to 5
•Looping Through 2D Array:
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
// Process arr[i][j]
}
}
• C Program Example:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
printf("%d ", arr[i][j]); }
printf("\n");
}
return 0;
}
OUTPUT: 1 2 3
456
Matrix Operations
• Definition:
In the C programming language, a Matrix Operation is any mathematical or logical
manipulation performed on elements stored within a two-dimensional array (often referred to as
a matrix).
• Common Operations:
Addition: A + B (add corresponding elements)
Subtraction: A - B (subtract corresponding elements)
Multiplication: A × B (multiply rows by columns)
Transpose: Aᵗ (swap rows and columns)
Inverse: A⁻¹ (matrix such that A × A⁻¹ = I)
Determinant: |A| (scalar value representing matrix properties)
• Usage:
Used in mathematics, engineering, computer graphics, data science.
Addition in Matrix in C Language
• Definition: Matrix addition is performed by adding corresponding elements of two matrices
of equal size.
• Condition: Both matrices must have the same dimensions (same number of rows and
columns).
• C Program Example:
for (int i = 0; i < 2; i++) {
#include <stdio.h> for (int j = 0; j < 2; j++) {
int main() { printf("%d ", C[i][j]);
int A[2][2] = {{1, 2}, {3, 4}}; }
int B[2][2] = {{5, 6}, {7, 8}}; printf("\n");
int C[2][2]; // Resultant matrix }
for (int i = 0; i < 2; i++) { return 0;
for (int j = 0; j < 2; j++) { }
C[i][j] = A[i][j] + B[i][j];
} • OUTPUT:
} Resultant Matrix:
printf("Resultant Matrix:\n"); 6 8
10 12
Subtraction in Matrix in C Language
• Definition: Matrix subtraction is performed by subtracting corresponding elements of two
matrices of equal size.
• Condition: Both matrices must have the same dimensions (same number of rows and
columns).
• C Program Example: printf("Result of matrix subtraction:\n");
#include <stdio.h> for(int i=0; i<2; i++) {
int main() { for(int j=0; j<2; j++) {
printf("%d ", C[i][j]); }
int A[2][2] = {{5, 7}, {9, 3}}; printf("\n"); }
int B[2][2] = {{2, 4}, {5, 2}}; return 0; }
int C[2][2];
for(int i=0; i<2; i++) { • OUTPUT:
for(int j=0; j<2; j++) { Result of matrix subtraction:
33
C[i][j] = A[i][j] - B[i][j]; } } 41
Multiplication in Matrix in C Language
• Definition: Matrix multiplication is performed by multiplying rows of the first matrix by
columns of the second matrix and summing the products.
• Condition:The number of columns in the first matrix must equal the number of rows in
the second matrix.
• C Program Example:
printf("Resultant Matrix:\n");
#include <stdio.h> for (i = 0; i < 2; ++i) {
int main() { for (j = 0; j < 2; ++j)
int mat1[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d ", result[i][j]);
int mat2[3][2] = {{7, 8}, {9, 10}, {11, 12}}; printf("\n");
int result[2][2] = {0}; }
int i, j, k; return 0;
for (i = 0; i < 2; ++i){ }
for (j = 0; j < 2; ++j){ • OUTPUT:
for (k = 0; k < 3; ++k){ Resultant Matrix:
result[i][j] += mat1[i][k] * mat2[k][j];} 58 64
} 139 154
}
Conclusion
• 2D Arrays in C Language provide a powerful way to store and manage tabular data.
• They are declared as datatype arrayName[row][column]; and accessed using two
indices.
• Applications: 2D arrays are widely used for matrices, game boards, image processing,
and more.
• Understanding memory layout, traversal, and initialization is key to efficient usage.
• Efficient Manipulation: Mastering operations like searching, sorting, and arithmetic
enhances problem-solving skills.
• Matrix Operations: 2D arrays form the basis for matrix operations such as addition,
subtraction, multiplication, and transposition. These operations are widely used in
fields like mathematics, engineering, and computer science.
• Strong foundation for advanced data structures and algorithms.