Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <omp.h> // OpenMP
- using namespace std;
- int Sorting()
- {
- int size;
- size = 100;
- vector <int> arr, arr2;
- int buffer;
- // заполнение массива arr
- for (int i = 0; i < size; i++) {
- arr.push_back(buffer);
- arr[i] = rand() % 100 + 1;
- }
- // заполнение массива arr2
- for (int i = 0; i < size; i++) {
- arr2.push_back(buffer);
- arr2[i] = arr[i];
- }
- // таймер
- std::clock_t start;
- double duration;
- start = std::clock();
- //сортировка пузырьком
- int temp;
- for (int i = 0; i < size - 1; i++) {
- for (int j = 0; j < size - i - 1; j++) {
- if (arr[j] > arr[j + 1]) {
- temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- duration = (std::clock() - start); // в тиках
- std::cout << "timer: " << duration << " tik\n";
- // таймер
- start = std::clock();
- //Встроенная сортировка
- std::sort(arr2.begin(), arr2.end());
- duration = (std::clock() - start); // в тиках
- std::cout << "timer: " << duration << " tik\n";
- return 0;
- }
- int Matrix_multiplication(int sw)
- {
- int row1, row2, col1, col2;
- double** a, ** b, ** c;
- if (sw == 1) // Умножение матриц
- {
- row1 = 100;
- row2 = 100;
- col1 = 100;
- col2 = 100;
- }
- if (sw == 2) // Умножение матрицы на вектор
- {
- row1 = 1;
- col1 = rand() % 100 + 1;
- row2 = col1;
- col2 = rand() % 100 + 1;
- }
- std::clock_t start;
- double duration;
- start = std::clock();
- // эл. первой матрицы. ввод
- a = new double* [row1];
- for (int i = 0; i < row1; i++)
- {
- a[i] = new double[col1];
- for (int j = 0; j < col1; j++)
- {
- a[i][j] = rand() % 100 + 1;
- }
- }
- // эл. второй матрицы. ввод
- b = new double* [row2];
- for (int i = 0; i < row2; i++)
- {
- b[i] = new double[col2];
- for (int j = 0; j < col2; j++)
- {
- b[i][j] = rand() % 100 + 1;
- }
- }
- // Умножение
- c = new double* [row1];
- for (int i = 0; i < row1; i++)
- {
- c[i] = new double[col2];
- for (int j = 0; j < col2; j++)
- {
- c[i][j] = 0;
- for (int k = 0; k < col1; k++)
- c[i][j] += a[i][k] * b[k][j];
- }
- }
- duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
- std::cout << "timer: " << duration << " sec\n";
- return 0;
- }
- int Solve_integral1()
- {
- double a = 0;
- double b = M_PI / 2;
- int n = 100; // количество точек
- double result = 0;
- cout << setprecision(20);
- std::clock_t start;
- double duration;
- start = std::clock();
- double h = (b - a) / n;
- double c = a + h;
- result += (pow(cos(3 * a), 3) * sin(6 * a));
- for (int i = 0; i < n; i++)
- {
- result += h * (pow(cos(3 * c), 3) * sin(6 * c));
- c += h;
- }
- duration = (std::clock() - start); // в тиках
- std::cout << "timer: " << duration << " tik\n";
- cout << "метод прямоугольников:" << endl;
- cout << result << endl;
- start = std::clock();
- c = a + h;
- result = ((pow(cos(3 * a), 3) * sin(6 * a)) +
- (pow(cos(3 * b), 3) * sin(6 * b))) / 2;
- for (int i = 0; i < n; i++)
- {
- result += (pow(cos(3 * c), 3) * sin(6 * c));
- c += h;
- }
- result *= h;
- duration = (std::clock() - start); // в тиках
- std::cout << "timer: " << duration << " tik\n";
- cout << "метод трапеций:" << endl;
- cout << result << endl;
- cout << setprecision(5);
- return 0;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- cout << "Что будем делать?\n1. Умножение матриц\n2. Умножение вектора на матрицу \n3. Сортировка \n4. Интегралы" << endl;
- int choise = 0;
- cin >> choise;
- if (choise == 1 || choise == 2)
- {
- Matrix_multiplication(choise);
- }
- if (choise == 3)
- {
- Sorting();
- }
- if (choise == 4)
- {
- Solve_integral1();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement