C++ Online Compiler
Example: Check Matrix Equality in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Check Matrix Equality #include <iostream> #include <vector> // Function to check if two matrices are equal bool areMatricesEqual(const std::vector<std::vector<int>>& mat1, const std::vector<std::vector<int>>& mat2) { // Step 1: Check if the number of rows are equal if (mat1.size() != mat2.size()) { return false; } // Step 2: If rows are 0 (empty matrices), they are equal if (mat1.empty()) { return true; // Both are empty, or one is empty and the other checked empty. // The first check `mat1.size() != mat2.size()` handles cases // where one is empty and other is not. } // Step 3: Check if the number of columns are equal for each row // Assuming matrices are rectangular (all rows have same number of columns) if (mat1[0].size() != mat2[0].size()) { return false; } // Step 4: Iterate through each element and compare for (size_t i = 0; i < mat1.size(); ++i) { // Iterate through rows for (size_t j = 0; j < mat1[i].size(); ++j) { // Iterate through columns if (mat1[i][j] != mat2[i][j]) { return false; // Found a differing element } } } // Step 5: If all checks pass, matrices are equal return true; } int main() { // Test Case 1: Equal Matrices std::vector<std::vector<int>> matrixA = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::vector<std::vector<int>> matrixB = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::cout << "Test Case 1 (Equal Matrices): " << std::endl; if (areMatricesEqual(matrixA, matrixB)) { std::cout << "Matrices A and B are equal." << std::endl; } else { std::cout << "Matrices A and B are NOT equal." << std::endl; } std::cout << std::endl; // Test Case 2: Unequal Matrices (different element) std::vector<std::vector<int>> matrixC = {{1, 2, 3}, {4, 5, 0}, {7, 8, 9}}; // Element (1,2) is 0 std::vector<std::vector<int>> matrixD = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; std::cout << "Test Case 2 (Unequal Matrices - different element): " << std::endl; if (areMatricesEqual(matrixC, matrixD)) { std::cout << "Matrices C and D are equal." << std::endl; } else { std::cout << "Matrices C and D are NOT equal." << std::endl; } std::cout << std::endl; // Test Case 3: Unequal Matrices (different number of rows) std::vector<std::vector<int>> matrixE = {{1, 2}, {3, 4}}; std::vector<std::vector<int>> matrixF = {{1, 2}, {3, 4}, {5, 6}}; std::cout << "Test Case 3 (Unequal Matrices - different rows): " << std::endl; if (areMatricesEqual(matrixE, matrixF)) { std::cout << "Matrices E and F are equal." << std::endl; } else { std::cout << "Matrices E and F are NOT equal." << std::endl; } std::cout << std::endl; // Test Case 4: Unequal Matrices (different number of columns) std::vector<std::vector<int>> matrixG = {{1, 2, 3}, {4, 5, 6}}; std::vector<std::vector<int>> matrixH = {{1, 2}, {4, 5}}; std::cout << "Test Case 4 (Unequal Matrices - different columns): " << std::endl; if (areMatricesEqual(matrixG, matrixH)) { std::cout << "Matrices G and H are equal." << std.endl; } else { std::cout << "Matrices G and H are NOT equal." << std::endl; } std::cout << std::endl; // Test Case 5: Empty Matrices std::vector<std::vector<int>> matrixI = {}; std::vector<std::vector<int>> matrixJ = {}; std::cout << "Test Case 5 (Empty Matrices): " << std::endl; if (areMatricesEqual(matrixI, matrixJ)) { std::cout << "Matrices I and J are equal." << std::endl; } else { std::cout << "Matrices I and J are NOT equal." << std::endl; } std::cout << std::endl; // Test Case 6: One empty, one not std::vector<std::vector<int>> matrixK = {{1}}; std::vector<std::vector<int>> matrixL = {}; std::cout << "Test Case 6 (One empty, one not): " << std::endl; if (areMatricesEqual(matrixK, matrixL)) { std::cout << "Matrices K and L are equal." << std::endl; } else { std::cout << "Matrices K and L are NOT equal." << std::endl; } std::cout << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS