C Program to Check if Two Matrices are Equal

«
»

This C Program checks if 2 matrices are equal.

Problem Description

This C Program checks whether the 2 Matrices are Equal. The program first reads 2 matrices and then checks both the matrices are equal. If both the matrices are equal then display they are equal. If both the matrices are not equal then display they are different.

Problem Solution

1. Create two matrices (2D arrays) and define their elements according to the size.
2. To check whether the two matrices are equal or not, first check if they have equal number od rows and columns.
3. Now, create a nested for loop to access each element of the two matrices and compare the elements of same location for equality.
4. The loop will break if it finds any inequality with setting a flag variable 0, to indicate unequal matrices.

Program/Source Code

Here is source code of the C program to check whether the 2 Matrices are Equal. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * C Program to accept two matrices and check if they are equal
  4.      */
  5.  
  6.     #include <stdio.h>
  7.     #include <stdlib.h>
  8.  
  9.     void main() 
  10.     {
  11.  
  12.         int a[10][10], b[10][10];
  13.         int i, j, row1, column1, row2, column2, flag = 1;
  14.  
  15.         printf("Enter the order of the matrix A \n");
  16.         scanf("%d %d", &row1, &column1);
  17.  
  18.         printf("Enter the order of the matrix B \n");
  19.         scanf("%d %d", &row2, &column2);
  20.  
  21.         printf("Enter the elements of matrix A \n");
  22.         for (i = 0; i < row1; i++)
  23.         {
  24.             for (j = 0; j < column1; j++) 
  25.             {
  26.                 scanf("%d", &a[i][j]);
  27.             }
  28.         }
  29.  
  30.         printf("Enter the elements of matrix B \n");
  31.         for (i = 0; i < row2; i++) 
  32.         {
  33.             for (j = 0; j < column2; j++)
  34.             {
  35.                 scanf("%d", &b[i][j]);
  36.             }
  37.         }
  38.  
  39.         printf("MATRIX A is \n");
  40.         for (i = 0; i < row1; i++)
  41.         {
  42.             for (j = 0; j < column1; j++) 
  43.             {
  44.                 printf("%3d", a[i][j]);
  45.             }
  46.             printf("\n");
  47.  
  48.         }
  49.  
  50.         printf("MATRIX B is \n");
  51.         for (i = 0; i < row2; i++)
  52.         {
  53.             for (j = 0; j < column2; j++) 
  54.             {
  55.                 printf("%3d", b[i][j]);
  56.             }
  57.             printf("\n");
  58.  
  59.         }
  60.  
  61.         /*  Comparing two matrices for equality */
  62.  
  63.         if (row1 == row2 && column1 == column2)
  64.         {
  65.             printf("Matrices can be compared \n");
  66.             for (i = 0; i < row1; i++) 
  67.             {
  68.                 for (j = 0; j < column2; j++)
  69.                 {
  70.                     if (a[i][j] != b[i][j])
  71.                     {
  72.                         flag = 0;
  73.                         break;
  74.                     }
  75.                  }
  76.             }
  77.         }
  78.  
  79.         else 
  80.         {
  81.             printf(" Cannot be compared\n");
  82.             exit(1);
  83.         }
  84.  
  85.         if (flag == 1)
  86.             printf("Two matrices are equal \n");
  87.         else
  88.             printf("But, two matrices are not equal \n");
  89.  
  90.     }
Program Explanation

1. Declare two 2D array, of some fixed capacity.
2. Take input from users the number of rows and columns. Accordingly define the elements of the array.
3. Declare a flag variable with initial value as 1, which will indicate the equal/unequal matrix.
4. Start a for loop with 2 iterators, i and j, and access each element of the two arrays using these.
5. Compare the elements of both the two arrays at the same location, and set flag to 0 in inequality occurs.
6. The matrix will be equal if flag remains 1 and has not been changed.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
Enter the order of the matrix A
2 2
Enter the order of the matrix B
2 2
Enter the elements of matrix A
23 56
45 80
Enter the elements of matrix B
50 26
39 78
MATRIX A is
 23 56
 45 80
MATRIX B is
 50 26
 39 78
Matrices can be compared
But,two matrices are not equal
 
$ a.out
Enter the order of the matrix A
2 2
Enter the order of the matrix B
2 2
Enter the elements of matrix A
10 50
15 30
Enter the elements of matrix B
10 50
15 30
MATRIX A is
 10 50
 15 30
MATRIX B is
 10 50
 15 30
Matrices can be compared
Two matrices are equal

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.