Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. double **a, **b, **c;
  4. int row1, row2, col1, col2;
  5.  
  6. void multiply() {
  7. // Умножение матриц
  8. c = new double*[row1];
  9. for (int i = 0; i < row1; i++)
  10. {
  11. c[i] = new double[col2];
  12. for (int j = 0; j < col2; j++)
  13. {
  14. c[i][j] = 0;
  15. for (int k = 0; k < col1; k++)
  16. c[i][j] += a[i][k] * b[k][j];
  17. }
  18. }
  19. }
  20.  
  21. int matrixWork(void(*f)()) { // параметр - указатель на функцию
  22. cout « "Введите количество строк первой матрицы: ";
  23. cin » row1;
  24. cout « "Введите количество столбцов первой матрицы: ";
  25. cin » col1;
  26. cout « "Введите количество строк второй матрицы: ";
  27. cin » row2;
  28. cout « "Введите количество столбцов второй матрицы: ";
  29. cin » col2;
  30. if (col1 != row2)
  31. {
  32. cout « "Умножение невозможно!";
  33. cin.get(); cin.get();
  34. return 0;
  35. }
  36. // Ввод элементов первой матрицы
  37. a = new double*[row1];
  38. cout « "Введите элементы первой матрицы" « endl;
  39. for (int i = 0; i < row1; i++)
  40. {
  41. a[i] = new double[col1];
  42. for (int j = 0; j < col1; j++)
  43. {
  44. cout « "a[" « i « "][" « j « "]= ";
  45. cin » a[i][j];
  46. }
  47. }
  48. // Вывод элементов первой матрицы
  49. for (int i = 0; i < row1; i++)
  50. {
  51. for (int j = 0; j < col1; j++)
  52. cout « a[i][j] « " ";
  53. cout « endl;
  54. }
  55. // Ввод элементов второй матрицы
  56. b = new double*[row2];
  57. cout « "Введите элементы второй матрицы" « endl;
  58. for (int i = 0; i < row2; i++)
  59. {
  60. b[i] = new double[col2];
  61. for (int j = 0; j < col2; j++)
  62. {
  63. cout « "b[" « i « "][" « j « "]= ";
  64. cin » b[i][j];
  65. }
  66. }
  67. // Вывод элементов второй матрицы
  68. for (int i = 0; i < row2; i++)
  69. {
  70. for (int j = 0; j < col2; j++)
  71. {
  72. cout « b[i][j] « " ";
  73. }
  74. cout « endl;
  75. }
  76.  
  77. f(); // вызов функции, указанной в параметре
  78.  
  79. // Вывод матрицы произведения
  80. cout « "Матрица произведения" « endl;
  81. for (int i = 0; i < row1; i++)
  82. {
  83. for (int j = 0; j < col2; j++)
  84. cout « c[i][j] « " ";
  85. cout « endl;
  86. }
  87. return 1;
  88. }
  89.  
  90. int main()
  91. {
  92. void(*x)();
  93. x = multiply; // создание указателя на функцию
  94.  
  95. system("chcp 1251");
  96. system("cls");
  97. matrixWork(x); // передача указателя как входного значения
  98. cin.get(); cin.get();
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement