C Program to Add Two Matrices with Output and Explanation
for loops to sum corresponding elements and store them in a result matrix, like result[i][j] = matrix1[i][j] + matrix2[i][j];.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> int main() { int rows = 2, cols = 2; int matrix1[2][2] = {{1, 2}, {3, 4}}; int matrix2[2][2] = {{5, 6}, {7, 8}}; int result[2][2]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } printf("Resultant Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", result[i][j]); } printf("\n"); } return 0; }
Dry Run
Let's trace adding two 2x2 matrices [[1,2],[3,4]] and [[5,6],[7,8]] through the code.
Initialize matrices
matrix1 = [[1,2],[3,4]], matrix2 = [[5,6],[7,8]], result = [[0,0],[0,0]]
Add element at position (0,0)
result[0][0] = 1 + 5 = 6
Add element at position (0,1)
result[0][1] = 2 + 6 = 8
Add element at position (1,0)
result[1][0] = 3 + 7 = 10
Add element at position (1,1)
result[1][1] = 4 + 8 = 12
Print result matrix
Print rows: [6 8] and [10 12]
| Position | matrix1[i][j] | matrix2[i][j] | result[i][j] |
|---|---|---|---|
| (0,0) | 1 | 5 | 6 |
| (0,1) | 2 | 6 | 8 |
| (1,0) | 3 | 7 | 10 |
| (1,1) | 4 | 8 | 12 |
Why This Works
Step 1: Matrix element-wise addition
Each element in the result matrix is the sum of elements from the same position in the two input matrices using result[i][j] = matrix1[i][j] + matrix2[i][j];.
Step 2: Use nested loops
Nested for loops let us visit every row and column index to perform addition systematically.
Step 3: Store and print results
The sums are stored in a new matrix and then printed row by row to show the final added matrix.
Alternative Approaches
#include <stdio.h> int main() { int rows, cols; printf("Enter rows and columns: "); scanf("%d %d", &rows, &cols); int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols]; printf("Enter elements of first matrix:\n"); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) scanf("%d", &matrix1[i][j]); printf("Enter elements of second matrix:\n"); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) scanf("%d", &matrix2[i][j]); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) result[i][j] = matrix1[i][j] + matrix2[i][j]; printf("Resultant Matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) printf("%d ", result[i][j]); printf("\n"); } return 0; }
#include <stdio.h> int main() { int matrix1[2][2] = {{1, 2}, {3, 4}}; int matrix2[2][2] = {{5, 6}, {7, 8}}; int rows = 2, cols = 2; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { matrix1[i][j] += matrix2[i][j]; } } printf("Resultant Matrix (in-place):\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", matrix1[i][j]); } printf("\n"); } return 0; }
Complexity: O(rows * cols) time, O(rows * cols) space
Time Complexity
The program uses nested loops over rows and columns, so it processes each element once, resulting in O(rows * cols) time.
Space Complexity
It requires extra space for the result matrix of the same size, so space complexity is O(rows * cols). In-place addition can reduce space.
Which Approach is Fastest?
All approaches have similar time complexity; in-place addition saves space but overwrites data, while dynamic input adds flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed size with separate result | O(rows * cols) | O(rows * cols) | Simple, clear code |
| Dynamic input size | O(rows * cols) | O(rows * cols) | Flexible matrix sizes |
| In-place addition | O(rows * cols) | O(1) | Memory efficient, no original data needed |