C Program to Subtract Two Matrices with Output
for loops to subtract corresponding elements and store the result in a new 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] = {{4, 3}, {2, 1}}; 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 after subtraction:\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 subtracting matrix2 from matrix1 with values [[1, 2], [3, 4]] and [[4, 3], [2, 1]].
Initialize matrices
matrix1 = [[1, 2], [3, 4]], matrix2 = [[4, 3], [2, 1]]
Subtract element at (0,0)
result[0][0] = 1 - 4 = -3
Subtract element at (0,1)
result[0][1] = 2 - 3 = -1
Subtract element at (1,0)
result[1][0] = 3 - 2 = 1
Subtract element at (1,1)
result[1][1] = 4 - 1 = 3
| Position (i,j) | matrix1[i][j] | matrix2[i][j] | result[i][j] |
|---|---|---|---|
| (0,0) | 1 | 4 | -3 |
| (0,1) | 2 | 3 | -1 |
| (1,0) | 3 | 2 | 1 |
| (1,1) | 4 | 1 | 3 |
Why This Works
Step 1: Access each element
The program uses nested for loops to go through each row and column of the matrices.
Step 2: Subtract corresponding elements
At each position, it subtracts the element of the second matrix from the first using result[i][j] = matrix1[i][j] - matrix2[i][j];.
Step 3: Store and print result
The result is stored in a new matrix and printed row by row to show the subtraction output.
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] = {{5, 6}, {7, 8}}; int matrix2[2][2] = {{1, 2}, {3, 4}}; 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 after in-place subtraction:\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 time grows with the number of elements, making it O(rows * cols).
Space Complexity
It uses extra space for the result matrix of the same size, so space complexity is O(rows * cols).
Which Approach is Fastest?
In-place subtraction saves space but modifies original data; using a separate result matrix is safer and clearer.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Separate Result Matrix | O(rows * cols) | O(rows * cols) | Clear output without modifying inputs |
| In-place Subtraction | O(rows * cols) | O(1) | Saving memory when input modification is allowed |
| User Input Matrices | O(rows * cols) | O(rows * cols) | Flexible matrix sizes and user-driven data |