nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

commit 46c548c3163a985f59af84cf0ba3c96abd1112fd
parent a6a95716c86092b57a2f7578e988a25641d319fd
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Fri, 27 Oct 2017 03:22:11 +0530

add
Diffstat:
As3/oops/matrix.cpp | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+), 0 deletions(-)

diff --git a/s3/oops/matrix.cpp b/s3/oops/matrix.cpp @@ -0,0 +1,106 @@ +#include <iostream> + +class matrix{ + int row, col, **p; +public: + int getR(){ + return this->row; + }; + int getC(){ + return this->col; + }; + matrix(){ + p = NULL; + }; + bool initMatrix(int r, int c){ + row = r; col = c; + p = new int *[row]; + for (int i = 0; i < row; i++) { + p[i] = new int [col]; + } + if(p == NULL){ + std::cout << "Insufficient memory." << '\n'; + return 1; + } + return 0; + }; + bool operator==(matrix other){ + if(this->row == other.row && this->col == other.col) + return true; + return false; + }; + + matrix operator+(matrix other){ + matrix r; + if(r.initMatrix(other.getR(), other.getC())) + std::cout << "Insufficient memory." << '\n'; + else{ + for(int i = 0; i < row; i++) + for(int j = 0; j < col; j++) + r.p[i][j] = this->p[i][j] + other.p[i][j]; + return r; + } + }; + + matrix operator-(matrix other){ + matrix r; + if(r.initMatrix(other.getR(), other.getC())) + std::cout << "Insufficient memory." << '\n'; + else{ + for(int i = 0; i < row; i++) + for(int j = 0; j < col; j++) + r.p[i][j] = this->p[i][j] - other.p[i][j]; + return r; + } + }; + + void getData(){ + for (int i = 0; i < row; i++) + for(int j = 0; j < col; j++) + std::cin >> p[i][j]; + }; + void operator <<(int x){ + for (int i = 0; i < row; i++){ + for(int j = 0; j < col; j++) + std::cout << p[i][j] << " "; + std::cout << std::endl; + } + }; +}; + +int main(){ + matrix m, n, a, s; + int r, c; + do{ + std::cout << "Enter rows and columns of first matrix: "; + std::cin >> r >> c; + if(r < 1 || c < 1) + std::cout << "Invalid size, try again." << '\n'; + }while (r < 1 || c < 1); + if(m.initMatrix(r, c)) + return 0; + do{ + std::cout << "Enter rows and columns of second matrix: "; + std::cin >> r >> c; + if(r < 1 && c < 1) + std::cout << "Invalid size, try again." << '\n'; + }while (r < 1 && c < 1); + if(n.initMatrix(r, c)) + return 0; + std::cout << "Enter first matrix:" << '\n'; + m.getData(); + std::cout << "Enter second matrix: " << '\n'; + n.getData(); + if(m == n) { + a = m + n; + a << 0; + s = m - n; + s << 0; + } + else { + std::cout << "Incompatible.\n"; + m << 0; + n << 0; + } + return 0; +}