**C programs to check the equality of two matrices** have been shown here. Two matrices $[A]_{m \times n}$ and $[B]_{p \times q}$ are considered to be equal if both of the following conditions are satisfied:

(i) Number of rows and columns are same for both of the matrices i.e. $m = p$ and $n = q$

(ii) Each elements of $A$ is equal to corresponding element of $B$ i.e. $A_{ij} = B_{xy}$ for each $i \in m$, $j \in n$, $x \in p$, $y \in q$, $i = x$ and $j = y$.

The algorithm, pseudocode and time complexity of the program have also been shown.

#### Page content(s):

## 1. Algorithm to check the equality of two matrices

1. Take two matrice $A_{m\times n}$ and $B_{p\times q}$

2. Check if $m=p$ and $n=q$

3. If step [2] is false then print "Not equal" and exit the program

4. If step [2] is true, then

5. Check if each corresponding element of $A$ and $B$ are equal

6. If step [5] is true then print "Equal" and exit program

7. If step [5] is false then print "Not equal" and exit program

## 2. Pseudocode to check the equality of two matrices

**Input**: Two matrices $A_{m\times n}$ and $B_{p\times q}$

**Output**: Equal or Not equal

1. **Procedure** matrixEquality($A_{m\times n}$, $B_{p\times q}$):

2. **If** $m==p$ and $n==q$:

3. **If** $A_{ij}==B_{ij}$ for each $i \in m$ and $j \in n$:

4. **Return** *Equal*

5. **Else:**

6. ** Return ***Not equal*

7. **Else:**

8. **Return ***Not equal*

9. **End Procedure**

## 3. Time Complexity to check the equality of two matrices

**Time Complexity:** **O($mn$)**

Where $m$ is the number of rows and $n$ is the number of columns in the matrices.

## 4. C Program to check the equality of two matrices

/****************************************** Alphabetacoder.com C program to check equality of two matrices *******************************************/ #include <stdio.h> int main() { // declare variables int m, n, p, q, flag = 0, i, j; int A[10][10] = {0}, B[10][10] = {0}; //take input of the order of first matrix printf("Enter the number of row and column of first matrix = "); scanf("%d%d", & m, & n); //take input of the first matrix printf("Enter the first matrix of order %d x %d = \n", m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", & A[i][j]); } } //take input of the order of second matrix printf("Enter the number of row and column of second matrix = "); scanf("%d%d", & p, & q); //take input of the first matrix printf("Enter the second matrix of order %d x %d = \n", p, q); for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { scanf("%d", & B[i][j]); } } // check if order of matrices are same // if not same order then matrices are of different order if (m != p || n != q) { printf("\nMatrices are of different order, hence not equal"); flag = 1; } else { //check equality of each corresponding elements for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (A[i][j] != B[i][j]) { // inequality spotted printf("\nMatrices are not equal. Element mismatch at row %d, column %d ", i + 1, j + 1); flag = 1; break; } } if (flag == 1) break; } } // if flag = 0, then matrices are equal if (flag == 0) printf("\nMatrices are equal"); return 0; }

Output

**Case 1:**

Enter the number of row and column of first matrix = 3 3

Enter the first matrix of order 3 x 3 =

1 2 3

4 5 6

7 8 9

Enter the number of row and column of second matrix = 2 3

Enter the second matrix of order 2 x 3 =

8 0 5

6 4 1

Matrices are of different order,hence not equal

**Case 2:**

Enter the number of row and column of first matrix = 2 2

Enter the first matrix of order 2 x 2 =

1 2

4 5

Enter the number of row and column of second matrix = 2 2

Enter the second matrix of order 2 x 2 =

1 2

4 5

Matrices are equal

**Case 3:**

Enter the number of row and column of first matrix = 2 2

Enter the first matrix of order 2 x 2 =

1 3

5 6

Enter the number of row and column of second matrix = 2 2

Enter the second matrix of order 2 x 2 =

1 3

5 7

Matrices are not equal. Element mismatch at row 2, column 2

## 5. C Program to check the equality of two matrices using recursion

/********************************************************** Alphabetacoder.com C program to check equality of two matrices using recursion ***********************************************************/ #include <stdio.h> #define MAXSIZE 10 // Declare a recursive function to check // equality of two matrices // This function takes two matices (A and B), Sizes of both matrices (m, n) // and index of current row & column as parameters int check_equality(int A[][MAXSIZE], int B[][MAXSIZE], int m, int n, int row, int col) { // check if all columns are traversed // if yes the set col = 0 and increment row if (col >= n) { row++; col = 0; } // check if all rows are traversed if (row >= m) return 1; // check equality of corresponding element // if current element of both matrices are // not equal then return 0 // If elements are same then go for next element // by calling the recursive function if (A[row][col] != B[row][col]) { printf("\nMatrices are not equal. Element mismatch at row %d, column %d", row + 1, col + 1); return 0; } else return check_equality(A, B, m, n, row, col + 1); } int main() { // declare variables int m, n, p, q, flag = 0, i, j; int A[MAXSIZE][MAXSIZE] = {0}, B[MAXSIZE][MAXSIZE] = {0}; //take input of the order of first matrix printf("Enter the number of row and column of first matrix = "); scanf("%d%d", & m, & n); //take input of the first matrix printf("Enter the first matrix of order %d x %d = \n", m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", & A[i][j]); } } //take input of the order of second matrix printf("Enter the number of row and column of second matrix = "); scanf("%d%d", & p, & q); //take input of the first matrix printf("Enter the second matrix of order %d x %d = \n", p, q); for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { scanf("%d", & B[i][j]); } } // check if order of matrices are same // if not same order then check each corresponding elements if (m != p || n != q) { printf("\nMatrices are of different order, hence not equal"); flag = 1; } else { //check equality of each corresponding elements // by calling a recursive function // If the function return 1 then the matrices are equal // If the function return 0 then the matrices are not equal if (check_equality(A, B, m, n, 0, 0)) printf("\nMatrices are equal"); } return 0; }

Output

**Case 1:**

Enter the number of row and column of first matrix = 4 3

Enter the first matrix of order 4 x 3 =

3 2 3

4 1 6

5 8 9

2 7 5

Enter the number of row and column of second matrix = 2 3

Enter the second matrix of order 2 x 3 =

5 0 1

6 4 7

Matrices are of different order,hence not equal

**Case 2:**

Enter the number of row and column of first matrix = 4 3

Enter the first matrix of order 4 x 3 =

3 2 3

4 1 6

5 8 9

2 7 5

Enter the number of row and column of second matrix = 4 3

Enter the second matrix of order 4 x 3 =

3 2 3

4 1 6

5 8 9

2 7 5

Matrices are equal

**Case 3:**

Enter the number of row and column of first matrix = 2 2

Enter the first matrix of order 2 x 2 =

7 8

4 6

Enter the number of row and column of second matrix = 2 2

Enter the second matrix of order 2 x 2 =

7 2

4 6

Matrices are not equal. Element mismatch at row 1, column 2

## No comments:

## Post a Comment

If you have any doubts or suggestions, please leave a note.