Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <ctime>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. setlocale(LC_ALL, "rus");
  10.  
  11. srand(time(0));
  12. const int nrows = 3, ncols = 3;
  13. int A[nrows][ncols], B[nrows][ncols], C[nrows][ncols];
  14. cout << "Матрицы A и B: \n";
  15. for (int i = 0; i < nrows; i++) { // вывод и заполнение матриц А и В
  16. for (int j = 0; j < ncols; j++)
  17. cout << setw(5) << (A[i][j] = -9 + rand() % 19);
  18. cout << " ";
  19. for (int j = 0; j < ncols; j++)
  20. cout << setw(5) << (B[i][j] = -9 + rand() % 19);
  21. cout << endl;
  22. }
  23.  
  24. for (int i = 0; i < nrows; i++) //заполнение матрицы произведения нулями
  25. {
  26. for (int j = 0; j < ncols; j++)
  27. {
  28. C[i][j] = 0;
  29. }
  30. }
  31. for (int i = 0; i < nrows; i++) //произведение матриц А и В
  32. {
  33. for (int j = 0; j < ncols; j++)
  34. {
  35. for (int k = 0; k < nrows; k++)
  36. {
  37. C[i][j] += A[i][k] * B[k][j];
  38. }
  39.  
  40. }
  41. }
  42. cout << endl << "Произведение матриц А*В : " << endl;
  43. for (int i = 0; i < nrows; i++) //вывод матрицы произведения
  44. {
  45. for (int j = 0; j < ncols; j++)
  46. {
  47. if (j == ncols - 1) {
  48. cout << setw(5) << C[i][j] << endl;
  49.  
  50. }
  51. else
  52. cout << setw(5) << C[i][j];
  53. }
  54. }
  55. system("pause");
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement