Advertisement
krstoilo

Compare Matrices

Nov 3rd, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. void readInput(int & inputRows, vector<vector<int>> & matrix, int & sizeCols){
  9.  
  10.  
  11.     for(int rows = 0; rows < inputRows; rows++){
  12.  
  13.         vector<int> colsMatrix;
  14.         string numbersInCol;
  15.         getline(cin,numbersInCol);
  16.         istringstream istr(numbersInCol);
  17.  
  18.         string recordStream;
  19.         int currentNum = 0;
  20.         while(istr >> recordStream){
  21.  
  22.         currentNum = stoi(recordStream);
  23.         colsMatrix.push_back(currentNum);
  24.         }
  25.  
  26.         sizeCols = colsMatrix.size();
  27.         matrix.push_back(colsMatrix);
  28.  
  29.         }
  30. }
  31.  
  32. bool compareMatrices (const vector<vector<int>> & matrix1, const vector<vector<int>> & matrix2,
  33.                       const int & rows1, const int & cols1, const int & rows2, const int & cols2){
  34.  
  35.     bool sizeEqual;
  36.  
  37.     if(rows1 == rows2 && cols1 == cols2 ){
  38.  
  39.         sizeEqual = true;
  40.  
  41.     }
  42.  
  43.     bool elementsEqual;
  44.     int falseCounter = 0;
  45.  
  46.  
  47.     for(int rows = 0; rows < rows1; rows++){
  48.  
  49.         for(int cols = 0; cols < cols1; cols++){
  50.  
  51.  
  52.             if(matrix1[rows][cols] == matrix2[rows][cols]){
  53.  
  54.                 elementsEqual = true;
  55.  
  56.             } else{
  57.  
  58.                 falseCounter++;
  59.             }
  60.         }
  61.     }
  62.  
  63.     if(falseCounter > 0){
  64.  
  65.         elementsEqual = false;
  66.     }
  67.  
  68.  
  69.     bool equalMatrices;
  70.  
  71.     if(sizeEqual && elementsEqual){
  72.  
  73.         equalMatrices = true;
  74.     }
  75.  
  76.     return equalMatrices;
  77. }
  78.  
  79.  
  80. int main(){
  81.  
  82.     int rowsMatrix1 = 0;
  83.     cin >> rowsMatrix1;
  84.     cin.ignore();
  85.  
  86.     vector<vector<int>> matrix1;
  87.     int sizeCols = 0;
  88.  
  89.     readInput(rowsMatrix1,matrix1,sizeCols);
  90.  
  91.     int rowsMatrix2 = 0;
  92.     cin >> rowsMatrix2;
  93.     cin.ignore();
  94.  
  95.     vector<vector<int>> matrix2;
  96.     int sizeCols2 = 0;
  97.  
  98.     readInput(rowsMatrix2,matrix2,sizeCols2);
  99.  
  100.     bool areEqual = compareMatrices(matrix1,matrix2,rowsMatrix1,sizeCols,rowsMatrix2,sizeCols2);
  101.  
  102.  
  103.     if(areEqual == true){
  104.  
  105.         cout << "equal" << endl;
  106.  
  107.     } else {
  108.  
  109.         cout << "not equal" << endl;
  110.     }
  111.  
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement