Viết chương trình nhập vào hai ma trận a, b và thực hiện các công việc: cộng, trừ, nhân hai ma trận trên bằng các cách: - Dùng phương thức - Dùng hàm tự do - Dùng hàm bạn - Dùng phương thức toán tử (+, -, *) Phần mềm sử dụng: Visual Studio Sử dụng phương thức CODE: Select All #include <iostream>using namespace std;#define max 10int n;class matran{private: int mt[max][max];public: matran() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mt[i][j] = 0; } } } void nhapsl() { cout << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << "Ma tran thu [" << i << "][" << j << "]: "; cin >> mt[i][j]; } } cout << endl; } void insl() { for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << mt[i][j] << " "; } cout << endl; } } matran cong(matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] +=mt[i][j] + b.mt[i][j]; } } return c; } matran hieu(matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += mt[i][j] - b.mt[i][j]; } } return c; } matran nhan(matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += mt[i][j] * b.mt[i][j]; } } return c; }};void main(){ matran a,b,c,d; cout << "nhap cap cua ma tran: "; cin >> n; cout << "Nhap ma tran thu nhat:"; a.nhapsl(); cout << "\nNhap ma tran thu hai:"; b.nhapsl(); system("cls"); cout << "\n Ma tran A:\n"; a.insl(); cout << "\n Ma tran B:\n"; b.insl(); c = a.cong(b); cout << "\n Tong 2 ma tran AB:\n"; c.insl(); c = a.hieu(b); cout << "\n Hieu 2 ma tran AB:\n"; c.insl(); c = a.nhan(b); cout << "\n nhan 2 ma tran AB:\n"; c.insl(); system("pause");} Sử dụng hàm tự do CODE: Select All #include <iostream>using namespace std;#define max 10int n;class matran{public: int mt[max][max]; matran() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mt[i][j] = 0; } } } void nhapsl() { cout << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << "Ma tran thu [" << i << "][" << j << "]: "; cin >> mt[i][j]; } } cout << endl; } void insl() { for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << mt[i][j] << " "; } cout << endl; } }}; matran cong(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] +=a.mt[i][j] + b.mt[i][j]; } } return c; } matran hieu(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] - b.mt[i][j]; } } return c; } matran nhan(matran a,matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] * b.mt[i][j]; } } return c; }void main(){ matran a,b,c,d; cout << "nhap cap cua ma tran: "; cin >> n; cout << "Nhap ma tran thu nhat:"; a.nhapsl(); cout << "\nNhap ma tran thu hai:"; b.nhapsl(); system("cls"); cout << "\n Ma tran A:\n"; a.insl(); cout << "\n Ma tran B:\n"; b.insl(); c = cong(a,b); cout << "\n Tong 2 ma tran AB:\n"; c.insl(); c = hieu(a,b); cout << "\n Hieu 2 ma tran AB:\n"; c.insl(); c = nhan(a,b); cout << "\n nhan 2 ma tran AB:\n"; c.insl(); system("pause");} Sử dụng hàm bạn CODE: Select All #include <iostream>using namespace std;#define max 10int n;class matran{private: int mt[max][max];public: matran() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mt[i][j] = 0; } } } void nhapsl() { cout << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << "Ma tran thu [" << i << "][" << j << "]: "; cin >> mt[i][j]; } } cout << endl; } void insl() { for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << mt[i][j] << " "; } cout << endl; } } friend matran cong(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] +=a.mt[i][j] + b.mt[i][j]; } } return c; } friend matran hieu(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] - b.mt[i][j]; } } return c; } friend matran nhan(matran a,matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] * b.mt[i][j]; } } return c; }};void main(){ matran a,b,c,d; cout << "nhap cap cua ma tran: "; cin >> n; cout << "Nhap ma tran thu nhat:"; a.nhapsl(); cout << "\nNhap ma tran thu hai:"; b.nhapsl(); system("cls"); cout << "\n Ma tran A:\n"; a.insl(); cout << "\n Ma tran B:\n"; b.insl(); c = cong(a,b); cout << "\n Tong 2 ma tran AB:\n"; c.insl(); c = hieu(a,b); cout << "\n Hieu 2 ma tran AB:\n"; c.insl(); c = nhan(a,b); cout << "\n nhan 2 ma tran AB:\n"; c.insl(); system("pause");} Sử dụng toán tử CODE: Select All #include <iostream>using namespace std;#define max 10int n;class matran{public: int mt[max][max]; matran() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mt[i][j] = 0; } } } void nhapsl() { cout << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << "Ma tran thu [" << i << "][" << j << "]: "; cin >> mt[i][j]; } } cout << endl; } void insl() { for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << mt[i][j] << " "; } cout << endl; } }};matran operator +(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] +=a.mt[i][j] + b.mt[i][j]; } } return c; } matran operator -(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] - b.mt[i][j]; } } return c; } matran operator *(matran a, matran b) { matran c; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { c.mt[i][j] += a.mt[i][j] * b.mt[i][j]; } } return c; }void main(){ matran a,b,c,d; cout << "nhap cap cua ma tran: "; cin >> n; cout << "Nhap ma tran thu nhat:"; a.nhapsl(); cout << "\nNhap ma tran thu hai:"; b.nhapsl(); system("cls"); cout << "\n Ma tran A:\n"; a.insl(); cout << "\n Ma tran B:\n"; b.insl(); c = a + b; cout << "\n Tong 2 ma tran AB:\n"; c.insl(); c = a - b; cout << "\n Hieu 2 ma tran AB:\n"; c.insl(); c = a * b; cout << "\n nhan 2 ma tran AB:\n"; c.insl(); system("pause");}