C Program to check two matrices are equal or not

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

A Matrix is a set of values in Row and Column format. In this C program, we need to check two Matrices are Equal or Not. To check the two Matrices are Equal, we need; first, we need to check the Order of the two Matrices is Equal or not; if the Order of the Matrices is not equal, then we cannot compare those Matrices.

What is the order of a matrix?

The Order of a Matrix means the number of Columns and Rows a Matrix has, is called the Order. Example: If Matrix A has 5 Rows and 2 Columns, then the Order of the Matrix is 5*2.

How to check the matrices are equal in C?

The logic of this program is first to declare two Matrix A and B.Then read the Order of Matrix A and B from the user. Read the elements of the Matrix A and B using for loop. Then check the Order of the Matrix is Equal or Not. If the Order of the Matrix is Equal, then we can make the comparison. Else display Matrix cannot be compared. The comparison is made by checking each element of Matrix A with the element of Matrix B using a nested for loop. If all elements are equal, then set Flag=1 else Flag=0. If Flag==1 then displays two Matrices are Equal else shows two Matrices are not Equal.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the Matrix A, B, as an integer.

STEP 3: Declare the integer variables i,j,R1,C1,R2,flag.

STEP 4: Set flag=1.

STEP 5: Read the Order of matrix A  into the variables R1 and C1.

STEP 6: Read the Order of Matrix B  into the variables R2 and C2.

STEP 7: Read the Elements of Matrix A by using the for loop.

STEP 8: Read the Elements of Matrix B by using the for loop.

STEP 9: Display the Matrix A by using for loop.

STEP 10: Display the Matrix B by using for loop.

STEP 11: Then check if R1 == R2 && C1 == C2,then do Step 12.Else do step 19

STEP 12: Set i=0.

STEP 13: Check i

STEP 14: Set j=0.

STEP 15: Check j

STEP 16: Check if(A[i][j] != B[i][j]) then set flag=0 and go to step 

STEP 17: Increment j by one and do step 15.

STEP 18: Increment i by one repeat step 13.

STEP 19: If the Rows and columns are not equal, then display Cannot be Compared and exit.

STEP 20: If flag==1, then display two matrices are equal.

STEP 21: If flag==0, then display two matrices are not equal.

C Source Code

                                          #include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main() {
  int A[10][10], B[10][10];
  int i, j, R1, C1, R2, C2, flag = 1;
  printf("Enter the order of the matrix A\n");
  scanf("%d %d", & R1, & C1);
  printf("Enter the order of the matrix B\n");
  scanf("%d %d", & R2, & C2);
  printf("Enter the elements of matrix A\n"); /* accept the elements of first matrix */
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C1; j++) {
      scanf("%d", & A[i][j]);
    }
  }
  printf("Enter the elements of matrix B\n"); /* accept the elements of second matrix  */
  for (i = 0; i < R2; i++) {
    for (j = 0; j < C2; j++) {
      scanf("%d", & B[i][j]);
    }
  }
  printf("MATRIX A is\n");
  for (i = 0; i < R1; i++) {
    for (j = 0; j < C1; j++) {
      printf("%d", A[i][j]);
    }
    printf("\n");
  }
  printf("MATRIX B is\n"); /* prints the elements of second matrix  */
  for (i = 0; i < R2; i++) {
    for (j = 0; j < C2; j++) {
      printf("%d", B[i][j]);
    }
    printf("\n");
  }
  if (R1 == R2 && C1 == C2) /* Comparing two matrices for equality */ {
    printf("Matrices can be compared\n");
    for (i = 0; i < R1; i++) {
      for (j = 0; j < C2; j++) {
        if (A[i][j] != B[i][j]) {
          flag = 0;
          break;
        }
      }
    }
  } else {
    printf(" Cannot be compared\n");
    exit(1);
  }
  if (flag == 1)
    printf("Two matrices are equal\n");
  else
    printf("But,two matrices are not equal\n");
}

                                      

OUTPUT

Enter the order of the matrix A
2* 2

Enter the order of the matrix B
2 *2

Enter the elements of matrix A
  1 2
  3 4

Enter the elements of matrix B
  1 2
  3 4

MATRIX A is
  1  2
  3  4

MATRIX B is
 1  2
 3  4

Matrices can be compared
Two matrices are equal