Advertisement
kompilainenn

Untitled

Nov 20th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. //matrix multiple
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8.     std::cout << "Enter rows amount of matrix 1: ";
  9.     int row_matr_1;
  10.     std::cin >> row_matr_1;
  11.     std::cout << std::endl;
  12.  
  13.     std::cout << "Enter columns amount of matrix 1: ";
  14.     int col_matr_1;
  15.     std::cin >> col_matr_1;
  16.     std::cout << std::endl;
  17.  
  18.     std::cout << "Row amount of matrix 2 is equal column amount for matrix 1: " << col_matr_1;
  19.     int row_matr_2 = col_matr_1;
  20.     std::cout << std::endl << std::endl;
  21.  
  22.     std::cout << "Enter column amount of matrix 2: ";
  23.     int col_matr_2;
  24.     std::cin >> col_matr_2;
  25.     std::cout << std::endl;
  26.  
  27.     int first[row_matr_1][col_matr_1];
  28.     int second[row_matr_2][col_matr_2];
  29.     int result[row_matr_1][col_matr_2];
  30.  
  31.     std::cout << "Let's fill the matrix 1:" << std::endl;
  32.     for (int i=0; i<row_matr_1; i++)
  33.         for (int j=0; j<col_matr_1; j++)
  34.         {
  35.             std::cout << "Enter " << j << " item in " << i << " row: ";
  36.             std::cin >> first[i][j];
  37.         };
  38.  
  39.     std::cout << std::endl;
  40.  
  41.     std::cout << "Let's fill the matrix 2:" << std::endl;
  42.     for (int i=0; i<row_matr_2; i++)
  43.         for (int j=0; j<col_matr_2; j++)
  44.         {
  45.             std::cout << "Enter " << j << " item in " << i << " row: ";
  46.             std::cin >> second[i][j];
  47.         };
  48.  
  49.     std::cout << std::endl;
  50.  
  51.     for (int i=0; i<row_matr_1; i++)
  52.         for (int j=0; j<col_matr_2; j++){
  53.             result[i][j] = 0;
  54.             for (int k=0; k<col_matr_1; k++)
  55.                 result[i][j] += first[i][k]*second[k][j];
  56.         };
  57.  
  58.     std::cout << "The multiple result is:" << std::endl;
  59.  
  60.     for (int i=0; i<col_matr_1; i++){
  61.         for (int j=0; j<col_matr_2; j++)
  62.             std::cout << result[i][j] << " ";
  63.         std::cout << std::endl;
  64.     };
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement