Advertisement
Temabowl

Test1

Sep 16th, 2021
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #include <iostream>
  2. #define _USE_MATH_DEFINES
  3. #include <math.h>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7.  
  8. int Sorting()
  9. {
  10. int* arr;
  11. int size;
  12. cout << "n = ";
  13. cin >> size;
  14. arr = new int[size];
  15.  
  16. for (int i = 0; i < size; i++) {
  17. arr[i] = rand() % 100 + 1;
  18. }
  19.  
  20. cout << "no sort:" << endl;
  21. for (int i = 0; i < size; i++) {
  22. cout << arr[i] << " ";
  23. }
  24.  
  25.  
  26. int temp;
  27. for (int i = 0; i < size - 1; i++) {
  28. for (int j = 0; j < size - i - 1; j++) {
  29. if (arr[j] > arr[j + 1]) {
  30. temp = arr[j];
  31. arr[j] = arr[j + 1];
  32. arr[j + 1] = temp;
  33. }
  34. }
  35. }
  36. cout << "\nsort: " << endl;
  37. for (int i = 0; i < size; i++) {
  38. cout << arr[i] << " ";
  39. }
  40. cout << endl;
  41.  
  42. return 0;
  43. }
  44.  
  45.  
  46. int Matrix_multiplication()
  47. {
  48. int row1, row2, col1, col2;
  49. double** a, **b, **c;
  50.  
  51. cout << "row1: ";
  52. cin >> row1;
  53. cout << "col1: ";
  54. cin >> col1;
  55. cout << "row2: ";
  56. cin >> row2;
  57. cout << "col2: ";
  58. cin >> col2;
  59. if (col1 != row2)
  60. {
  61. cout << "multiplication is not possible!";
  62. cin.get(); cin.get();
  63. return 0;
  64. }
  65. // эл. первой матрицы. ввод
  66. a = new double* [row1];
  67. cout << "elements 1" << endl;
  68. for (int i = 0; i < row1; i++)
  69. {
  70. a[i] = new double[col1];
  71. for (int j = 0; j < col1; j++)
  72. {
  73. cout << "a[" << i << "][" << j << "]= ";
  74. cin >> a[i][j];
  75. }
  76. }
  77. // эл. первой матрицы. вывод
  78. for (int i = 0; i < row1; i++)
  79. {
  80. for (int j = 0; j < col1; j++)
  81. cout << a[i][j] << " ";
  82. cout << endl;
  83. }
  84. // эл. второй матрицы. ввод
  85. b = new double* [row2];
  86. cout << "elements 2" << endl;
  87. for (int i = 0; i < row2; i++)
  88. {
  89. b[i] = new double[col2];
  90. for (int j = 0; j < col2; j++)
  91. {
  92. cout << "b[" << i << "][" << j << "]= ";
  93. cin >> b[i][j];
  94. }
  95. }
  96. // эл. второй матрицы. вывод
  97. for (int i = 0; i < row2; i++)
  98. {
  99. for (int j = 0; j < col2; j++)
  100. {
  101. cout << b[i][j] << " ";
  102. }
  103. cout << endl;
  104. }
  105. // Умножение
  106. c = new double* [row1];
  107. for (int i = 0; i < row1; i++)
  108. {
  109. c[i] = new double[col2];
  110. for (int j = 0; j < col2; j++)
  111. {
  112. c[i][j] = 0;
  113. for (int k = 0; k < col1; k++)
  114. c[i][j] += a[i][k] * b[k][j];
  115. }
  116. }
  117. // Вывод матрицы произведения
  118. cout << "result" << endl;
  119. for (int i = 0; i < row1; i++)
  120. {
  121. for (int j = 0; j < col2; j++)
  122. cout << c[i][j] << " ";
  123. cout << endl;
  124. }
  125. cin.get(); cin.get();
  126. return 0;
  127.  
  128.  
  129. }
  130. int Solve_integral1()
  131. {
  132. double a = 0;
  133. double b = M_PI/2;
  134. int n = 2;
  135.  
  136. double h = (b - a) / n;
  137. double c = a + h;
  138. double result = 0;
  139.  
  140.  
  141. result += (pow(cos(3 * a), 3) * sin(6 * a));
  142. for (int i = 0; i < n; i++)
  143. {
  144. result += h*(pow(cos(3 * c), 3) * sin(6 * c));
  145. c += h;
  146. }
  147. cout << "метод прямоугольников:" << endl;
  148. cout << result << endl;
  149.  
  150. c = a + h;
  151. result = ((pow(cos(3 * a), 3) * sin(6 * a))+
  152. (pow(cos(3 * b), 3) * sin(6 * b)))/2;
  153. for (int i = 0; i < n; i++)
  154. {
  155. result += (pow(cos(3 * c), 3) * sin(6 * c));
  156. c += h;
  157. }
  158. result *= h;
  159. cout << "метод трапеций:" << endl;
  160. cout << result << endl;
  161.  
  162. return 0;
  163. }
  164.  
  165.  
  166. int main()
  167. {
  168.  
  169. setlocale(LC_ALL, "Russian");
  170.  
  171. cout << "Что будем делать?\n1. Умножение матриц\n2. Умножение вектора на матрицу \n3. Сортировка \n4. Интегралы" << endl;
  172. int choise = 0;
  173. cin >> choise;
  174. if (choise == 1 || choise == 2)
  175. {
  176. Matrix_multiplication();
  177. }
  178. if (choise == 3)
  179. {
  180. Sorting();
  181. }
  182. if (choise == 4)
  183. {
  184. Solve_integral1();
  185. }
  186.  
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement