Advertisement
Guest User

enver

a guest
Oct 31st, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. class DimensionMismatchException//Returns error message,message is "dimension mismatch exception!"
  2. {
  3. public:
  4. DimensionMismatchException();
  5. const char * what();//returns error message
  6. private:
  7. const char *msg;
  8. };
  9.  
  10. class InvalidIndexException//Returns error message,message is "invalid index exception!"
  11. {
  12. public:
  13. InvalidIndexException();
  14. const char * what();//returns error message
  15. private:
  16. const char *msg;
  17. };
  18.  
  19. template <class T>
  20. class Matrix
  21. {
  22. private:
  23. T** data; // matrix elements stored here
  24. int rows; // number of rows
  25. int cols; // number of columns
  26. public:
  27. Matrix(int numRows = 0, int numCols = 0); // makes storage allocation but leaves it uninitialized, for 0,0 dont allocate memory
  28. Matrix(T const* const* inputData, int numRows, int numCols);
  29. Matrix(const Matrix& rhs);
  30. ~Matrix();
  31.  
  32. Matrix& operator=(const Matrix& rhs);
  33.  
  34. // all of the below functions may throw dimension mismatch exception if the given matrix's dimensions does not match with the initial dimensions
  35. Matrix operator+(const Matrix& rhs) const; // element-wise addition of two matrices
  36. Matrix operator-(const Matrix& rhs) const; // element-wise subtraction of two matrices
  37. Matrix operator*(const Matrix& rhs) const; // multiplication of two matrices, dot product
  38.  
  39. // 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
  40. T operator()(int r, int c) const; // returns the element value at row r and column c
  41. T& operator()(int r, int c); // returns reference of the element value at row r and column c
  42.  
  43. int getRows() const; // returns the number of rows
  44. int getCols() const; // returns the number of columns
  45.  
  46. 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
  47. };
  48.  
  49. /* DO NOT MODIFY ANYTHING ABOVE THIS LINE.
  50. IT WILL BE REPLACED BY THE ORIGINAL INTERFACE,
  51. AND IF YOU MAKE CHANGES THEY WILL BE LOST.
  52. INSERT YOUR IMPLEMENTATION BELOW THIS LINE. */
  53.  
  54.  
  55. //Your solutions will come to here
  56.  
  57. #include<iostream>
  58. using namespace std;
  59. DimensionMismatchException::DimensionMismatchException() : msg("dimension mismatch exception!") {}
  60. const char* DimensionMismatchException::what() {return msg;}
  61. //=================================
  62.  
  63. InvalidIndexException::InvalidIndexException() : msg("invalid index exception!") {}
  64. const char* InvalidIndexException::what() {return msg;}
  65.  
  66. //=================================
  67. template <class T>
  68. Matrix<T>::Matrix(int numRows, int numCols)
  69. :rows(numRows),cols(numCols)
  70. { int i,k;
  71. data= new T*[rows];
  72.  
  73. for(i=0;i<rows;i++)
  74. {
  75. data[i]=new T[cols];
  76. }
  77. }
  78. //=================================
  79. template <class T>
  80. Matrix<T>::Matrix(T const* const* inputData, int numRows, int numCols)
  81. :rows(numRows),cols(numCols)
  82. {
  83. int i,k;
  84. data= new T*[rows];
  85.  
  86. for(i=0;i<rows;i++)
  87. {
  88. data[i]=new T[cols];
  89. for(k=0;k<cols;k++){data[i][k]=inputData[i][k]; }
  90. }
  91.  
  92. }
  93.  
  94. //=================================
  95. template <class T>
  96. Matrix<T>::Matrix(const Matrix& rhs)
  97. {
  98. rows=rhs.rows;
  99. cols=rhs.cols;
  100. int i,k;
  101. data= new T*[rows];
  102. for(i=0;i<rows;i++)
  103. {
  104. data[i]=new T[cols];
  105. for(k=0;k<cols;k++){data[i][k]=rhs.data[i][k]; }
  106. }
  107.  
  108. }
  109. //=================================
  110. template <class T>
  111. Matrix<T>::~Matrix()
  112. { if (NULL != this->data)
  113. {
  114. for (int i=0;i<rows;i++)
  115. delete[] data[i];
  116.  
  117. delete[] data;
  118. }
  119. }
  120.  
  121. //=================================
  122. template <class T>
  123. Matrix<T> & Matrix<T>::operator=(const Matrix& rhs)
  124. {
  125. if(this != &rhs )
  126. {
  127. int i,k;
  128. rows=rhs.rows;
  129. cols=rhs.cols;
  130. delete [] data;
  131. data= new T*[rows];
  132. for(i=0;i<rows;i++)
  133. {
  134. data[i]=new T[cols];
  135. for(k=0;k<cols;k++){data[i][k]=rhs.data[i][k]; }
  136. }
  137. }
  138. return *this;
  139. }
  140. //=================================
  141. template <class T>
  142. Matrix<T> Matrix<T>::operator+(const Matrix& rhs) const
  143. {
  144.  
  145. if(rhs.cols==this->cols && rhs.rows==this->rows){
  146. int i,k;
  147. Matrix<T> res;
  148. res.rows=rhs.rows;
  149. res.cols=rhs.cols;
  150. res.data= new T*[rows];
  151. for(i=0;i<res.rows;i++)
  152. {
  153. res.data[i]=new T[cols];
  154. for(k=0;k<res.cols;k++){res.data[i][k]=rhs.data[i][k]+this->data[i][k]; }
  155. }
  156. return res;
  157. }
  158. throw DimensionMismatchException();
  159. }
  160. //=================================
  161. template <class T>
  162. Matrix<T> Matrix<T>::operator-(const Matrix& rhs) const
  163. {
  164.  
  165. if(rhs.cols==this->cols && rhs.rows==this->rows){
  166. int i,k;
  167. Matrix<T> res;
  168. res.rows=rhs.rows;
  169. res.cols=rhs.cols;
  170. res.data= new T*[rows];
  171. for(i=0;i<res.rows;i++)
  172. {
  173. res.data[i]=new T[cols];
  174. for(k=0;k<res.cols;k++){res.data[i][k]=-rhs.data[i][k]+this->data[i][k]; }
  175. }
  176. return res;
  177. }
  178. throw DimensionMismatchException();
  179.  
  180. }
  181. //=================================
  182. template <class T>
  183. Matrix<T> Matrix<T>::operator*(const Matrix& rhs) const
  184. {
  185.  
  186. if(this->cols==rhs.rows){
  187. int i,k,t;
  188. Matrix<T> res;
  189.  
  190. res.rows=this->rows;
  191. res.cols=rhs.cols;
  192. res.data= new T*[res.rows];
  193.  
  194. for(i=0;i<res.rows;i++)
  195. {
  196. res.data[i]=new T[rhs.cols];
  197. for(k=0;k<res.cols;k++)
  198. {
  199. for(t=0;t<rhs.rows;t++)
  200. {
  201. res.data[i][k]+=this->data[i][t]*rhs.data[t][k];
  202. }
  203. }
  204. }
  205. return res;
  206. }
  207. throw DimensionMismatchException();
  208.  
  209. }
  210.  
  211. //=================================
  212. template <class T>
  213. T Matrix<T>::operator()(int r, int c) const
  214. {
  215.  
  216. if((0<r && r<=this->rows) && (0<c && c<=this->cols))
  217. {
  218. return this->data[r-1][c-1];
  219. }
  220. else {throw InvalidIndexException();}
  221.  
  222.  
  223. }
  224. //=================================
  225. template <class T>
  226. T& Matrix<T>::operator()(int r, int c)
  227. {
  228.  
  229. if((0<r && r<=this->rows) && (0<c && c<=this->cols))
  230. {
  231. return this->data[r-1][c-1];
  232. }
  233. else {throw InvalidIndexException();}
  234. }
  235. //=================================
  236. template <class T>
  237. int Matrix<T>::getRows() const {return rows;}
  238. //=================================
  239. template <class T>
  240. int Matrix<T>::getCols() const {return cols;}
  241. //=================================
  242.  
  243. template <class T>
  244. void Matrix<T>::print() const
  245. {
  246. int i,k;
  247. for(i=0;i<rows;i++)
  248. {
  249. for(k=0;k<cols-1;k++)
  250. cout<<data[i][k]<<"\t";
  251. cout<<data[i][k]<<endl;
  252. }
  253. }
  254. //=================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement