Advertisement
dimon-torchila

Untitled

Oct 16th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. void input(int** arr, int n1, int n2)
  4. {
  5.     cout << "Заполните матрицу: ";
  6.     for (int i = 0; i < n1; i++)
  7.     {
  8.         for (int j = 0; j < n2; j++)
  9.             cin >> arr[i][j];
  10.     }  
  11. }
  12. void output(int** arr, int n1, int n2)
  13. {
  14.     for (int i = 0; i < n1; i++)
  15.     {
  16.         for (int j = 0; j < n2; j++)
  17.             cout << arr[i][j] << ' ';
  18.         cout << endl;
  19.     }
  20. }
  21. int main()
  22. {
  23.     setlocale(0, "");
  24.     int n1_1, n1_2, n2_1, n2_2;
  25.     do
  26.     {
  27.         cout << "Введите размер первой матрицы: ";
  28.         cin >> n1_1 >> n1_2;
  29.         cout << "Введите размер второй матрицы: ";
  30.         cin >> n2_1 >> n2_2;
  31.         if (n1_2 != n2_1)
  32.         {
  33.             cout << "Данные матрицы невозможно перемножить.... " << endl;
  34.         }
  35.     } while (n1_2 != n2_1);
  36.  
  37.     int** ptr_arr1 = new int* [n1_1];
  38.     for (int i = 0; i < n1_1; i++)
  39.         ptr_arr1[i] = new int[n1_2];
  40.     int** ptr_arr2 = new int* [n2_1];
  41.     for (int i = 0; i < n2_1; i++)
  42.         ptr_arr2[i] = new int[n2_2];
  43.     int** ptr_arr3 = new int* [n1_1];
  44.     for (int i = 0; i < n1_1; i++)
  45.         ptr_arr3[i] = new int[n2_2];
  46.     for (int i = 0; i < n1_1; i++)
  47.         for (int j = 0; j < n2_2; j++)
  48.             ptr_arr3[i][j] = 0;
  49.     input(ptr_arr1, n1_1, n1_2);
  50.     input(ptr_arr2, n2_1, n2_2);
  51.     cout << endl;
  52.     for (int i = 0; i < n1_1; i++)
  53.         for (int j = 0; j < n2_2; j++)
  54.             for (int k = 0; k < n1_2; k++)
  55.                 ptr_arr3 += ptr_arr1[i][k] * ptr_arr2[k][j];
  56.     output(ptr_arr1, n1_1, n1_2);
  57.     cout << endl;
  58.     output(ptr_arr2, n2_1, n2_2);
  59.     cout << " " << endl;
  60.     output(ptr_arr3, n1_1, n2_2);
  61.     for (int i = 0; i < n1_1; i++)
  62.     {
  63.         delete[] ptr_arr1[i];
  64.         delete[] ptr_arr2[i];
  65.         delete[] ptr_arr3[i];
  66.     }
  67.     delete[] ptr_arr1;
  68.     delete[] ptr_arr2;
  69.     delete[] ptr_arr3;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement