Subtract two matrices with C.
#include <stdio.h>
#define SIZE 3 // Size of the matrix
int main()
{
int A[3][3]; // Matrix 1
int B[3][3]; // Matrix 2
int C[3][3]; // Resultant matrix
int row, col;
/* Input elements in first matrix */
printf("Enter elements in matrix A of size 3x3: \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
scanf("%d", &A[row][col]);
}
}
printf("\nEnter elements in matrix B of size 3x3: \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
scanf("%d", &B[row][col]);
}
}
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
/* Cij = Aij - Bij */
C[row][col] = A[row][col] - B[row][col];
}
}
printf("\nDifference of two matrices A-B = \n");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
printf("%d ", C[row][col]);
}
printf("\n");
}
return 0;
}
Output
Enter elements in matrix A of size 3x3 :
1 2 3
4 5 6
7 8 9
Enter elements in matrix B of size 3x3 :
9 8 7
6 5 4
3 2 1
Difference of both matrices A -
B =
-8 -6 -4
-2 0 2
4 6 8
0 Comments