Advertisement
MeehoweCK

Untitled

Aug 19th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. double f(vector<double> t)
  9. {
  10.     // skopiowanie tablicy
  11.     vector<double> temp = t;
  12.  
  13.     // sortowanie bąbelkowe:
  14.     for(unsigned i = 0; i < temp.size() - 1; ++i)
  15.     {
  16.         for(unsigned j = 0; j < temp.size() - 1 - i; ++j)
  17.             if(temp[j] > temp[j + 1])
  18.                 swap(temp[j], temp[j + 1]);
  19.     }
  20.  
  21.     if(t.size() % 2 == 1)
  22.         return temp[(temp.size() - 1) / 2];
  23.     unsigned i = temp.size() / 2;
  24.     return (temp[i] + temp[i - 1]) / 2;
  25. }
  26.  
  27. int main()
  28. {
  29.     srand(static_cast<unsigned>(time(nullptr)));
  30.  
  31.     vector<double> wektor;
  32.     wektor.resize(10);
  33.  
  34.     for(unsigned i = 0; i < 10; ++i)
  35.     {
  36.         wektor[i] = 100.0 * rand() / RAND_MAX;
  37.         cout << wektor[i] << '\t';
  38.     }
  39.     cout << endl << f(wektor);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement