Home »
C programs »
C two-dimensional arrays programs
C program to check two matrices are identical or not
Here, we are going to learn how to check whether two matrices are identical or not in C programming language?
Submitted by Nidhi, on July 13, 2021
Problem statement
Given two matrices, we have to check whether they are identical or not using C program.
Checking two matrices are identical or not
The source code to check two matrices are identical or not is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
C program to check two matrices are identical or not
// C program to check two matrices are identical or not
#include <stdio.h>
#define SIZE 3
int areIdentical(int Matrix1[][SIZE], int Matrix2[][SIZE])
{
int i = 0;
int j = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (Matrix1[i][j] != Matrix1[i][j])
return 0;
}
}
return 1;
}
int main()
{
int Matrix1[SIZE][SIZE] = {
{ 1, 2, 3 },
{ 2, 1, 3 },
{ 3, 2, 1 },
};
int Matrix2[SIZE][SIZE] = {
{ 1, 2, 3 },
{ 2, 1, 3 },
{ 3, 2, 1 },
};
if (areIdentical(Matrix1, Matrix2))
printf("Both matrices are identical\n");
else
printf("Matrices are not identical\n");
return 0;
}
Output
Both matrices are identical
Explanation
In the above program, we created two functions areIdentical() and main() function. The areIdentical() function is a user-defined function, it is used to check that, given matrices are identical or not.
In the main() function, we created two matrices Matrix1, Matrix2. Then we compared both matrices using areIdentical() function and then we printed the appropriate message on the console screen.
C Two-dimensional Arrays Programs »