【发布时间】:2021-07-07 13:25:11
【问题描述】:
我有编码来查找提供的文件的标准偏差。我的讲师建议在函数中进行。这是我尝试过的编码。但是在主函数中调用函数有错误。它说 std 不能隐蔽浮动。我应该怎么做才能找到文件中的标准差?
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
float calculateSD(float namefile);
int main()
{
string input;
ifstream namefile("payment.txt");
if (!namefile)
return (cout << " ERROR : cannot open file.\n"), 1;
while (getline(namefile, input))
cout << input << endl;
namefile.clear();
namefile.seekg(0);
int a {}, b {}, c {};
nameFile >> a >> b >> c ;
calculateSD(nameFile >> a >> b >> c);
}
float calculateSD(float nameFile)
{
float sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
float mean1, mean2, mean3;
float standardDeviation1 = 0.0, standardDeviation2 = 0.0, standardDeviation3 = 0.0;
for (int a {}, b {}, c {}; namefile >> a >> b >> c; )
{
sum1 += a;
sum2 += b;
sum3 += c;
}
mean1 = sum1/10;
mean2 = sum2/10;
mean3 = sum3/10;
for (int a {}, b {}, c {}; nameFile >> a >> b >> c ; )
{
standardDeviation1 += pow(a - mean1, 2);
standardDeviation2 += pow(b - mean2, 2);
standardDeviation3 += pow(c - mean3, 2);
}
cout << "Standard Deviation 1 = " << sqrt(standardDeviation1 / 10) << endl;
cout << "Standard Deviation 2 = " << sqrt(standardDeviation2 / 10)<< endl;
cout << "Standard Deviation 3 = " << sqrt(standardDeviation3 / 10)<< endl;
return 0;
}
如果你们能帮助我,真的很感激
【问题讨论】:
-
请不要解释错误,但在问题中包含编译器错误消息
-
float nameFile这看起来很奇怪。 -
float calculateSD(std::iostream&);?