I am honestly too lazy to input hundreds of values into the calculator and calculate every their average & uncertainty, so here’s me automating this process.
实在是懒得去把数值一个一个输进计算器里然后计算平均值和偏差值了……所以写了一小段代码来自动化这个过程。
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(){
string val;
vector<double> valVctr;
cin.ignore(1);
do{
getline(cin, val);
if(val.size() == 0){
break;
}
valVctr.push_back(atof(val.c_str()));
}while(true);
cout << "A total of " << valVctr.size() << " values have been entered." << endl;
double sum = 0;
for(unsigned i = 0; i < valVctr.size(); i++){
sum += valVctr.at(i);
}
double average = sum/valVctr.size();
cout << "The average value is: " << average << endl;
sum = 0;
for(unsigned i = 0; i < valVctr.size(); i++){
sum += pow(average-valVctr.at(i), 2);
}
cout << "The uncertainty is: \u00B1" << sqrt(sum/(valVctr.size()-1)) << endl;
}
oh yes i love doing homework by not doing homework
酷,我目前也在大学里学c++