Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct matrix{
- int rows;
- int cols;
- vector<vector<float> > data;
- matrix();
- matrix(int nrows, int ncols,vector<vector<float> > ndata);
- void display();
- };
- matrix * operator *(matrix &jedna, matrix &druga);
- bool mult_condition(matrix &jedna, matrix &druga);
- matrix::matrix(){
- rows=0;
- cols=0;
- }
- matrix::matrix(int nrows, int ncols, vector<vector<float> > ndata)
- {
- rows = nrows;
- cols = ncols;
- data = ndata;
- data.resize(rows,vector<float>(cols));
- }
- void matrix::display(){
- int k=0;
- for(vector<vector<float> >::iterator i= data.begin(); i != data.end(); ++i, ++k){
- int l=0;
- for(std::vector<float>::iterator j= data.at(k).begin(); j != data.at(k).end(); ++j, ++l){
- cout << data.at(k).at(l) << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- bool mult_condition(matrix &jedna, matrix &druga){
- return (jedna.cols==druga.rows);
- }
- matrix * operator *(matrix &jedna, matrix &druga){
- if (!mult_condition(jedna, druga)) return NULL;
- vector<vector<float> > result(jedna.rows, vector<float>(druga.cols ,1));
- float tmp;
- for(int i=0; i<jedna.rows; i++){
- for (int j=0; j<druga.cols; j++){
- tmp = 0;
- for(int k=0; k<jedna.cols; k++){
- tmp += jedna.data[i][k]*druga.data[k][j];
- }
- result[i][j] = tmp;
- }
- }
- matrix * wynik = new matrix(jedna.rows, druga.cols, result);
- wynik->display();
- return wynik;
- }
- int main()
- {
- matrix a(3, 2, {{1, 2}, {1, 1}, {0, 2}});
- matrix I(2, 2, {{1, 0}, {0, 1}});
- a.display();
- I.display();
- matrix * b = a*I;
- delete b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement