,所以我已经张贴了几次,以前我的问题是相当含糊计算标准差和变异在C++
我开始C++本周一直在做一个小项目
所以我想到calc下标准差&方差
我的代码加载100个整数的文件,并把它们放到一个数组,统计他们,Calcs(计算)的意思,总之,VAR和SD
但我在与方差有点麻烦
我不断收到一个巨大的数字 - 我有一种感觉,它与它的计算
我均值和金额做都行
任何帮助或建议吗?
NB:
干杯,
杰克
using namespace std;
int main()
{
int n = 0;
int Array[100];
float mean;
float var;
float sd;
string line;
float numPoints;
ifstream myfile(“numbers.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile, line);
stringstream convert(line);
if (!(convert >> Array[n]))
{
Array[n] = 0;
}
cout << Array[n] << endl;
n++;
}
myfile.close();
numPoints = n;
}
else cout<< "Error loading file" <<endl;
int sum = accumulate(begin(Array), end(Array), 0, plus<int>());
cout << "The sum of all integers: " << sum << endl;
mean = sum/numPoints;
cout << "The mean of all integers: " << mean <<endl;
var = ((Array[n] - mean) * (Array[n] - mean))/numPoints;
sd = sqrt(var);
cout << "The standard deviation is: " << sd <<endl;
return 0;
}
在'(数组[n] - 平均值)'是不'N'比元件的数量多一个已阅读?此外,['while(!myfile.eof())']几乎总是错的](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) –
你应该使用double而不是float – FredK
''''应该是''' –