wohyperion

Matrix Multiply [Умножение матриц]

Feb 7th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void MatrixMultiply(int **first_matrix, int **second_matrix, int row, int col, int s){
  8.     int **result = new int *[row];
  9.     for (int i = 0; i < row; i++) {
  10.         result[i] = new int[col];
  11.     }
  12.     cout << "Результат умножения матриц: \n";
  13.     for (int i = 0; i < row; i++) {
  14.         for (int j = 0; j < col; j++) {
  15.             for (int inn = 0; inn < s; inn++) {
  16.                 result[i][j] += first_matrix[i][inn] * second_matrix[inn][j];
  17.             }
  18.             cout << result[i][j] << ' ';
  19.         }
  20.         cout << '\n';
  21.     }
  22. }
  23.  
  24. int main(){
  25.     setlocale(LC_ALL, "russian");
  26.     int n, m, k, l;
  27.     cout << "Введите размерность матриц(n, m, k, l): ";
  28.     cin >> n >> m >> k >> l;
  29.     int **a = new int *[n];
  30.     for (int i = 0; i < n; i++) {
  31.         a[i] = new int [m];
  32.     }
  33.     int **b = new int *[k];
  34.     for (int i = 0; i < k; i++) {
  35.         b[i] = new int [l];
  36.     }
  37.     cout << "Введите элементы первой матрицы: ";
  38.     for (int i = 0; i < n; i++){
  39.         for (int j = 0; j < m; j++){
  40.             cin >> a[i][j];
  41.         }
  42.     }
  43.     cout << "Введите элементы второй матрицы: ";
  44.     for (int i = 0; i < k; i++){
  45.         for (int j = 0; j < l; j++){
  46.             cin >> b[i][j];
  47.         }
  48.     }
  49.     if (m == k) {
  50.         MatrixMultiply(a, b, n, l, m);
  51.     }
  52.     system("pause");
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment