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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
class Matrix{
public:
Matrix(){}
Matrix(int a, int b):row(a),col(b){
int i;
pointer=new int*[row];
for(i=0;i<row;i++)
pointer[i]=new int[col];
}
Matrix(Matrix& m){
row=m.row;
col=m.col;
int i,j;
pointer=new int*[row];
for(i=0;i<row;i++)
pointer[i]=new int[col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
pointer[i][j]=m.pointer[i][j];
}
Matrix operator=(Matrix& m){
row=m.row;
col=m.col;
int i, j;
pointer=new int*[row];
for(i=0;i<row;i++)
pointer[i]=new int[col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
pointer[i][j]=m.pointer[i][j];
return *this;
}
Matrix operator*(Matrix& m){
Matrix solution;
solution.row=row;
solution.col=m.col;
cout<<"-------------------------\n";
cout<<"M1\n";
cout<<"row: "<<row<<endl;
cout<<"col: "<<col<<endl;
cout<<"+++++++++++++++++++++++++\n";
cout<<"M2\n";
cout<<"row: "<<m.row<<endl;
cout<<"col: "<<m.col<<endl;
cout<<"+++++++++++++++++++++++++\n";
cout<<"Final row: "<<solution.row<<endl;//good
cout<<"Final col: "<<solution.col<<endl;//good
int i,j,k;
solution.pointer=new int*[solution.row]();//initialized to zero
for(i=0;i<solution.row;i++)
solution.pointer[i]=new int[solution.col]();//initialized to zero
for(i=0;i<row;i++){
for(j=0;j<m.col;j++){
for(k=0;k<col;k++){
solution.pointer[i][j]+=m.pointer[i][k]*pointer[k][j];////PROBLME HERE ON THIS LINE!!!!!!!!!!!!!!!!
}
}
}
return solution;
}
Matrix operator-(Matrix& m){
Matrix solution;
solution.row=row;
solution.col=col;
int i,j;
solution.pointer=new int*[solution.row];
for(i=0;i<solution.row;i++)
solution.pointer[i]=new int[solution.col];
for(i=0;i<solution.row;i++)
for(j=0;j<solution.col;j++)
solution.pointer[i][j]=m.pointer[i][j]-pointer[i][j];
return solution;
}
bool check_legality(Matrix& m){
if(m.row==row && m.col==col)return true;
else
return false;
}
Matrix operator+(Matrix& m){
Matrix solution;
int i, j;
solution.row=m.row;
solution.col=m.col;
solution.pointer=new int*[solution.row];
for(i=0;i<solution.row;i++)
solution.pointer[i]=new int[solution.col];
for(i=0;i<solution.row;i++)
for(j=0;j<solution.col;j++)
solution.pointer[i][j]=m.pointer[i][j]+pointer[i][j];
return solution;
}
~Matrix(){
for(int i=0;i<row;i++)
delete[]pointer[i];
delete[] pointer;
}
friend istream& operator>>(istream& in, Matrix& m);
friend ostream& operator<<(ostream& out, Matrix& m);
private:
int **pointer;
int row, col;
};
int main(){
Matrix m1(4,2), m2(2,4);
cin>>m1;
cout<<endl<<m1<<endl;
cin>>m2;
cout<<endl<<m2<<endl;
Matrix mult=m2*m1;
cout<<mult;
_getch();
return 0;
}
ostream& operator<<(ostream& out, Matrix& m){
for(int i=0;i<m.row;i++){
for(int j=0;j<m.col;j++){
out<<m.pointer[i][j]<<" ";
}
out<<endl;
}
cout<<endl;
return out;
}
istream& operator>>(istream& in, Matrix& m){
for(int i=0;i<m.row;i++){
for(int j=0;j<m.col;j++){
cout<<"Enter "<<"["<<i<<"]["<<j<<"]"<<" value: ";
in>>m.pointer[i][j];
}
}
return in;
}
|