interquartile range

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; vector<double> append(vector<double> a, vector<double> b) { for (int i = 0; i < b.size(); i++) { a.push_back(b[i]); } return a; } 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; } vector<double> quartiles(vector<double> mainarr) { sort(mainarr.begin(), mainarr.end()); int N = mainarr.size(); 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; } vector<double> retvect(3); retvect[0] = q1; retvect[1] = q2; retvect[2] = q3; return retvect; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int N; cin >> N; vector<double> nums(N); vector<double> freqs(N); vector<double> fullarr; for (int i = 0; i < nums.size(); i++) { cin >> nums[i]; } for (int i = 0; i < nums.size(); i++) { cin >> freqs[i]; vector<double> temp((int)freqs[i],nums[i]); fullarr = append(fullarr, temp); } vector<double> qs(quartiles(fullarr)); cout << fixed << setprecision(1) << qs[2] - qs[0] << 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.