- What is matrix addition and its conditions
- How to add two matrices using nested loops
- How to handle matrices with different dimensions
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to add two matrices.
Matrix addition is a simple operation where we add corresponding elements of two matrices. It is a fundamental operation in linear algebra and is used in many applications.
š” Key Point: Matrix addition is possible only when both matrices have the same dimensions (same number of rows and columns).
Matrix Addition Rule
- Matrix A: Dimensions
r Ć c - Matrix B: Dimensions
r Ć c(must match) - Result Matrix C: Dimensions
r Ć c - Formula:
C[i][j] = A[i][j] + B[i][j]
Formula:
C[i][j] = A[i][j] + B[i][j]
Each element of the result matrix is the sum of corresponding elements from A and B.
Visual Example
Matrix A (2Ć3):
[1 2 3] [4 5 6]
Matrix B (2Ć3):
[7 8 9] [10 11 12]
Result C (2Ć3):
[8 10 12] [14 16 18]
Calculation: C[0][0] = 1 + 7 = 8
C[0][1] = 2 + 8 = 10
C[0][2] = 3 + 9 = 12
C[1][0] = 4 + 10 = 14
C[1][1] = 5 + 11 = 16
C[1][2] = 6 + 12 = 18
Matrix addition is used in many real-world applications, such as:
- Image Processing: Adding two images (overlay)
- Data Analysis: Combining datasets
- Physics: Adding vectors and matrices
- Computer Graphics: Combining transformation matrices
C Program for Matrix Addition
#include <stdio.h>
int main() {
int rows, cols, i, j;
// Ask user for matrix dimensions
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare matrices
int A[rows][cols], B[rows][cols], C[rows][cols];
// ====== READ FIRST MATRIX ======
printf("\nEnter %d elements for first matrix:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("A[%d][%d] = ", i, j);
scanf("%d", &A[i][j]);
}
}
// ====== READ SECOND MATRIX ======
printf("\nEnter %d elements for second matrix:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("B[%d][%d] = ", i, j);
scanf("%d", &B[i][j]);
}
}
// ====== MATRIX ADDITION ======
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// ====== DISPLAY MATRICES ======
printf("\n=== First Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("\n=== Second Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
printf("\n=== Result Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output
Enter the number of rows: 2 Enter the number of columns: 3 Enter 6 elements for first matrix: A[0][0] = 1 A[0][1] = 2 A[0][2] = 3 A[1][0] = 4 A[1][1] = 5 A[1][2] = 6 Enter 6 elements for second matrix: B[0][0] = 7 B[0][1] = 8 B[0][2] = 9 B[1][0] = 10 B[1][1] = 11 B[1][2] = 12 === First Matrix (2Ć3) === 1 2 3 4 5 6 === Second Matrix (2Ć3) === 7 8 9 10 11 12 === Result Matrix (2Ć3) === 8 10 12 14 16 18
Example with Square Matrices:
Enter the number of rows: 3 Enter the number of columns: 3 Enter 9 elements for first matrix: A[0][0] = 1 A[0][1] = 2 A[0][2] = 3 A[1][0] = 4 A[1][1] = 5 A[1][2] = 6 A[2][0] = 7 A[2][1] = 8 A[2][2] = 9 Enter 9 elements for second matrix: B[0][0] = 9 B[0][1] = 8 B[0][2] = 7 B[1][0] = 6 B[1][1] = 5 B[1][2] = 4 B[2][0] = 3 B[2][1] = 2 B[2][2] = 1 === First Matrix (3Ć3) === 1 2 3 4 5 6 7 8 9 === Second Matrix (3Ć3) === 9 8 7 6 5 4 3 2 1 === Result Matrix (3Ć3) === 10 10 10 10 10 10 10 10 10
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int rows, cols;ā dimensions of the matricesint i, j;ā loop counters
- Declare Matrices:
int A[rows][cols];ā first matrixint B[rows][cols];ā second matrixint C[rows][cols];ā result matrix
- Read Matrices: Uses nested loops to read elements into both matrices.
- Matrix Addition: The nested loops add corresponding elements and store the result in
C[i][j]. - Display Matrices: Prints all three matrices.
- Return:
return 0;indicates successful program execution.
š Note: The matrices must have the same dimensions for addition to work. If the dimensions differ, the program will not produce correct results.
Algorithm for Matrix Addition
Step-by-step algorithm:
- Start
- Read the number of rows and columns
- Read elements of first matrix (A)
- Read elements of second matrix (B)
- For
i = 0torows-1:- For
j = 0tocols-1:C[i][j] = A[i][j] + B[i][j]
- For
- Print the result matrix
- End
Adding Matrices with Dimension Validation
A more robust program checks if the matrices have the same dimensions before performing addition:
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Check if addition is possible
if(r1 != r2 || c1 != c2) {
printf("\nā Matrix addition not possible!\n");
printf("Both matrices must have the same dimensions.\n");
return 1;
}
int A[r1][c1], B[r2][c2], C[r1][c1];
// Read matrices (code omitted for brevity)
// ... read A and B
// Add matrices
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// Display result
printf("\n=== Result Matrix ===\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output (Error Case):
Enter rows and columns of first matrix: 2 3 Enter rows and columns of second matrix: 2 4 ā Matrix addition not possible! Both matrices must have the same dimensions.
Time and Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Reading Matrices | O(r Ć c) | O(1) |
| Matrix Addition | O(r Ć c) | O(1) |
| Overall | O(r Ć c) | O(r Ć c) |
š» Practice Exercise
Challenge 1: Write a program to add two matrices using functions (create a function for matrix addition).
Challenge 2: Add three matrices together.
š Click to Show Solution for Challenge 1
#include <stdio.h>
void addMatrices(int A[][10], int B[][10], int C[][10], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
void displayMatrix(int matrix[][10], int rows, int cols) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter rows and columns: ");
scanf("%d %d", &rows, &cols);
int A[10][10], B[10][10], C[10][10];
printf("\nEnter elements of first matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%d", &A[i][j]);
}
}
printf("\nEnter elements of second matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
scanf("%d", &B[i][j]);
}
}
addMatrices(A, B, C, rows, cols);
printf("\nResult Matrix:\n");
displayMatrix(C, rows, cols);
return 0;
}
Frequently Asked Questions
1. What is the condition for matrix addition?
Both matrices must have the same dimensions (same number of rows and columns). If A is m Ć n, then B must also be m Ć n.
2. How do you add two matrices in C?
Use nested loops to iterate through each element and add corresponding elements: C[i][j] = A[i][j] + B[i][j].
3. What is the time complexity of matrix addition?
The time complexity is O(r Ć c), where r is the number of rows and c is the number of columns.
4. Is matrix addition commutative?
Yes, matrix addition is commutative. A + B = B + A. It is also associative: (A + B) + C = A + (B + C).
5. Can we add a matrix to itself?
Yes, adding a matrix to itself is the same as multiplying it by 2. For example, A + A = 2A.
š” Tip: Always check that the matrices have the same dimensions before performing addition. Otherwise, the operation is not defined.