Check whether two matrices are equal or not with C.
#include <stdio.h>
int main()
{
int A[3][3];
int B[3][3];
int row, col, isEqual;
/* Input elements in first matrix from user */
printf("Enter elements in matrix A of 3 %dx%d: \n", 3, 3);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Input elements in second matrix from user */
printf("\nEnter elements in matrix B of 3 %dx%d: \n");
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf("%d", &B[row][col]);
}
}
/* Assumes that the matrices are equal */
isEqual = 1;
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
if(A[row][col] != B[row][col])
{
isEqual = 0;
break;
}
}
}
if(isEqual == 1)
{
printf("\nMatrix A is equal to Matrix B");
}
else
{
printf("\nMatrix A is not equal to Matrix B");
}
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 :
1 2 3
4 5 6
7 8 9
Matrix A is not equal to Matrix B
0 Comments