Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- int getMatrix2D(int** matrix, int rows)
- {
- //Allocate memory and initilize
- string input;
- stringstream lineIn;
- int elm;
- getline(cin, input);
- lineIn << input;
- int col = 0;
- //Get number on colons
- while (lineIn >> elm)
- {
- col++;
- }
- //Clear and load buffer
- lineIn.str("");
- lineIn.clear();
- lineIn << input;
- matrix[0] = new int[col];
- for (int j = 0; j < col; j++)
- {
- lineIn >> elm;
- matrix[0][j] = elm;
- }
- //Clear buffer
- lineIn.str("");
- lineIn.clear();
- for (int i = 1; i < rows; i++)
- {
- getline(cin, input);
- lineIn << input;
- matrix[i] = new int[col];
- for (int j = 0; j < col; j++)
- {
- lineIn >> elm;
- matrix[i][j] = elm;
- }
- //Clear buffer
- lineIn.str("");
- lineIn.clear();
- }
- return col;
- }
- bool compareMatrix(int** matrix1, int** matrix2, int rows, int cols)
- {
- for (int i = 0; i < rows; i++)
- for (int j = 0; j < cols; j++)
- if (matrix1[i][j] != matrix2[i][j])
- return false;
- return true;
- }
- void deleteMatrix(int** matrix, int rows)
- {
- for (int i = 0; i < rows; i++)
- delete[] matrix[i];
- delete[] matrix;
- }
- int main()
- {
- //initilized with 0
- // int matrix1[10][10] = {};
- // int matrix2[10][10] = {};
- int row1, row2, col1, col2;
- cin >> row1; cin.ignore();
- int** matrix1 = new int* [row1];
- col1 = getMatrix2D(matrix1, row1);
- cin >> row2; cin.ignore();
- if (row1 != row2)
- {
- cout << "not equal";
- deleteMatrix(matrix1, row1);
- return 0;
- }
- int** matrix2 = new int* [row2];
- col2 = getMatrix2D(matrix2, row2);
- if (col1 != col2)
- {
- cout << "not equal";
- deleteMatrix(matrix1, row1);
- deleteMatrix(matrix2, row2);
- return 0;
- }
- if (compareMatrix(matrix1, matrix2, row1, col1))
- cout << "equal";
- else
- cout << "not equal";
- deleteMatrix(matrix1, row1);
- deleteMatrix(matrix2, row2);
- }
Advertisement
Add Comment
Please, Sign In to add comment