C Program to Subtract Two Matrices

Matrix Subtraction in C is used to subtract two matrices. i.e. calculate and print the difference of them.

Example:

Given two matrices of same size, this program will subtract the corresponding elements of each matrix and print the result.

Input:
First Matrix: \(\begin{bmatrix}
4 & 7 & 9\\
8 & 5 & 6
\end{bmatrix}\)

Second Matrix: \(\begin{bmatrix}
1 & 2 & 3\\
8 & 7 & 9
\end{bmatrix}\)

Output:
\(\begin{bmatrix}
4 & 7 & 9\\
8 & 5 & 6
\end{bmatrix}\) – \(\begin{bmatrix}
1 & 2 & 3\\
8 & 7 & 9
\end{bmatrix}\) = \(\begin{bmatrix}
4-1 & 7-2 & 9-3\\
8-8 & 5-7 & 6-9
\end{bmatrix}\) = \(\begin{bmatrix}
3 & 5 & 6\\
0 & -2 & -3\end{bmatrix}\)

Problem Description

Write a C program which calculates the subtraction of two matrices of the same size.

Problem Solution
advertisement
advertisement

In order to subtract two matrices, we need to subtract the corresponding elements of each matrix. Suppose we have two matrices of size m x n and p x q.

Algorithm:

  1. Take the number of rows and columns as input.
  2. Store the number in a variable.
  3. Initialize the first matrix.
  4. Initialize the second matrix.
  5. Initialize the result matrix.
  6. Inside a loop, subtract the corresponding elements of the two matrices and store them in the result matrix.
  7. Print the result matrix.

There are several ways to subtract two matrices in C language. Let’s take a detailed look at all the approaches to matrix subtraction in C.

Method 1: Subtraction of Two Matrix in C using For Loop (Naive Approach)
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

In this approach, we’ll use a for loop to loop through the number of rows and columns and subtract the corresponding elements of the two matrices and store them in the result matrix.

Example:

Input:

First Matrix: \(\begin{bmatrix}
34 & 65 & 12 & 43\\
15 & 97 & 35 & 21\\
4 & 77 & 10 & 46\\
39 & 62 & 36 & 24
\end{bmatrix}\) and Second Matrix: \(\begin{bmatrix}
10 & 35 & 92 & 48\\
59 & 27 & 19 & 38\\
59 & 3 & 47 & 99\\
37 & 59 & 86 & 18
\end{bmatrix}\)

Output:

\(\begin{bmatrix}
34 & 65 & 12 & 43\\
15 & 97 & 35 & 21\\
4 & 77 & 10 & 46\\
39 & 62 & 36 & 24
\end{bmatrix}\)– \(\begin{bmatrix}
10 & 35 & 92 & 48\\
59 & 27 & 19 & 38\\
59 & 3 & 47 & 99\\
37 & 59 & 86 & 18
\end{bmatrix}\) = \(\begin{bmatrix}
34-10 & 65-35 & 12-92 & 43-48\\
15-59 & 97-27 & 35-19 & 21-38\\
4-59 & 77-3 & 10-47 & 46-99\\
39-37 & 62-59 & 36-86 & 24-18
\end{bmatrix}\)

= \(\begin{bmatrix}
24 & 30 & -80 & -5\\
-44 & 70 & 16 & -17\\
-55 & 74 & -37 & -53\\
2 & 3 & -50 & 6\\
\end{bmatrix}\)
Program/Source Code
advertisement

Here is source code of the C program to subtract two matrices using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to subtract two matrices of same size using for loop 
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. int main(void)
  9. {
  10.     int r, c;
  11.     printf("Enter the number of rows: ");
  12.     scanf("%d", &r);
  13.     printf("Enter the number of columns: ");
  14.     scanf("%d", &c);
  15.     int i, j;
  16.     int a[r][c], b[r][c], res[r][c];
  17.     printf("Enter the elements of first matrix:\n");
  18.     for (i = 0; i < r; i++)
  19.     {
  20.         for (j = 0; j < c; j++)
  21.         {
  22.             printf("[%d][%d]: ", i, j);
  23.             scanf("%d", &a[i][j]);
  24.         }
  25.     }
  26.     printf("Enter the elements of second matrix:\n");
  27.     for (i = 0; i < r; i++)
  28.     {
  29.         for (j = 0; j < c; j++)
  30.         {
  31.             printf("[%d][%d]: ", i, j);
  32.             scanf("%d", &b[i][j]);
  33.         }
  34.     }
  35.     for (i = 0; i < r; i++)
  36.     {
  37.         for (j = 0; j < c; j++)
  38.         {
  39.             res[i][j] = a[i][j] - b[i][j];
  40.         }
  41.     }
  42.     printf("The result matrix is:\n");
  43.     for (i = 0; i < r; i++)
  44.     {
  45.         for (j = 0; j < c; j++)
  46.         {
  47.             printf("%6d", res[i][j]);
  48.         }
  49.         printf("\n");
  50.     }
  51. }
Program Explanation

1. The program will ask the user to enter the number of rows and columns and will return the result matrix.
2. The program will use a for loop through the number of rows and columns and subtract the corresponding elements of the two matrices and store them in the result matrix.
3. Print the result matrix.

Time complexity: O(n2)
Time complexity of this algorithm is O(n2). For r rows and c columns, for initialization, we go through rc elements. For subtraction, we go through rc elements. So, the time complexity is O(r*c) which is roughly O(n2) for comparable values of r and c.

Space Complexity: O(n2)
Space complexity of this algorithm is O(n2). For r rows and c columns, we need r*c elements to store the result matrix.

Runtime Test Cases
advertisement

In this case, we enter “4” for the number of rows and “4” for the number of columns as input for the subtraction of two matrices.

Enter the number of rows: 4
Enter the number of columns: 4
Enter the elements of first matrix:
[0][0]: 34
[0][1]: 65
[0][2]: 12
[0][3]: 43
[1][0]: 15
[1][1]: 97
[1][2]: 35
[1][3]: 21
[2][0]: 4
[2][1]: 77
[2][2]: 10
[2][3]: 46
[3][0]: 39
[3][1]: 62
[3][2]: 36
[3][3]: 24
Enter the elements of second matrix:
[0][0]: 10
[0][1]: 35
[0][2]: 92
[0][3]: 48
[1][0]: 59
[1][1]: 27
[1][2]: 19
[1][3]: 38
[2][0]: 59
[2][1]: 3 
[2][2]: 47
[2][3]: 99
[3][0]: 37
[3][1]: 59
[3][2]: 86
[3][3]: 18
The result matrix is:
    24    30   -80    -5
   -44    70    16   -17
   -55    74   -37   -53
     2     3   -50     6

Method 2: Subtraction of Two Matrix in C using Function

In this approach, we’ll use a function to subtract the corresponding elements of the two matrices and store them in the result matrix.

Method used:
subtract(int **, int **, int **, int, int): This function subtracts the corresponding elements of the two matrices and stores them in the result matrix.

Example:

Input:
First Matrix: \(\begin{bmatrix}
23 & 654\\
35 & 87\\
46 & 25
\end{bmatrix}\) and Second Matrix: \(\begin{bmatrix}
4 & 75\\
32 & 78\\
21 & 11
\end{bmatrix}\)

Output:
\(\begin{bmatrix}
23 & 654\\
35 & 87\\
46 & 25
\end{bmatrix}\) – \(\begin{bmatrix}
4 & 75\\
32 & 78\\
21 & 11
\end{bmatrix}\) = \(\begin{bmatrix}
23-4 & 654-75\\
35-32 & 87-78\\
46-21 & 25-11
\end{bmatrix}\) = \(\begin{bmatrix}
19 & 579\\
3 & 9\\
25 & 14
\end{bmatrix}\)

Program/Source Code

Here is source code of the C program to subtract two matrices using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to subtract two matrices of same size using function
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void subtract(int **a, int **b, int **res, int r, int c)
  9. {
  10.     int i, j;
  11.     for (i = 0; i < r; i++)
  12.     {
  13.         for (j = 0; j < c; j++)
  14.         {
  15.             res[i][j] = a[i][j] - b[i][j];
  16.         }
  17.     }
  18. }
  19.  
  20. int main(void)
  21. {
  22.     int r, c;
  23.     printf("Enter the number of rows: ");
  24.     scanf("%d", &r);
  25.     printf("Enter the number of columns: ");
  26.     scanf("%d", &c);
  27.     int i, j;
  28.     int **a = (int **)malloc((unsigned) r * sizeof(int *));
  29.     int **b = (int **)malloc((unsigned) r * sizeof(int *));
  30.     int **res = (int **)malloc((unsigned) r * sizeof(int *));
  31.     for (i = 0; i < r; i++)
  32.     {
  33.         a[i] = (int *)malloc((unsigned) c * sizeof(int));
  34.         b[i] = (int *)malloc((unsigned) c * sizeof(int));
  35.         res[i] = (int *)malloc((unsigned) c * sizeof(int));
  36.     }
  37.     printf("Enter the elements of first matrix:\n");
  38.     for (i = 0; i < r; i++)
  39.     {
  40.         for (j = 0; j < c; j++)
  41.         {
  42.             printf("[%d][%d]: ", i, j);
  43.             scanf("%d", &a[i][j]);
  44.         }
  45.     }
  46.     printf("Enter the elements of second matrix:\n");
  47.     for (i = 0; i < r; i++)
  48.     {
  49.         for (j = 0; j < c; j++)
  50.         {
  51.             printf("[%d][%d]: ", i, j);
  52.             scanf("%d", &b[i][j]);
  53.         }
  54.     }
  55.     subtract(a, b, res, r, c);
  56.     printf("The result matrix is:\n");
  57.     for (i = 0; i < r; i++)
  58.     {
  59.         for (j = 0; j < c; j++)
  60.         {
  61.             printf("%6.d", res[i][j]);
  62.         }
  63.         printf("\n");
  64.     }
  65. }
Program Explanation

1. The program will ask the user to enter the number of rows and columns.
2. Then it will initialize the first matrix, initialize the second matrix and initialize the result matrix.
3. Here, the memory allocation is done a bit differently. We’ll use malloc to allocate the memory for the two matrices and the result matrix.
4. First we allocate the rows of the three matrices and then we allocate the columns.
5. The program will then call the function to subtract the corresponding elements of the two matrices and store them in the result matrix.
6. In the function, we’ll loop through the number of rows and columns and subtract the corresponding elements of the two matrices and store them in the result matrix.
7. Print the result matrix.

Time complexity: O(n2)
Time complexity of this algorithm is O(n2). For r rows and c columns, for initialization, we go through rc elements. For subtraction, we go through rc elements. So, the time complexity is O(r*c) which is roughly O(n2) for comparable values of r and c.

Space Complexity: O(n2)
Space complexity of this algorithm is O(n2). For r rows and c columns, we need r*c elements to store the result matrix.

Runtime Test Cases

In this case, we enter “3” for the number of rows and “2” for the number of columns as input for the subtraction of two matrices.

Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of first matrix:
[0][0]: 23
[0][1]: 654
[1][0]: 35
[1][1]: 87
[2][0]: 46
[2][1]: 25
Enter the elements of second matrix:
[0][0]: 4
[0][1]: 75
[1][0]: 32
[1][1]: 78
[2][0]: 21
[2][1]: 11
The result matrix is:
    19   579
     3     9
    25    14

Method 3: Advanced Approach for subtracting Two Matrices in C

In this approach, we’ll use functions to subtract the corresponding elements of the two matrices and store them in the result matrix.

Methods used:

  • printMatrix(int **, int, int): This function will print the matrix.
  • initializeMatrix(int **, int, int): This function will initialize the matrix.
  • subtractMatrix(int **, int **, int **, int, int): This function will subtract the corresponding elements of the two matrices and store them in the result matrix.
Program/Source Code

Here is source code of the C program to subtract two matrices using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to subtract two matrices of same size using functions
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void printMatrix(int **a, int r, int c)
  9. {
  10.     int i, j;
  11.     for (i = 0; i < r; i++)
  12.     {
  13.         for (j = 0; j < c; j++)
  14.         {
  15.             printf("%d ", a[i][j]);
  16.         }
  17.         printf("\n");
  18.     }
  19. }
  20.  
  21. void initializeMatrix(int **a, int r, int c)
  22. {
  23.     int i, j;
  24.     for (i = 0; i < r; i++)
  25.     {
  26.         for (j = 0; j < c; j++)
  27.         {
  28.             printf("[%d][%d]: ", i, j);
  29.             scanf("%d", &a[i][j]);
  30.         }
  31.     }
  32. }
  33.  
  34. void subtractMatrix(int **a, int **b, int **res, int r, int c)
  35. {
  36.     int i, j;
  37.     for (i = 0; i < r; i++)
  38.     {
  39.         for (j = 0; j < c; j++)
  40.         {
  41.             res[i][j] = a[i][j] - b[i][j];
  42.         }
  43.     }
  44. }
  45.  
  46. int main(void)
  47. {
  48.     int r, c;
  49.     printf("Enter the number of rows: ");
  50.     scanf("%d", &r);
  51.     printf("Enter the number of columns: ");
  52.     scanf("%d", &c);
  53.     int i, j;
  54.     int **a = (int **)malloc(r * sizeof(int *));
  55.     int **b = (int **)malloc(r * sizeof(int *));
  56.     int **res = (int **)malloc(r * sizeof(int *));
  57.     for (i = 0; i < r; i++)
  58.     {
  59.         a[i] = (int *)malloc(c * sizeof(int));
  60.         b[i] = (int *)malloc(c * sizeof(int));
  61.         res[i] = (int *)malloc(c * sizeof(int));
  62.     }
  63.     printf("Enter the elements of first matrix:\n");
  64.     initializeMatrix(a, r, c);
  65.     printf("Enter the elements of second matrix:\n");
  66.     initializeMatrix(b, r, c);
  67.     subtractMatrix(a, b, res, r, c);
  68.     printf("The result matrix is:\n");
  69.     printMatrix(res, r, c);
  70. }
Program Explanation

1. The program begins with asking the number of rows and columns and then initializes the matrices using the function initializeMatrix.
2. The program then calls the subtractMatrix function to subtract the corresponding elements of the two matrices and store them in the result matrix.
3. Print the result matrix using the function printMatrix().

Time complexity: O(n2)
Time complexity of this algorithm is O(n2). For r rows and c columns, for initialization, we go through rc elements. For subtraction, we go through rc elements. So, the time complexity is O(r*c) which is roughly O(n2) for comparable values of r and c.

Space Complexity: O(n2)
Space complexity of this algorithm is O(n2). For r rows and c columns, we need r*c elements to store the result matrix.

Runtime Test Cases

In this case, we enter “3” for the number of rows and “2” for the number of columns as input for the subtraction of two matrices.

Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of first matrix:
[0][0]: 43
[0][1]: 76
[1][0]: 26
[1][1]: 56
[2][0]: 12
[2][1]: 62
Enter the elements of second matrix:
[0][0]: 12
[0][1]: 67
[1][0]: 42
[1][1]: 87
[2][0]: 45
[2][1]: 24
The result matrix is:
    31      9 
   -16    -31 
   -33     38

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.