Statistical measures
Write a program to compute the most common statistical measures (minimum, maximum and average) of several integer numbers.
These numbers are known increasingly. At every moment, it is also possible to delete the smallest element.
Input: A sequence of (possibly, many) numbers, together with deleting instructions, as can be seen in the sample input. The numbers may be repeated. Deleting means removing the smallest element (one instance of it, if it is repeated). If there are no elements, deleting does nothing.
Output: After every instruction of the input, print the minimum, maximum and average of the current elements with four digits after the decimal point, or tell that there are no elements. Follow the format of the sample output.
Try to use a
priority_queue
with first element the smallest among the ones seen so far.To output a double x with 4 digits of precision you can use
#include<iomanip>
andcout << fixed << setprecision(4) <<
the double you want to output.#include<limits>
allows you to access e.g. the smallestint
withnumeric_limits<int>::lowest()
.
//STL - Satistical measures
#include<iostream>
#include<queue>
#include<limits>
#include<iomanip>
using namespace std;
const auto MININFTY = numeric_limits<int>::lowest();
int main(){
;
string op<int,vector<int>,greater<int>> q;
priority_queueint sum = 0;
int max = MININFTY;
while (cin >> op){
if (op == "delete"){
if (not q.empty()){
-=q.top();
sum .pop();
qif (q.empty()) max = MININFTY;
}
}
else{// case of "number"
int value;
>> value;
cin .push(value);
q+=value;
sum if (value > max) max = value;
}
int sz = q.size();
if (sz == 0) cout << "no elements" << endl;
else{
double avg = double(sum) / sz;
<< "minimum: " << q.top();
cout << ", maximum: " << max;
cout << ", average: " << fixed << setprecision(4) << avg << endl;
cout }
}
}