first second and third quartile

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; double median(vector<double> a) { sort(a.begin(), a.end()); int sz = a.size(); if (sz % 2) { return a[(sz + 1) / 2 - 1]; } else { return (a[sz / 2 - 1] + a[sz / 2]) / 2; } } vector<double> removeatpos(vector<double> a, int pos) { vector<double> retvect; for (int i = 0; i < a.size(); i++) { if (i!=pos) { retvect.push_back(a[i]); } } return retvect; } int main() { int N; cin >> N; vector<double> mainarr(N); for (int i = 0; i < mainarr.size(); i++) { cin >> mainarr[i]; } sort(mainarr.begin(), mainarr.end()); double q1, q2, q3; if (N % 2) { q2 = median(mainarr); vector<double> temparr(removeatpos(mainarr, (N + 1) / 2 - 1)); vector<double> fh(temparr.begin(), temparr.begin() + temparr.size() / 2); vector<double> sh(temparr.begin() + temparr.size() / 2, temparr.end()); q1 = median(fh); q3 = median(sh); cout << q1 << endl << q2 << endl << q3 << endl; } else { q2 = median(mainarr); vector<double> fh(mainarr.begin(), mainarr.begin() + N / 2); vector<double> sh(mainarr.begin() + N / 2, mainarr.end()); q1 = median(fh); q3 = median(sh); cout << q1 << endl << q2 << endl << q3 << endl; } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.