输入n个数,输出其总和,平均值,方差,输入的数以!结束

输入n个数,输出其总和,平均值,方差,输入的数以!结束


#include <iostream>
#include<math.h>
#include <memory>
#include <iomanip>
using namespace std;
double sum,avg,fc,bzc,rmax,rmin;
void deviation(double f[], int size)
{
int i;
double s=0;
rmax=rmin=f[0];
for(i=0;i<size;i++)
{
sum+=f[i];
if(f[i]>rmax)rmax=f[i];
if(f[i]<rmin)rmin=f[i];
}

avg = sum/size;//平均值

for(i=0;i<size;i++)
{
s+=pow(f[i]-avg,2);// 偏离平均数的距离和
}

fc=s/size;//方差
bzc=sqrt(fc); //标准差
}

int main()
{
int i=0;
bool isZero = false;
double f[100],tmp;
while(!isZero)
{
cout<<"请输入数字 ==> ";
cin>>tmp;

if(tmp)f[i++]=tmp;
else isZero = true;
}
cout<<endl;

double* res = new double[i];
for(int j=0;j<i;j++)
{
res[j]=f[j];
}

deviation(res,i);

delete res;

cout<<"输入的数字个数:"<<i<<endl;

cout<<fixed<<setprecision(5)<<right<<endl;

cout<<"总和: "<<sum<<endl;
cout<<"平均值: "<<avg<<endl;
cout<<"方差: "<<fc<<endl;
cout<<"标准差: "<<bzc<<endl;

cout<<fixed<<endl;
cout<<"最大值: "<<rmax<<endl;
cout<<"最小值: "<<rmin<<endl;
system("pause");

return 0;
}

测试:
请输入数字 ==> 100
请输入数字 ==> 50.64
请输入数字 ==> 300
请输入数字 ==> 210
请输入数字 ==> 45
请输入数字 ==> 120
请输入数字 ==> 20.86
请输入数字 ==> 0

输入的数字个数:7

总和: 846.50000
平均值: 120.92857
方差: 8736.93050
标准差: 93.47155

最大值: 300.00000
最小值: 20.86000
请按任意键继续. . .