Mean and Standard Deviation-从文件中读取数据计算其平均数和标准差
2014-09-02 20:36
411 查看
Meanand Standard Deviation-从文件中读取数据计算其平均数和标准差
//Meanand Standard Deviation-从文件中读取数据计算其平均数和标准差 #include<iostream> #include<fstream> #include<cstdlib> #include<cmath> int main() { usingnamespace std; ifstream fin; ofstream fout; double aver,tem,count =0,sum = 0; double stadev; fin.open("numbers.dat"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } //fin>>tem; //sum = tem; while(fin>>tem) { count++; sum += tem; } cout<<"sum ="<<sum<<endl; cout<<"count = "<<count<<endl; aver = sum/count; fin.close(); fin.open("numbers.dat"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } sum =0; fin>>tem; while(!fin.eof()) { sum += (tem - aver)*(tem - aver); fin>>tem; } stadev = sqrt(sum / count); cout<<"The average is "<<aver<<endl; cout<<"The stadev is "<<stadev<<endl; fin.close(); return 0; }文件1:
1 2 3 4 5 6结果1:
sum=21 count= 6 Theaverage is 3.5 The stadev is1.36931文件2:
8 8 8 8 8 8结果2:
sum=48 count= 6 Theaverage is 8 The stadev is 0文件3:
11.2 1.3 2.4 8.5 7.6 3.7 4.8 6.9 5.0 9.1结果3:
sum=60.5 count= 10 Theaverage is 6.05 The stadev is 2.9837
相关文章推荐
- average and standard deviation-平均数和标准差
- C语言之文件操作07——读取文件数据并计算均值方差标准差
- JAVA编程中中如何读取TXT文件数据并计算
- 读取文件中的数据,并且计算其平均值和标准偏差
- Java源码——读取顺序存取文件中的数据(read text file and display each record)
- C语言之文件操作07——读取文件数据并计算均值方差标准差
- 从一个磁盘文件中读取数据计算平均值,最后把计算结果放到另一个磁盘文件中(fscanf(),fprintf()函数应用)
- java 数据与文本文件存储读取,文件大小计算。
- Mathematics | Mean, Variance and Standard Deviation
- Mean, Median, Mode, Range, and Standard Deviation
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 于Excel文件上传读取数据的问题
- ADO 读取Excel文件数据, 丢失数据或数据错误问题。
- ASP.NET中上传并读取Excel文件数据
- ASP.NET中上传并读取Excel文件数据
- Outlook 2007: Accounts and data files 帐户和数据文件 - RSS Feeds
- ASP.NET中上传并读取Excel文件数据
- .net(c#)读取*.dbf文件数据的类库
- ASP.NET中上传并读取Excel文件数据
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中