输入n个数,输出其总和,平均值,方差,输入的数以!结束
2条回答 默认 最新
- 神仙别闹 2021-12-27 15:07关注
#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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 目详情-五一模拟赛详情页
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line