Vla_DOS

Untitled

Jun 18th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <chrono>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. void bubbleSort(int* arr, int n)
  13. {
  14.     int i, j;
  15.     for (i = 0; i < n - 1; i++)
  16.         for (j = 0; j < n - i - 1; j++)
  17.             if (arr[j] > arr[j + 1])
  18.                 swap(arr[j], arr[j + 1]);
  19. }
  20.  
  21. int main() {
  22.     setlocale(LC_CTYPE, "");
  23.     int num;
  24.     cout << "Кiлькiсть елементiв: ";
  25.     cin >> num;
  26.     int* mass = new int[num];
  27.     for (int i = 0; i < num; i++) {
  28.         mass[i] = rand() % 27 - 10;
  29.         cout << mass[i] << "\t";
  30.     }
  31.  
  32.     cout << "\nВiдсортований масив:" << endl;
  33.     bubbleSort(mass, num);
  34.  
  35.     for (int i = 0; i < num; i++) {
  36.         cout << mass[i] << "\t";
  37.     }
  38.     cout << "\n\nСума першого та останього елементів масиву: " << mass[0] + mass[num - 1];
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment