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}\)
Write a C program which calculates the subtraction of two matrices of the same size.
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:
- Take the number of rows and columns as input.
- Store the number in a variable.
- Initialize the first matrix.
- Initialize the second matrix.
- Initialize the result matrix.
- Inside a loop, subtract the corresponding elements of the two matrices and store them in the result matrix.
- 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.
- Subtraction of Two Matrix in C using For Loop
- Subtraction of Two Matrix in C using Function
- Advanced Approach for Subtracting Two Matrices in C
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}\)
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.
/*
* C Program to subtract two matrices of same size using for loop
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int a[r][c], b[r][c], res[r][c];
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] - b[i][j];
}
}
printf("The result matrix is:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%6d", res[i][j]);
}
printf("\n");
}
}
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.
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
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}\)
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.
/*
* C Program to subtract two matrices of same size using function
*/
#include <stdio.h>
#include <stdlib.h>
void subtract(int **a, int **b, int **res, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] - b[i][j];
}
}
}
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int **a = (int **)malloc((unsigned) r * sizeof(int *));
int **b = (int **)malloc((unsigned) r * sizeof(int *));
int **res = (int **)malloc((unsigned) r * sizeof(int *));
for (i = 0; i < r; i++)
{
a[i] = (int *)malloc((unsigned) c * sizeof(int));
b[i] = (int *)malloc((unsigned) c * sizeof(int));
res[i] = (int *)malloc((unsigned) c * sizeof(int));
}
printf("Enter the elements of first matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
subtract(a, b, res, r, c);
printf("The result matrix is:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%6.d", res[i][j]);
}
printf("\n");
}
}
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.
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
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.
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.
/*
* C Program to subtract two matrices of same size using functions
*/
#include <stdio.h>
#include <stdlib.h>
void printMatrix(int **a, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
void initializeMatrix(int **a, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
}
void subtractMatrix(int **a, int **b, int **res, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
res[i][j] = a[i][j] - b[i][j];
}
}
}
int main(void)
{
int r, c;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
int i, j;
int **a = (int **)malloc(r * sizeof(int *));
int **b = (int **)malloc(r * sizeof(int *));
int **res = (int **)malloc(r * sizeof(int *));
for (i = 0; i < r; i++)
{
a[i] = (int *)malloc(c * sizeof(int));
b[i] = (int *)malloc(c * sizeof(int));
res[i] = (int *)malloc(c * sizeof(int));
}
printf("Enter the elements of first matrix:\n");
initializeMatrix(a, r, c);
printf("Enter the elements of second matrix:\n");
initializeMatrix(b, r, c);
subtractMatrix(a, b, res, r, c);
printf("The result matrix is:\n");
printMatrix(res, r, c);
}
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.
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”.
- Check C Books
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for C Internship