C++ program to check if the matrices are equal or not using class

Submitted by Shubh Pachori, on September 15, 2022

Problem statement

Given two matrices, we to check they are equal or not using the class and object approach.

Example:

```Input:
Enter 1st Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 9

Enter 2nd Matrix elements :
[0][0]: 1
[0][1]: 2
[0][2]: 3
[1][0]: 4
[1][1]: 5
[1][2]: 6
[2][0]: 7
[2][1]: 8
[2][2]: 1

Output:
Entered 1st Matrix :
1  2  3
4  5  6
7  8  9

Entered 2nd Matrix :
1  2  3
4  5  6
7  8  1

Matrices are not equal!
```

C++ code to check if the matrices are equal or not using the class and object approach

```#include <iostream>
using namespace std;

// create a class
class Matrix {
// private data member
private:
int matrix1[3][3], matrix2[3][3];

// public member funtions
public:
// getMatrix() funtion to insert matrices
void getMatrix() {
cout << "Enter 1st Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix1[row][column];
}
}

cout << "\nEnter 2nd Matrix elements :" << endl;
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
cout << "[" << row << "][" << column << "]: ";
cin >> matrix2[row][column];
}
}
}

// equalMatrix() funtion to check whether the
// matrices are equal or not
void equalMatrix() {
// initialising int type variables
// to perform operations
int row, column, check = 1;

cout << "\nEntered 1st Matrix :" << endl;

// for loop to show the inserted 1st matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix1[row][column] << "  ";
}
cout << "\n";
}

cout << "\nEntered 2nd Matrix :" << endl;

// for loop to show the inserted 2nd matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
cout << matrix2[row][column] << "  ";
}
cout << "\n";
}

// nested for loop to traverse the whole matrix
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
// if condition to check elements of both
// the matrices are not equal
if (matrix1[row][column] != matrix2[row][column]) {
check = 0;
break;
}
}
}

if (check == 1) {
cout << "\nMatrices are equal!" << endl;
} else {
cout << "\nMatrices are not equal!" << endl;
}
}
};

int main() {
// create an object
Matrix M;

// calling getMatrix() funtion to
// insert Matrix
M.getMatrix();

// calling equalMatrix() funtion to check the
// matrices are equal or not
M.equalMatrix();

return 0;
}
```

Output

```Enter 1st Matrix elements :
[0][0]:1
[0][1]:2
[0][2]:3
[1][0]:4
[1][1]:5
[1][2]:6
[2][0]:7
[2][1]:8
[2][2]:9

Enter 2nd Matrix elements :
[0][0]:1
[0][1]:2
[0][2]:3
[1][0]:4
[1][1]:5
[1][2]:6
[2][0]:7
[2][1]:8
[2][2]:9

Entered 1st Matrix :
1  2  3
4  5  6
7  8  9

Entered 2nd Matrix :
1  2  3
4  5  6
7  8  9

Matrices are equal!
```

Explanation

In the above code, we have created a class Matrix, two int type 2d array data members matrix1[3][3] and matrix2[3][3] to store the elements of the matrix, and public member functions getMatrix() and equalMatrix() to store the matrix elements and to check if the matrices are equal or not.

In the main() function, we are creating an object M of class Matrix, reading the inputted matrix by the user using getMatrix() function, and finally calling the equalMatrix() member function to check if the matrices are equal or not. The equalMatrix() function contains the logic to check if the matrices are equal or not and printing the result.