// Program to perform matrix manipulation with the aid of a Class. // Assignment-21 // matmanip.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #include #else #include #include using namespace std; #endif #include // For getch. const int MAXMATSIZE = 5; class Matrix { int* mat[MAXMATSIZE]; int row, col; public: Matrix(const int& = 0, const int& = 0); void initialize(const int& = 0); void addition(const Matrix&, const Matrix&); void subtraction(const Matrix&, const Matrix&); void multiplication(const Matrix&, const Matrix&); int comparison(const Matrix&, const Matrix&); void display(); }; Matrix::Matrix(const int& r, const int& c) { row = r; col = c; } void Matrix::initialize(const int& init) { int i, j; if (init != 0) { cout << "Enter " << row*col << " matrix elements: "; for (i = 0; i < row; i++) for (j = 0; j < col; j++) { mat[i + j] = new int; // Set aside memory. cin >> mat[i][j]; } } else for (i = 0; i < row; i++) for (j = 0; j < col; j++) { mat[i + j] = new int; // Set aside memory. mat[i][j] = 0; } } void Matrix::addition(const Matrix& mat1, const Matrix& mat2) { int i, j; for (i = 0; i < this->row; i++) for (j = 0; j < this->col; j++) this->mat[i][j] = mat1.mat[i][j] + mat2.mat[i][j]; } void Matrix::subtraction(const Matrix& mat1, const Matrix& mat2) { int i, j; for (i = 0; i < this->row; i++) for (j = 0; j < this->col; j++) this->mat[i][j] = mat1.mat[i][j] - mat2.mat[i][j]; } void Matrix::multiplication(const Matrix& mat1, const Matrix& mat2) { int i, j, k; if (mat1.col != mat2.row) { cout << "Cannot perform. Required condition failed."; return; } for (i = 0; i < mat1.row; i++) for (j = 0; j < mat2.col; j++) for (k = 0; k < mat2.col; k++) this->mat[i][j] += mat1.mat[i][k] * mat2.mat[k][j]; } int Matrix::comparison(const Matrix& mat1, const Matrix& mat2) { int i, j; if (mat1.row == mat2. row && mat1.col == mat2.col) for (i = 0; i < mat1.row; i++) for (j = 0; j < mat1.col; j++) if (mat1.mat[i][j] < mat2.mat[i][j]) return -1; else if (mat1.mat[i][j] > mat2.mat[i][j]) return 1; return 0; } void Matrix::display() { int i, j; cout << endl; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << setw(4) << mat[i][j] << " "; cout << endl; } } int main() { #ifdef TC clrscr(); #endif int r, c; do { cout << "Row and column: "; cin >> r >> c; } while ((r < 1 || r > MAXMATSIZE) || (c < 1 || c > MAXMATSIZE)); // Declare the objects. Matrix a(r, c), b(r, c); Matrix res(r, c); // Initialize the members matrices. cout << "\nMatrix A. "; a.initialize(1); cout << "Matrix B. "; b.initialize(1); res.initialize(0); // Display them back to user. cout << "\nMatrix A, "; a.display(); cout << "\nMatrix B, "; b.display(); cout << "\nResultant Matrix, "; res.display(); cout << "\nContinue..."; getch(); // Perform addition, and display the result. res.addition(a, b); cout << "\n\nAddition Result,"; res.display(); cout << "\nContinue..."; getch(); // Perform subtraction, and display the result. res.subtraction(a, b); cout << "\n\nSubtraction Result,"; res.display(); cout << "\nContinue..."; getch(); // Perform subtraction, and display the result. // Reinitialize resultant matrix (required for clean-up). res.initialize(0); res.multiplication(a, b); cout << "\n\nMultiplication Result,"; res.display(); cout << "\nContinue..."; getch(); // Perform subtraction, and display the result. // In the following stmt, the dummy phinomenon can be eliminated by // making comparison() function friend of the class, instead of member. int result = res.comparison(a, b); // Here res is working as dummy. cout << "\n\nComparison Result,\n"; result < 0 ? cout << "Matrix A < Matrix B" : (result == 0 ? cout << "Matrix A = Matrix B" : cout << "Matrix A > Matrix B"); cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }