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
| class Matrix{ int *p_data; int lin,col; public: Matrix(int l, int c){ lin = l; col = c; p_data = new int[lin*col]; } ~Matrix(){ delete []p_data; } int &element(int i, int j){ return *(p_data+i*col+j); } void dimension(int &l, int &c){ l = lin; c = col; } void display(){ int *p=p_data; for (int i=0; i<lin; i++){ for (int j=0; j<col; j++){ cout << *p << ' '; p++; } cout << endl; } }; };
class Vector{ int *p_data; int num; public: Vector(int n){ num = n; p_data = new int[num]; } ~Vector(){ delete []p_data; } int &element(int i) { return p_data[i]; } void dimension(int &n) { n = num; } void display(){ int *p=p_data; for (int i=0; i<num; i++,p++) cout << *p << ' '; cout << endl; } };
void multiply(Matrix &m, Vector &v, Vector &r){ int lin, col; m.dimension(lin,col); for (int i=0; i<lin; i++){ r.element(i) = 0; for (int j=0; j<col; j++) r.element(i) += m.element(i,j)*v.element(j); } }
void main(){ Matrix m(10,5); Vector v(5); Vector r(10); multiply(m,v,r); m.display(); v.display(); r.display(); }
|