我为家庭作业制作的程序存在严重的输出问题。下面是我用来计算用户给出的一组数字的平均值和标准差的代码,直到达到标志值 -1。我不允许使用数组。我应该使用我的教授给出的公式(用于标准开发部分),即 ((sum (xi)^n) - ((sum xi) ^2/ n) /(n-1)) xi is x sub i,即每个输入。我在我的 macbook pro 上的 win7 x64 vm 上使用 Visual Studio 2010(怀疑这有什么关系,但以防万一)。
下面是代码(后跟错误的屏幕截图)
#include<iostream>
#include <cmath>
using namespace std;
int i;
int sum_unkown_vars();
double std_dev(int sum, int n);
/*double std_dev(int sum, int n)
{
double dev;
dev = sqrt(((pow(sum, 2.0)-(pow(sum, 2.0)/n)))/(n-1));
return dev;
}*/
int sum_unkown_vars()
{
i = 0;
int n;
int sum1 = 0;
int sum_sqd = 0;
double sdev;
cout<<"This part will sum variables given by user until flag value of -1"<<endl;
cout<<"\nNext Variable Please: ";
cin >> n;
while(n != -1)
{
sum_sqd = sum_sqd + n*n;
sum1 = sum1 + n;
i++;
cout<<"\nNext Variable Please: ";
cin >> n;
}
cout <<"\nNumber of variables is "<< i <<endl
<<"Sum of variables is "<< sum1 <<endl;
sdev = sqrt(((sum_sqd*1.0)-(1.0*pow(sum1, 2.0)/n))/(n*1.0-1.0));
cout <<"\nStandard Deviation is "<< sdev << endl;
return sum1;
}
int main()
{
int sum = 0;
int j;
double avg;
double std_dev1;
cout<<"This program will take integers given by the user,"
<<"\nsum them, then find average and standard deviation\n\n";
sum = sum_unkown_vars();
//cout <<"\nPlease enter number of integers previously given: ";
//cin >> j;
avg = sum / (i*1.0);
cout <<"\nAverage is: "<<avg<<endl;
//std_dev1 = std_dev(sum, i);
//cout <<"Standard Deviation is : "<< std_dev1 <<endl;
system("pause");
return 0;
}
提前感谢您收到的任何帮助