#include using namespace std; class matrix; class vector { private: int n; float *arrVec; public: vector(int n, float *arrVec); ~vector(); void display(); friend vector multiply(const matrix&, const vector&); }; vector::vector(int n, float *arrVec) { this->n = n; this->arrVec = new float[n]; for (int i = 0; i < n; i++) this->arrVec[i] = arrVec[i]; } vector::~vector() { if (arrVec != NULL) delete[]arrVec; } void vector::display() { cout << "\nVECTOR : " << endl; cout << "["; for (int i = 0; i < n; i++) { cout << arrVec[i]; if (i >= 0 && i < n - 1) cout << " , "; } cout << "]"; } class matrix { private: int m, n; float **arrMa; public: matrix(int m, int n, float **arrMa); ~matrix(); void display(); friend vector multiply(const matrix&, const vector&); }; matrix::matrix(int m, int n, float **arrMa) { this->m = m; this->n = n; this->arrMa = new float *[m]; for (int i = 0; i < m; i++) this->arrMa[i] = new float[n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) this->arrMa[i][j] = arrMa[i][j]; } } matrix::~matrix() { for (int i = 0; i < m; i++) delete[]arrMa[i]; delete[]arrMa; } void matrix::display() { cout << "\nMA TRAN : " << endl; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) cout << arrMa[i][j] << "\t"; cout << endl; } } vector multiply(const matrix &ml, const vector &vl) { float *arrC = new float[1000]; if (ml.m == vl.n) // số dòng của matrix == số cột của vector == TRUE. { for (int i = 0; i < ml.n; i++) arrC[i] = 0; vector *e = new vector(ml.n, arrC); // khởi tạo 1 vector e để lưu trữ kết quả for (int i = 0; i < ml.n; i++) { for (int j = 0; j < ml.m; j++) { e->arrVec[i] += (ml.arrMa[j][i] * vl.arrVec[j]); } } return *e; } } int main() { int a, b, c;// a là tham số truyền vào vector b,c truyền vào matrix cout << "NHAP VAO KICH THUOC MATRIX M x N := " << endl; cin >> b >> c; cout << "\nNHAP VAO MATRIX" << endl; float **arrM = new float *[b]; for (int i = 0; i < b; i++) arrM[i] = new float[c]; for (int i = 0; i < b; i++) { for (int j = 0; j < c; j++) { cout << "A[" << i << "][" << j << "]= "; cin >> arrM[i][j]; } } matrix mt(b, c, arrM); cout << "\nNHAP VAO SO CHIEU CUA VECTOR:= "; cin >> a; cout << "\nNHAP VAO VECTOR" << endl; float *arrV = new float[a]; for (int i = 0; i < a; i++) { cout << "A[" << i << "]= "; cin >> arrV[i]; } vector vt(a, arrV); system("cls"); cout << "\n\t\t-----------GIA TRI VUA NHAP-----------" << endl; mt.display(); vt.display(); cout << "\n\t\t-----------KET QUA TRA VE-----------" << endl; vector e = multiply(mt, vt); e.display(); cout << endl; system("pause"); }