【发布时间】:2015-05-13 02:05:02
【问题描述】:
这是我第一次尝试编程。我希望能够输入可变数量的数据并输出平均值和标准偏差。我希望它尽可能简单,最好使用当前程序中已经存在的命令。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float num[46];
float amount_num;
cout<< "How many numbers do you want? (max 45)\n";
cin>> amount_num;
for (int counter =0; counter < amount_num; counter++)
{
cout<< "Enter value "<< counter<< ":"<< endl;
cin>> num[counter];
}
int total;
int average;
int latot;
int fun;
int n;
int taco;
int sd;
int bell;
for(int t= 0; t< amount_num; t++)
{
total = total + num[t];
}
average= total/amount_num;
for(int t= 0; t< amount_num; t++)
{
latot = num[t] - average;
bell = pow (latot,2);
fun = fun + bell;
}
n=amount_num-1;
taco=fun/n;
sd=sqrt(taco);
cout<< "Average: "<< average<< endl;
cout<< "SD: "<< sd<< endl;
}
【问题讨论】:
-
这次尝试有什么问题?你有错误吗?意外的结果/行为?回复:代码,我不喜欢空格(我在运算符的两边各占一个空格,例如
n = amount - 1;),命名(latot和taco?!),以及分配一个大,不必要的数组。不过,好的开始;欢迎来到 C++! :) -
看起来您正在计算 sample 标准差。如果你没有得到你期望的结果,那么也许你想计算 population 标准差?
标签: c++ statistics standard-deviation