# Check Two Matrices are Identical in C | Check Two Matrices are Equal

## Program Description:

Write a Program to Check Two Matrices are Identical in C programming language. The program should accept two matrices from the user and do the element-by-element cooperation and return if the two matrices are equal/identical.

📢 This Program is part of the Arrays and Matrix Programs Series

Here is the example input and output of the program.

### Program Excepted Input and Output:

Input:

Output:

Given Matrices X &amp; Y are Equal

As we can see from the above output, both matrices X and Y are equal to each other.

### Program Prerequisites:

We need to know the one-dimensional and multi-dimensional arrays and how to pass multi-dimensional arrays to function.

## Check Two Matrices are Identical in C Program Explanation:

Let’s look at the step-by-step instructions to check if two matrices are identical i.e two matrices are equal C Program

1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns.(Change this number if you want to use large arrays)
2. Start the Program by declaring two Matrices (2D Arrays). They are X matrix, Y matrix. The row size and column size of the matrices are ROWS and COLUMNS respectively.
3. Prompt the user to provide the desired Row size and Column Size, and store them in the variables rows and columns respectively.
4. Check if the array size is going out of present limits ( 1-100) using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the present limits.
5. Take the user input for Matrix-X, We need to create two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
6. Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<rows (which is equal to for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
• Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). At each iteration of the Inner Loop
• Prompt the user to provide the Matrix or 2D Array element and Read the element using the scanf function and Update the X[i][j] element.
• Repeat the above step for all elements in the 2D array or matrix
7. Repeat the step-5 to Take the Second Matrix ( Matrix-Y) elements from the user.
8. To Check if the two matrices are Identical, The two matrices need to be compared element by element. We can say that two matrices are identical or equal to one another if they both had similar elements at a similar index.
9. Create Outer For Loop to iterate over rows – for(i=0; i<rows; i++)
• Create Inner For loop to iterate over columns – for(j=0; j<columns; j++), At Each Iteration,
• Check if the X[i][j] element is equal to Y[i][j] element – if(X[i][j] != Y[i][j])
• If any element pair is not equal, Then Matrix X and Matrix Y are not Identical.
10. Once the above step-9 is finished and Program is not returned, Then the all elements in the Matrix X and Matrix Y are the same, So we can say both matrices are Identical or Equal.
11. Display the message on the console.
12. Stop the Program.

## Program to Check Two Matrices are Identical in C Using Iterative Approach:

Here is the program to check if the two matrices are equal in the C programming.

### Program Output:

Compile and Run the above c matrix program.

Test 1: Check Normal Cases:

As we can see from the above output, Given Matrices X and Y are Identical.

If you observe the above output, Element X and Y are different, So Matrices X and Y are not equal or Not Identical.

Test 2: Negative cases:

If the user enters the row size or columns which is beyond the current limit, Then program displays an error message Invalid Rows or Columns size, Please Try again)

## Check Two Matrices are Identical using Functions in C:

We have used a single function (i.e main() function) to check if if the two matrices are identical, But it is advised to use the functions to perform a specific task. In the above program, we have a repeating task, reading matrix input. We can create a function to read the matrix elements, and we can call the function as many times as we want. Let’s rewrite the above program and use the functions.

Here is the modified version of the above program with three user-defined functions. Except the main() function.

We have defined three user-defined functions(except the main()) in the above program.

1. The readMatrix() function – Used to read the elements from the user and update the given matrix
• Function Prototypevoid readMatrix(int rows, int columns, int A[rows][columns])
• The readMatrix() function takes three formal arguments which are rows, columns, and Matrix A.
• This function uses Two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &A[i][j]);
• The readMatrix() function doesn’t return anything back to the caller. So return type is void.
• As the arrays are passed by the address/reference, All the changes made to the Matrix-A in the readMatrix() function will be visible in the Caller Function (i.e main() function).
2. The displayMatrix() function is used to print the Matrix elements on the Standard Output.
• The prototype of displayMatrix function is void displayMatrix(int rows, int columns, int A[rows][columns])
• Similar to the readMatrix() function, The displayMatrix() function also takes three formal arguments. i.e rows, columns, and A[][] (2D Array)
• The displayMatrix() function uses Two For loops to iterate over the A matrix and print the elements of the matrix. – printf("%d ", A[i][j]);
3. The isEqual() function – This function checks if the Matrix-X and Matrix-Y are Identical / Equal.
• Function Prototypeint isEqual(int rows, int columns, int X[rows][columns], int Y[rows][columns])
• This function takes Four Formal Arguments which are rows, columns, matrix X, and matrix Y.
• The isEqual() function uses two for loops to iterate over the elements of the Matrices X and Y and does the element-wise comparison – if(X[i][j] != Y[i][j])
• This function returns an Integer ( int). If the both Matrices X and Y are equal, Then isEqual() function return One(1), Otherwise, it returns Zero(0)

### Program Output:

Compile the program

\$ gcc check-matrices-equal.c

Run the Program.

From the above Output, We can see the first attempt where Both matrices are Identical and in the second attempt, Matrices are not equal.

## Related Array Programs: Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 1 Response

1. […] C Program to Check Two Matrices are Equal […]