C Program to Subtract 2 Matrices

Write a Program for the Subtraction of 2 Matrices in C.

Example,

MatrixA
1 2 3
2 3 1
1 1 1

MatrixB
1 1 1 
2 2 2
1 1 1

Output
0  1  2
0  1 -1
0  0  0

The Program accepts 2 Matrices: MatrixA and MatrixB.

Steps

  1. Enter Number of Rows and Number of Columns of MatrixA.
  2. Then Enter MatrixA (row-wise).
  3. Enter Number of Rows and Number of Columns of MatrixB.
  4. Then Enter MatrixB (row-wise).
  5. The result of MatrixA – MatrixB is stored in MatrixC.

For Matrix Subtraction, the order of MatrixA and MatrixB should be same.

Matrix Subtraction can be Implemented in 2 ways

  1. Using Loops.
  2. Using Recursion.

C Program to Subtract 2 Matrices (using loops)

#include<stdio.h>

// The function subtract matrixA and matrixB
// and stores the result in matrixC
void MatrixSubtraction(int matrixA[][50], int matrixB[][50], int matrixC[][50], int r, int c) {

	int i, j;
	for (i = 0; i < r; ++i) {
		for (j = 0; j < c; ++j) {
			matrixC[i][j] = matrixA[i][j] - matrixB[i][j];
		}
	}

}

int main() {

	int i, j;
	// rA: row of matrixA cA: column of matrixA
	// rB: row of matrixB cB: column of matrixB
	int rA, cA, rB, cB;
	int matrixA[50][50], matrixB[50][50], matrixC[50][50];


	// Enter Matrix A
	printf("Enter Number of Rows in MatrixA: ");
	scanf("%d", &rA);
	printf("Enter Number of columns in MatrixA: ");
	scanf("%d", &cA);

	printf("Enter MatrixA: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cA; ++j) {
			scanf("%d", &matrixA[i][j]);
		}
	}

	// Enter Matrix B
	printf("Enter Number of Rows in MatrixB: ");
	scanf("%d", &rB);
	printf("Enter Number of columns in MatrixB: ");
	scanf("%d", &cB);

	printf("Enter MatrixB: \n");
	for (i = 0; i < rB; ++i) {
		for (j = 0; j < cB; ++j) {
			scanf("%d", &matrixB[i][j]);
		}
	}

	// checking if matrices have same order or not
	if (rA != rB || cA != cB) {
		printf("MatrixA and MatrixB are not Compatible");
	}
	else {

		// the function subtract matrixA and matrixB and stores
		// the result in matrixC
		MatrixSubtraction(matrixA, matrixB, matrixC, rA, cB);

		// Printing MatrixC
		printf("\nMatrixA - MatrixB: \n");
		for (i = 0; i < rA; ++i) {
			for (j = 0; j < cB; ++j) {
				printf("%d ", matrixC[i][j]);
			}
			printf("\n");
		}

	}

}

Output

C Program to Subtract 2 Matrices (using recursion)

#include<stdio.h>

// The function subtract matrixA and matrixB
// and stores the result in matrixC
void MatrixSubtraction(int matrixA[][50], int matrixB[][50], int matrixC[][50], int r, int c, int i, int j) {

	matrixC[i][j] = matrixA[i][j] - matrixB[i][j];

	if (i == r - 1 && j == c - 1)
		return;
	else if (j == c - 1)
		++i;

	j = (j + 1) % c;

	MatrixSubtraction(matrixA, matrixB, matrixC, r, c, i, j);

}

int main() {

	int i, j;
	// rA: row of matrixA cA: column of matrixA
	// rB: row of matrixB cB: column of matrixB
	int rA, cA, rB, cB;
	int matrixA[50][50], matrixB[50][50], matrixC[50][50];


	// Enter Matrix A
	printf("Enter Number of Rows in MatrixA: ");
	scanf("%d", &rA);
	printf("Enter Number of columns in MatrixA: ");
	scanf("%d", &cA);

	printf("Enter MatrixA: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cA; ++j) {
			scanf("%d", &matrixA[i][j]);
		}
	}

	// Enter Matrix B
	printf("Enter Number of Rows in MatrixB: ");
	scanf("%d", &rB);
	printf("Enter Number of columns in MatrixB: ");
	scanf("%d", &cB);

	printf("Enter MatrixB: \n");
	for (i = 0; i < rB; ++i) {
		for (j = 0; j < cB; ++j) {
			scanf("%d", &matrixB[i][j]);
		}
	}

	// checking if matrices have same order or not
	if (rA != rB || cA != cB) {
		printf("MatrixA and MatrixB are not Compatible");
	}
	else {

		// the function subtract matrixA and matrixB and stores
		// the result in matrixC
		MatrixSubtraction(matrixA, matrixB, matrixC, rA, cB, 0, 0);

		// Printing MatrixC
		printf("\nMatrixA - MatrixB: \n");
		for (i = 0; i < rA; ++i) {
			for (j = 0; j < cB; ++j) {
				printf("%d ", matrixC[i][j]);
			}
			printf("\n");
		}

	}

}

Output

Time Complexity: O(N2)

What to study next?

  1. Matrix Multiplication
  2. Transpose Matrix

References

Matrix Subtraction

Leave a Comment

Your email address will not be published. Required fields are marked *