Advertisement
Temabowl

test lab_2

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