Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DimensionMismatchException//Returns error message,message is "dimension mismatch exception!"
- {
- public:
- DimensionMismatchException();
- const char * what();//returns error message
- private:
- const char *msg;
- };
- class InvalidIndexException//Returns error message,message is "invalid index exception!"
- {
- public:
- InvalidIndexException();
- const char * what();//returns error message
- private:
- const char *msg;
- };
- template <class T>
- class Matrix
- {
- private:
- T** data; // matrix elements stored here
- int rows; // number of rows
- int cols; // number of columns
- public:
- Matrix(int numRows = 0, int numCols = 0); // makes storage allocation but leaves it uninitialized, for 0,0 dont allocate memory
- Matrix(T const* const* inputData, int numRows, int numCols);
- Matrix(const Matrix& rhs);
- ~Matrix();
- Matrix& operator=(const Matrix& rhs);
- // all of the below functions may throw dimension mismatch exception if the given matrix's dimensions does not match with the initial dimensions
- Matrix operator+(const Matrix& rhs) const; // element-wise addition of two matrices
- Matrix operator-(const Matrix& rhs) const; // element-wise subtraction of two matrices
- Matrix operator*(const Matrix& rhs) const; // multiplication of two matrices, dot product
- // these two functions may throw invalid index exception, it does this if the values are negative or if they are out of bounds or if they are 0
- T operator()(int r, int c) const; // returns the element value at row r and column c
- T& operator()(int r, int c); // returns reference of the element value at row r and column c
- int getRows() const; // returns the number of rows
- int getCols() const; // returns the number of columns
- void print() const; // prints the matrix with each column element separated by a tab and each row element in a new line print a newline after the last row
- };
- /* DO NOT MODIFY ANYTHING ABOVE THIS LINE.
- IT WILL BE REPLACED BY THE ORIGINAL INTERFACE,
- AND IF YOU MAKE CHANGES THEY WILL BE LOST.
- INSERT YOUR IMPLEMENTATION BELOW THIS LINE. */
- //Your solutions will come to here
- #include<iostream>
- using namespace std;
- DimensionMismatchException::DimensionMismatchException() : msg("dimension mismatch exception!") {}
- const char* DimensionMismatchException::what() {return msg;}
- //=================================
- InvalidIndexException::InvalidIndexException() : msg("invalid index exception!") {}
- const char* InvalidIndexException::what() {return msg;}
- //=================================
- template <class T>
- Matrix<T>::Matrix(int numRows, int numCols)
- :rows(numRows),cols(numCols)
- { int i,k;
- data= new T*[rows];
- for(i=0;i<rows;i++)
- {
- data[i]=new T[cols];
- }
- }
- //=================================
- template <class T>
- Matrix<T>::Matrix(T const* const* inputData, int numRows, int numCols)
- :rows(numRows),cols(numCols)
- {
- int i,k;
- data= new T*[rows];
- for(i=0;i<rows;i++)
- {
- data[i]=new T[cols];
- for(k=0;k<cols;k++){data[i][k]=inputData[i][k]; }
- }
- }
- //=================================
- template <class T>
- Matrix<T>::Matrix(const Matrix& rhs)
- {
- rows=rhs.rows;
- cols=rhs.cols;
- int i,k;
- data= new T*[rows];
- for(i=0;i<rows;i++)
- {
- data[i]=new T[cols];
- for(k=0;k<cols;k++){data[i][k]=rhs.data[i][k]; }
- }
- }
- //=================================
- template <class T>
- Matrix<T>::~Matrix()
- { if (NULL != this->data)
- {
- for (int i=0;i<rows;i++)
- delete[] data[i];
- delete[] data;
- }
- }
- //=================================
- template <class T>
- Matrix<T> & Matrix<T>::operator=(const Matrix& rhs)
- {
- if(this != &rhs )
- {
- int i,k;
- rows=rhs.rows;
- cols=rhs.cols;
- delete [] data;
- data= new T*[rows];
- for(i=0;i<rows;i++)
- {
- data[i]=new T[cols];
- for(k=0;k<cols;k++){data[i][k]=rhs.data[i][k]; }
- }
- }
- return *this;
- }
- //=================================
- template <class T>
- Matrix<T> Matrix<T>::operator+(const Matrix& rhs) const
- {
- if(rhs.cols==this->cols && rhs.rows==this->rows){
- int i,k;
- Matrix<T> res;
- res.rows=rhs.rows;
- res.cols=rhs.cols;
- res.data= new T*[rows];
- for(i=0;i<res.rows;i++)
- {
- res.data[i]=new T[cols];
- for(k=0;k<res.cols;k++){res.data[i][k]=rhs.data[i][k]+this->data[i][k]; }
- }
- return res;
- }
- throw DimensionMismatchException();
- }
- //=================================
- template <class T>
- Matrix<T> Matrix<T>::operator-(const Matrix& rhs) const
- {
- if(rhs.cols==this->cols && rhs.rows==this->rows){
- int i,k;
- Matrix<T> res;
- res.rows=rhs.rows;
- res.cols=rhs.cols;
- res.data= new T*[rows];
- for(i=0;i<res.rows;i++)
- {
- res.data[i]=new T[cols];
- for(k=0;k<res.cols;k++){res.data[i][k]=-rhs.data[i][k]+this->data[i][k]; }
- }
- return res;
- }
- throw DimensionMismatchException();
- }
- //=================================
- template <class T>
- Matrix<T> Matrix<T>::operator*(const Matrix& rhs) const
- {
- if(this->cols==rhs.rows){
- int i,k,t;
- Matrix<T> res;
- res.rows=this->rows;
- res.cols=rhs.cols;
- res.data= new T*[res.rows];
- for(i=0;i<res.rows;i++)
- {
- res.data[i]=new T[rhs.cols];
- for(k=0;k<res.cols;k++)
- {
- for(t=0;t<rhs.rows;t++)
- {
- res.data[i][k]+=this->data[i][t]*rhs.data[t][k];
- }
- }
- }
- return res;
- }
- throw DimensionMismatchException();
- }
- //=================================
- template <class T>
- T Matrix<T>::operator()(int r, int c) const
- {
- if((0<r && r<=this->rows) && (0<c && c<=this->cols))
- {
- return this->data[r-1][c-1];
- }
- else {throw InvalidIndexException();}
- }
- //=================================
- template <class T>
- T& Matrix<T>::operator()(int r, int c)
- {
- if((0<r && r<=this->rows) && (0<c && c<=this->cols))
- {
- return this->data[r-1][c-1];
- }
- else {throw InvalidIndexException();}
- }
- //=================================
- template <class T>
- int Matrix<T>::getRows() const {return rows;}
- //=================================
- template <class T>
- int Matrix<T>::getCols() const {return cols;}
- //=================================
- template <class T>
- void Matrix<T>::print() const
- {
- int i,k;
- for(i=0;i<rows;i++)
- {
- for(k=0;k<cols-1;k++)
- cout<<data[i][k]<<"\t";
- cout<<data[i][k]<<endl;
- }
- }
- //=================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement