1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int iArray[] = {2,10,5,1,55,7,48,103,22,6,3,99,45,99}; // LoL, as random as I can think of em.
vector<int> mArray(iArray,iArray+14); // Remember to set this number to match the number of items in your int array
vector<int>::iterator it;
sort(mArray.begin(),mArray.end());
// Find the median.
float median,middle,middleh;
float middlel;
vector<int>::iterator z;
switch(mArray.size()%2) {
case(0): // even
z = mArray.begin();
middlel = mArray.size()/2;
z += middlel;
middleh = (*z+*(--z))/2;
cout << "Median is: " << middleh << endl;
break;
case(1): // odd
z = mArray.begin();
middle = mArray.size()/2;
cout << "Median is : " << *(z+=middle) << endl;
break;
}
// display the sorted array.
for(it=mArray.begin();it!=mArray.end();it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
|