Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. void vop(int **arr,int row, int cols)/*Функция создания и заполнения массива*/
  9. {
  10.    
  11.     for (int i = 0; i < row; i++)
  12.     {
  13.         for (int j = 0; j < cols; j++)
  14.         {
  15.             cout << "Введите " << j + 1 << " элемент " << i + 1 << " строки: ";
  16.             cin >> arr[i][j];
  17.  
  18.         }
  19.  
  20.         cout << endl;
  21.     }
  22.  
  23.     cout << endl;
  24.  
  25.     for (int i = 0; i < row; i++)
  26.     {
  27.         for (int j = 0; j < cols; j++)
  28.         {
  29.             cout << arr[i][j] << "   ";
  30.         }
  31.  
  32.         cout << endl;
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     setlocale(LC_ALL, "rus");
  39.  
  40.  
  41.    
  42.     int row;
  43.     int cols;
  44.     cout << "===========Первая матрица=============" << endl;
  45.  
  46.     cout << "Введите количество строк: ";
  47.     cin >> row;
  48.     cout << endl;
  49.     cout << "Введите колочесиво столбцов: ";
  50.     cin >> cols;
  51.     cout << endl;
  52.  
  53.     int** arr = new int* [row];
  54.  
  55.     for (int i = 0; i < row; i++)
  56.     {
  57.         arr[i] = new int[cols];
  58.     }
  59.  
  60.    
  61.  
  62.     vop(arr, row, cols);
  63.  
  64.  
  65.     /*================================================================================================================*/
  66.  
  67.     int row1;
  68.     int cols1;
  69.     cout << "===========Вторая матрица=============" << endl;
  70.  
  71.     cout << "Введите количество строк: ";
  72.     cin >> row1;
  73.     cout << endl;
  74.     cout << "Введите колочесиво столбцов: ";
  75.     cin >> cols1;
  76.     cout << endl;
  77.  
  78.     int** arr1 = new int* [row1];
  79.  
  80.     for (int i = 0; i < row1; i++)
  81.     {
  82.         arr1[i] = new int[cols1];
  83.     }
  84.  
  85.     vop(arr1, row1, cols1);
  86.  
  87.  
  88.  
  89.     /*================================================================================================================*/
  90.  
  91.     int sum = 0;
  92.     int el = 0;
  93.  
  94.     int row2;
  95.     int cols2;
  96.  
  97.     row2 = row;
  98.  
  99.     cols2 = cols1;
  100.  
  101.  
  102.     int** arr2 = new int* [row2];
  103.  
  104.     for (int i = 0; i < row2; i++)
  105.     {
  106.         arr2[i] = new int[cols2];
  107.     }
  108.  
  109.     if (cols = row1)
  110.     {
  111.  
  112.         for (int k = 0; k < row; k++)
  113.         {
  114.  
  115.             for (int i = 0; i < cols1; i++)
  116.             {
  117.                 for (int j = 0; j < cols; j++)
  118.                 {
  119.                     el = arr[k][j] * arr1[j][i];
  120.                     sum += el;
  121.                 }
  122.  
  123.                 arr2[k][i] = sum;
  124.  
  125.                 sum = 0;
  126.  
  127.             }
  128.  
  129.         }
  130.  
  131.         /*================================================================================================================*/
  132.  
  133.         cout << "===========Сумма матриц===================" << endl;
  134.         cout << endl;
  135.         for (int i = 0; i < row; i++)
  136.         {
  137.             for (int j = 0; j < cols1; j++)
  138.             {
  139.                 cout << arr2[i][j] << "   ";
  140.             }
  141.  
  142.             cout << endl;
  143.         }
  144.  
  145.     }
  146.  
  147.     else
  148.     {
  149.         cout << "Количество стобцов в первой матрице должно быть равно количеству строк во второй!" << endl;
  150.  
  151.     }
  152.  
  153.     /*Удаление массивов*/
  154.  
  155.     for (int i = 0; i < row; i++)
  156.     {
  157.         delete[] arr[i];
  158.     }
  159.  
  160.     for (int i = 0; i < row1; i++)
  161.     {
  162.         delete[] arr1[i];
  163.     }
  164.  
  165.  
  166.     for (int i = 0; i < row; i++)
  167.     {
  168.         delete[] arr2[i];
  169.     }
  170.  
  171.     delete[] arr;
  172.     delete[] arr1;
  173.     delete[] arr2;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement