C Program for Matrix Subtraction

Matrix Subtraction

C programs to subtract two matrices have been shown here. Two matrices $[A]_{m \times n}$ and $[B]_{p \times q}$ are considered for subtraction if the number of rows and columns are same in both of the matrices i.e. $m = p$ and $n = q$.

Algorithm, pseudocode and time complexity of the program have also been shown.






1. Algorithm for matrix Subtraction


1. Take two matrice $A_{m\times n}$ and $B_{p\times q}$

2. Check if $m=p$ and $n=q$

3. If step [2] is false then print "Subtraction not possible" and exit program

4. If step [2] is true, then

5. Subtract each corresponding element of $B$ from $A$

6. Print the result and exit program




2. Pseudocode for matrix Subtraction


Input: Two matrices $A_{m\times n}$ and $B_{p\times q}$

Output: $A - B$

1. Procedure matrixSubtraction($A_{m\times n}$, $B_{p\times q}$):

2. If $m==p$ and $n==q$:

3. Repeat for each $i \in m$ and $j \in n$

4. $C_{ij}\leftarrow A_{ij} - B_{ij}$

5. Return $C$

6. Else:

7. Return Subtraction Not Possible

8. End Procedure





3. Time Complexity for matrix Subtraction


Time Complexity: O($mn$)

Where $m$ is the number of rows and $n$ is the number of columns in the matrices.




4. C Program for matrix Subtraction

Code has been copied
/************************************
        alphabetacoder.com
   C Program for Matrix Subtraction
*************************************/
#include <stdio.h>

int main() {
    // declare variables
    int m, n, p, q, i, j;
    int A[10][10] = {0}, B[10][10] = {0};

    //take input of the order of first matrix
    printf("Enter the number of row and column of first matrix=");
    scanf("%d%d", & m, & n);

    //take input of the first matrix
    printf("Enter the first matrix of order %d x %d=\n", m, n);
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            scanf("%d", & A[i][j]);

    //take input of the order of second matrix
    printf("Enter the number of row and column of second matrix=");
    scanf("%d%d", & p, & q);

    //take input of the first matrix
    printf("Enter the second matrix of order %d x %d=\n", p, q);
    for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
            scanf("%d", & B[i][j]);

    // check if order of matrices are same
    // if not then subtraction of two matrices is not possible
    if (m != p || n != q)
        printf("\nMatrices are of different order,hence subtraction is not possible");
    else {
        //subtract corresponding element of
        //  B from A and print the result
        printf("The resultant matrix after subtraction:\n");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++)
                printf("%d ", A[i][j] - B[i][j]);
            printf("\n");
        }
    }
    return 0;
}

Output


Case 1:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

3 5 7

4 5 9

0 1 1

Enter the number of row and column of second matrix =3 3

Enter the second matrix of order 3 x 3 =

9 4 5

3 7 8

1 2 3

The resultant matrix after Subtraction:

-6 1 2

1 -2 1

-1 -1 -2



Case 2:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3=

3 5 7

4 5 9

0 1 1

Enter the number of row and column of second matrix = 2 3

Enter the second matrix of order 2 x 3=

9 4 5

3 7 8


Matrices are of different order,hence Subtraction is not possible





5. C Program for matrix Subtraction using recursion

Code has been copied
/***********************************************
            Alphabetacoder.com
C program for matrix subtraction using recursion
************************************************/
#include <stdio.h>

#define MAXSIZE 10

// Declare a recursive function to subtract two matrices
// This function takes two matices (A and B), Sizes of both matrices (m, n)
// and index of current row & column as  parameters
// Elementwise subtraction is stored in B
int subtract_matrices(int A[][MAXSIZE], int B[][MAXSIZE], int m, int n, int row, int col) {

    // check if all columns are traversed
    // if yes the set col = 0 and increment row
    if (col >= n) {
        row++;
        col = 0;
    }

    // check if all rows are traversed
    if (row >= m)
        return 1;

    // Subtract elements of B from corresponding elements of A
    // Store the result in matrix B
    B[row][col] = A[row][col] - B[row][col];
    // return the call of recursive function 
    // to consider the next element
    return subtract_matrices(A, B, m, n, row, col + 1);
}

int main() {
    // declare variables
    int m, n, p, q, flag = 0, i, j;
    int A[MAXSIZE][MAXSIZE] = {0}, B[MAXSIZE][MAXSIZE] = {0};

    //take input of the order of first matrix
    printf("Enter the number of row and column of first matrix = ");
    scanf("%d%d", & m, & n);

    //take input of the first matrix
    printf("Enter the first matrix of order %d x %d = \n", m, n);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", & A[i][j]);
        }
    }
    //take input of the order of second matrix
    printf("Enter the number of row and column of second matrix = ");
    scanf("%d%d", & p, & q);

    //take input of the first matrix
    printf("Enter the second matrix of order %d x %d = \n", p, q);
    for (i = 0; i < p; i++) {
        for (j = 0; j < q; j++) {
            scanf("%d", & B[i][j]);
        }
    }

    // check if order of matrices are same
    // if same then subtract B matrix from A
    if (m != p || n != q) {
        printf("\nMatrices are of different order, hence subtraction is not possible");
        flag = 1;
    } else {
        // subtract B matrix from A by calling a recursive function
        subtract_matrices(A, B, m, n, 0, 0);
        // Display the B matrix as result
        printf("The resultant matrix after subtraction:\n");
        for (i = 0; i < p; i++) {
        	for (j = 0; j < q; j++) {
            	printf("%d ",  B[i][j]);
            }
            // new line
            printf("\n");
        }
    }

    return 0;
}

Output


Case 1:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

5 3 2

1 4 8

9 6 5

Enter the number of row and column of second matrix =3 3

Enter the second matrix of order 3 x 3 =

2 3 1

1 3 6

1 2 3

The resultant matrix after Subtraction:

3 0 1

0 1 2

8 4 2



Case 2:

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3=

2 5 4

4 6 9

3 1 5

Enter the number of row and column of second matrix = 2 2

Enter the second matrix of order 2 x 2=

8 4

3 3


Matrices are of different order,hence Subtraction is not possible