fiveriverflow

Array Sort + Median

Dec 15th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     int a[] = {3, 4, 5, 1, 6};
  10.     int size = sizeof(a)/sizeof(int);
  11.     std::sort(&a[0], &a[size]);
  12.     double median = size % 2 ? a[size / 2] : (a[size / 2 - 1] + a[size / 2]) / 2;
  13.  
  14.     cout << median << " ";
  15.  
  16.     return 0;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment