1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <stdlib.h>
#include "matrix.h"
#include "time.h"
using namespace std;
//OPERATORS OVERLOAD FOR MATRICES
//ADITION
matrix operator+(matrix& M1,matrix& M2){
int n1,m1,n2,m2;
double p;//value to fill the matrix
n1=M1.nrows();
m1=M1.ncolumns();
n2=M2.nrows();
m2=M2.ncolumns();
matrix result(n1,m1);
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
p=M1.get(i,j)+M2.get(i,j);
result.fill(p,i,j);
}
}
return result;
}
//SUBSTRACTION
matrix operator-(matrix& M1,matrix& M2){
int n1,m1,n2,m2;
double p;//value to fill the matrix
n1=M1.nrows();
m1=M1.ncolumns();
n2=M2.nrows();
m2=M2.ncolumns();
if(n1!=n2)||(n2!=m2){
cout<<"matrices must be of the same size. Program terminated "<<endl;
}
matrix result(n1,m1);
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
p=M1.get(i,j)-M2.get(i,j);
result.fill(p,i,j);
}
}
return result;
}
int main(){
srand(time(NULL));
matrix A(2,2);
matrix B(3,2),C;
A.double_rand(0,1);
B.double_rand(0,1);
A.printm();
cout<<endl<<endl;
B.printm();
C=A+B;
C.printm();
// clean matrices objects;
A.clean();
B.clean();
C.clean();
return 0;
}
|