dzungchaos

C++ "Tổng tích 2 ma trận"

Nov 6th, 2019
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.     int n, **A, **B, **C;
  6.     cout << ("Nhap n = ");
  7.     cin >> n;
  8.     // cap phat bo nho
  9.     A = new int*[n];
  10.     B = new int*[n];
  11.     C = new int*[n];
  12.     for(int i = 0; i < n; i++){
  13.         A[i] = new int[n];
  14.         B[i] = new int[n];
  15.         C[i] = new int[n];
  16.     }
  17.     // nhap cac phan tu
  18.     for(int i = 0; i < n; i++)
  19.         for(int j = 0; j < n; j++){
  20.             cout << "A[" << i << "][" << j << "] = ";
  21.             cin >> A[i][j];
  22.         }
  23.     for(int i = 0; i < n; i++)
  24.         for(int j = 0; j < n; j++){
  25.             cout << "B[" << i << "][" << j << "] = ";
  26.             cin >> B[i][j];
  27.         }
  28.     // tinh tong ma tran
  29.     for(int i = 0; i < n; i++)
  30.         for(int j = 0; j < n; j++){
  31.             C[i][j] = A[i][j] + B[i][j];
  32.         }
  33.     cout << "Tong hai ma tran la: \n";
  34.     for(int i = 0; i < n; i++){
  35.         for(int j = 0; j < n; j++)
  36.             cout << C[i][j] << " ";
  37.         cout << "\n";
  38.     }
  39.     // tinh tich ma tran
  40.     for(int i = 0; i < n; i++)
  41.         for(int j = 0; j < n; j++){
  42.             C[i][j] = 0;
  43.             for(int k = 0; k < n; k++)
  44.                 C[i][j] += A[i][k]*B[k][j];
  45.         }
  46.     cout << "Tich hai ma tran la: \n";
  47.     for(int i = 0; i < n; i++){
  48.         for(int j = 0; j < n; j++)
  49.             cout << C[i][j] << "  ";
  50.         cout << "\n";
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment