Advertisement
Guest User

macierze

a guest
Apr 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct matrix{
  7.     int rows;
  8.     int cols;
  9.     vector<vector<float> > data;
  10.  
  11.     matrix();
  12.     matrix(int nrows, int ncols,vector<vector<float> > ndata);
  13.     void display();
  14. };
  15.  
  16. matrix * operator *(matrix &jedna, matrix &druga);
  17. bool mult_condition(matrix &jedna, matrix &druga);
  18.  
  19. matrix::matrix(){
  20.     rows=0;
  21.     cols=0;
  22. }
  23.  
  24. matrix::matrix(int nrows, int ncols, vector<vector<float> > ndata)
  25. {
  26.     rows = nrows;
  27.     cols = ncols;
  28.     data = ndata;
  29.     data.resize(rows,vector<float>(cols));
  30. }
  31.  
  32. void matrix::display(){
  33.  
  34.     int k=0;
  35.     for(vector<vector<float> >::iterator i= data.begin(); i != data.end(); ++i, ++k){
  36.         int l=0;
  37.         for(std::vector<float>::iterator j= data.at(k).begin(); j != data.at(k).end(); ++j, ++l){
  38.             cout << data.at(k).at(l) << " ";
  39.         }
  40.         cout << endl;
  41.     }
  42.  
  43.     cout << endl;
  44. }
  45.  
  46. bool mult_condition(matrix &jedna, matrix &druga){
  47.     return (jedna.cols==druga.rows);
  48. }
  49.  
  50. matrix * operator *(matrix &jedna, matrix &druga){
  51.     if (!mult_condition(jedna, druga)) return NULL;
  52.  
  53.     vector<vector<float> >  result(jedna.rows, vector<float>(druga.cols ,1));
  54.     float tmp;
  55.  
  56.     for(int i=0; i<jedna.rows; i++){
  57.         for (int j=0; j<druga.cols; j++){
  58.         tmp = 0;
  59.             for(int k=0; k<jedna.cols; k++){
  60.                 tmp += jedna.data[i][k]*druga.data[k][j];
  61.             }
  62.             result[i][j] = tmp;
  63.         }
  64.     }
  65.  
  66.  
  67.     matrix * wynik = new matrix(jedna.rows, druga.cols, result);
  68.     wynik->display();
  69.     return wynik;
  70. }
  71.  
  72.  
  73. int main()
  74. {
  75.     matrix a(3, 2, {{1, 2}, {1, 1}, {0, 2}});
  76.     matrix I(2, 2, {{1, 0}, {0, 1}});
  77.  
  78.     a.display();
  79.     I.display();
  80.  
  81.     matrix * b = a*I;
  82.  
  83.     delete b;
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement