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
|
#include<vector>
#include<iostream>
#include <iomanip>
using namespace std;
#define MATRIX vector<vector<double>>
MATRIX operator +(MATRIX m1,MATRIX m2)
{
MATRIX ans=m1; // dimension the answer here
for(int r=0;r<m1.size();r++)
{
for(int k=0;k<m2[0].size();k++)
{
ans[r][k]=m1[r][k]+m2[r][k];
}
}
return ans;
}
MATRIX operator -(MATRIX m1,MATRIX m2)
{
MATRIX ans=m1; // dimension the answer here
for(int r=0;r<m1.size();r++)
{
for(int k=0;k<m2[0].size();k++)
{
ans[r][k]=m1[r][k]-m2[r][k];
}
}
return ans;
}
void print(MATRIX m1)
{
for(int r=0;r<m1.size();r++)
{
for(int k=0;k<m1[0].size();k++)
{
cout<<setw(3)<<m1[r][k]<<" ";
}
cout<<endl;
}
cout<<endl;
}
int main()
{
MATRIX v1 = { {3, 4}, {0, 1}, { 2, 3} };
MATRIX v2 = { {0, -1}, {1, 0}, {10, 10} };
MATRIX x=v1+v2;
cout<<" v1 "<<endl;print(v1);
cout<<" v2 "<<endl;print(v2);
cout<<" v1+v2 "<<endl;print(x);
cout<<endl<<endl;
v1 = { {3, 4, -3}, {0, 1, 9}, { 2, 3, 4}, {5, 6, 7} };
v2 = { {0, -1, -1}, {1, 0, 2}, {10, 10, 10}, {8, -3, 2} };
x=v1-v2;
cout<<" v1 "<<endl;print(v1);
cout<<" v2 "<<endl;print(v2);
cout<<" v1-v2 "<<endl;print(x);
system("pause");
}
|