Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- using namespace std;
- pair<bool, double> CalcMedian(vector<double> samples) {
- if (samples.empty()) {
- return { false, 0 };
- }
- sort(samples.begin(), samples.end());
- auto index = samples.size() / 2;
- if (samples.size() % 2 == 0) {
- return { true, (samples[index] + samples[index - 1])/2};
- }
- else {
- return { true, samples[index] };
- }
- }
- int main() {
- int size;
- cin >> size;
- vector<double> samples;
- for (int i = 0; i < size; ++i) {
- double sample;
- cin >> sample;
- samples.push_back(sample);
- }
- pair<bool, double> result = CalcMedian(samples);
- if (result.first) {
- cout << result.second << endl;
- }
- else {
- cout << "Empty vector"s << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement