0
0
CProgramBeginner · 2 min read

C Program to Subtract Two Matrices with Output

To subtract two matrices in C, use nested 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

InputMatrix1: [[1, 2], [3, 4]] Matrix2: [[4, 3], [2, 1]]
OutputResult: [[-3, -1], [1, 3]]
InputMatrix1: [[5, 5, 5], [5, 5, 5]] Matrix2: [[1, 2, 3], [4, 5, 6]]
OutputResult: [[4, 3, 2], [1, 0, -1]]
InputMatrix1: [[0, 0], [0, 0]] Matrix2: [[0, 0], [0, 0]]
OutputResult: [[0, 0], [0, 0]]
🧠

How to Think About It

To subtract two matrices, think of them as grids of numbers. You take each number from the first grid and subtract the matching number in the second grid. This means you go row by row and column by column, subtracting elements at the same position to get the result matrix.
📐

Algorithm

1
Get the number of rows and columns for the matrices.
2
Input the elements of the first matrix.
3
Input the elements of the second matrix.
4
For each position in the matrices, subtract the second matrix element from the first matrix element.
5
Store the result in a new matrix.
6
Print the resulting matrix.
💻

Code

c
#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;
}
Output
Resultant matrix after subtraction: -3 -1 1 3
🔍

Dry Run

Let's trace subtracting matrix2 from matrix1 with values [[1, 2], [3, 4]] and [[4, 3], [2, 1]].

1

Initialize matrices

matrix1 = [[1, 2], [3, 4]], matrix2 = [[4, 3], [2, 1]]

2

Subtract element at (0,0)

result[0][0] = 1 - 4 = -3

3

Subtract element at (0,1)

result[0][1] = 2 - 3 = -1

4

Subtract element at (1,0)

result[1][0] = 3 - 2 = 1

5

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)14-3
(0,1)23-1
(1,0)321
(1,1)413
💡

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

User Input Matrices
c
#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;
}
Allows dynamic matrix size and user input but requires more code and input handling.
In-place Subtraction
c
#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;
}
Subtracts directly into the first matrix to save space but overwrites original data.

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.

ApproachTimeSpaceBest For
Separate Result MatrixO(rows * cols)O(rows * cols)Clear output without modifying inputs
In-place SubtractionO(rows * cols)O(1)Saving memory when input modification is allowed
User Input MatricesO(rows * cols)O(rows * cols)Flexible matrix sizes and user-driven data
💡
Always ensure both matrices have the same dimensions before subtracting.
⚠️
Beginners often forget to match matrix sizes, causing incorrect results or runtime errors.