diff --git a/cpp/mean_median_mode.cpp b/cpp/mean_median_mode.cpp new file mode 100644 index 0000000..567a49d --- /dev/null +++ b/cpp/mean_median_mode.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + + vector a(n); + + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + + sort(begin(a), end(a)); + + double sum = accumulate(begin(a), end(a), 0); + double average = sum / n; + + double median = a[n / 2]; + + if (! (n & 1)) { + median += a[n / 2 - 1]; + median /= 2; + } + + map f; + + for (int e : a) { + f[e]++; + } + + int mode = 0; + + for (auto e : f) { + if (e.second > f[mode]) { + mode = e.first; + } + } + + cout << setprecision(1) << fixed << average << endl; + cout << setprecision(1) << fixed << median << endl; + cout << mode << endl; + + return 0; +}