let say
10.2
10.345
10.23
------
sum
sum/3 = ave
i want ave looks like nearest round so it has to be 10.3
and !
what if i want to fardest round so result will be 10.258
i want ave looks like nearest round so it has to be 10.3
cout<<fixed<<setprecision(1)<<ave<<endl;
what if i want to fardest round so result will be 10.258
cout<<fixed<<setprecision(3)<<ave<<endl;
the thing is you dont know which number going to be a input
it could be
10.3423
10.4435453
10.345345
No, for
10.3423
10.4435453
10.345345
the result should be 10.xxxx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
int main()
{
std::vector<double> values;
std::string temp;
size_t prec = 1000; //Change it later to constant from <limits>
//input either a filestream orstd::cin or something else
while(std::getline(/**/input/**/, temp)) {
prec = std::min(prec, temp.size() - temp.find('.') - 1);
values.push_back(std::stod(temp));
}
double avg = std::accumulate(values.begin(), values.end(), 0.0) / values.size();
std::cout << std::fixed << std::setprecision(prec) << avg;
|
//1st
12.45
12.555
^Z
12.50
//2nd
44.2345
0.123
1564.123557
^Z
536.160 |
Last edited on