C++ Program to Check Whether Two Matrices Are Equal or Not

13/06/2024 0 By indiafreenotes

To check whether two matrices are equal, we need to verify that they have the same dimensions and that each corresponding element is identical.

Problem Statement

Given two matrices, determine if they are equal. Two matrices are considered equal if they have the same dimensions and their corresponding elements are identical.

Approach

  1. Dimension Check: First, ensure that both matrices have the same number of rows and columns.
  2. Element-wise Comparison: Compare each corresponding element in the two matrices. If all corresponding elements are equal, the matrices are considered equal; otherwise, they are not.

Example

Given two 3×3 matrices:

Matrix A:

1 2 3

4 5 6

7 8 9

Matrix B:

1 2 3

4 5 6

7 8 9

These matrices are equal.

Given another pair of matrices:

Matrix A:

1 2 3

4 5 6

7 8 9

Matrix B:

9 8 7

6 5 4

3 2 1

These matrices are not equal.

C++ Code Implementation

Here is a C++ program to check whether two matrices are equal:

#include <iostream>

#include <vector>

using namespace std;

// Function to check if two matrices are equal

bool areMatricesEqual(const vector<vector<int>>& matrixA, const vector<vector<int>>& matrixB) {

    if (matrixA.size() != matrixB.size() || matrixA[0].size() != matrixB[0].size()) {

        return false;

    }

    for (size_t i = 0; i < matrixA.size(); ++i) {

        for (size_t j = 0; j < matrixA[0].size(); ++j) {

            if (matrixA[i][j] != matrixB[i][j]) {

                return false;

            }

        }

    }

    return true;

}

// Function to print a matrix

void printMatrix(const vector<vector<int>>& matrix) {

    for (const auto& row : matrix) {

        for (const auto& elem : row) {

            cout << elem << ” “;

        }

        cout << endl;

    }

}

int main() {

    // Initializing two 3×3 matrices

    vector<vector<int>> matrixA = {

        {1, 2, 3},

        {4, 5, 6},

        {7, 8, 9}

    };

    vector<vector<int>> matrixB = {

        {1, 2, 3},

        {4, 5, 6},

        {7, 8, 9}

    };

    vector<vector<int>> matrixC = {

        {9, 8, 7},

        {6, 5, 4},

        {3, 2, 1}

    };

    cout << “Matrix A:” << endl;

    printMatrix(matrixA);

    cout << “Matrix B:” << endl;

    printMatrix(matrixB);

    cout << “Matrix C:” << endl;

    printMatrix(matrixC);

    // Checking if matrixA and matrixB are equal

    if (areMatricesEqual(matrixA, matrixB)) {

        cout << “Matrix A and Matrix B are equal.” << endl;

    } else {

        cout << “Matrix A and Matrix B are not equal.” << endl;

    }

    // Checking if matrixA and matrixC are equal

    if (areMatricesEqual(matrixA, matrixC)) {

        cout << “Matrix A and Matrix C are equal.” << endl;

    } else {

        cout << “Matrix A and Matrix C are not equal.” << endl;

    }

    return 0;

}

Explanation

  1. Function areMatricesEqual:

  • Parameters:

Takes two 2D vectors (matrixA and matrixB).

  • Dimension Check:

First, checks if the dimensions of both matrices are the same.

  • Element-wise Comparison:

Iterates through each element, comparing corresponding elements in the matrices. If a mismatch is found, the function returns false.

  • Return Value:

If all corresponding elements are equal, the function returns true.

  1. Function printMatrix:

Prints the matrix in a formatted way for better visualization.

  1. Main Function:

  • Initializes three sample 3×3 matrices (matrixA, matrixB, and matrixC).
  • Prints the matrices.
  • Calls the areMatricesEqual function to check the equality of matrixA and matrixB, then matrixA and matrixC.
  • Outputs the results of the equality checks.