读取文件中的数据,并且计算其平均值和标准偏差

#include #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { double data; vector Vdata; // file contains an undermined number of integer values ifstream fin; // declare stream variable name fin.open(argv[1]/*"D://test//1.txt/0"*/,ios::in); // open file assert (!fin.fail()); fin >> data; // get first number from the file (priming the input statement) // You must attempt to read info prior to an eof( ) test. while (!fin.eof( )) //if not at end of file, continue reading numbers { Vdata.push_back(data); cout<> data; //get next number from file } fin.close( ); //close file double sum=0; int count=0; for (vector::iterator itr=Vdata.begin();itr!=Vdata.end();++itr) { ++count; sum+=*itr; } double avg=sum/count; //cout<::iterator itr=Vdata.begin();itr!=Vdata.end();++itr) { stdDev2+=pow((*itr-avg),2); } stdDev=sqrt(stdDev2/(count-1)); cout<<"Average="<

你可能感兴趣的:(读取文件中的数据,并且计算其平均值和标准偏差)