Advertisement
Spocoman

05. Compare Matrices

Feb 9th, 2024
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <memory>
  5.  
  6. using namespace std;
  7.  
  8. unique_ptr<unique_ptr<int[]>[]> matrixFiller(const int& c) {
  9.  
  10.     int r;
  11.     cin >> r;
  12.     cin.ignore();
  13.  
  14.     unique_ptr<unique_ptr<int[]>[]> m = make_unique<unique_ptr<int[]>[]>(r);
  15.  
  16.     string line;
  17.  
  18.     for (int row = 0; row < r; row++) {
  19.         m[row] = make_unique<int[]>(c);
  20.  
  21.         getline(cin, line);
  22.  
  23.         istringstream ss(line);
  24.  
  25.         int num;
  26.         for (int col = 0; col < c; col++) {
  27.             m[row][col] = ss >> num ? num : 0;
  28.         }
  29.     }
  30.  
  31.     return m;
  32. }
  33.  
  34. bool areEqual(const unique_ptr<unique_ptr<int[]>[]>& a,
  35.     const unique_ptr<unique_ptr<int[]>[]>& b, const int& c) {
  36.  
  37.     for (int row = 0; row < sizeof(a) / 4; row++) {
  38.         for (int col = 0; col < c; col++) {
  39.             if (a[row][col] != b[row][col]) {
  40.                 return false;
  41.             }
  42.         }
  43.     }
  44.  
  45.     return true;
  46. }
  47.  
  48. int main() {
  49.     int c = 10;
  50.  
  51.     unique_ptr<unique_ptr<int[]>[]> matrix1 = matrixFiller(c), matrix2 = matrixFiller(c);
  52.     cout << (areEqual(matrix1, matrix2, c) ? "equal" : "not equal") << endl;
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement