我的数学出了什么问题
What is wrong with my math?
好的,所以我的老师让我们制作一个使用一组数字的程序,并找到它的标准偏差。我的程序找到了平均值。然而,我的数学有问题。它出了什么问题。它给了我59的平均值和8.4的偏差。平均值是正确的,但偏差应为96.4。我的数学出了什么问题。
编辑:我的程序现在可以工作了
P。S.我已将以下代码更改为当前版本的代码。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Used To Round The Decimal Points
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(1);
//Declaring
double Numbers[] = {65, 49, 74, 59, 48}; //Work On Making This A User Input----------Deivation = 96.4
double Mean = 0, Items = 0, Sum = 0, Deviation = 0;
int Counter;
//Finds The Mean Of The Set Of Numbers
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Sum += Numbers[Counter]; //Adds All Numbers In Array Together
}
Items = sizeof(Numbers) / sizeof(double); //Gets The Number Of Items In The Array
Mean = Sum / Items; //Finds The Mean
}
//Finds The Standard Deviation
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Deviation += pow((Numbers[Counter] - Mean), 2) / Items; //Does Math Things...
}
Deviation = sqrt(Deviation);
cout << "Deviation = " << Deviation << endl; //Print Out The Standard Deviation
system("pause");
return 0;
}
[…]然而,偏差应为96.4
方差应该是96.4。它是根据平均值的平方差的平均值计算的,所以你根本不需要平方根:
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Variance += pow((Numbers[Counter] - Mean), 2) / Items;
}
Deviation = sqrt(Variance);
取方差的平方根得到9.81835。
从循环中提取sqrt,并在求和后应用它。
偏差的数学表达式存在错误,该表达式应为集合的方差平方根:
方差=总和(pow(集[i]-平均值,2))/n
偏差=sqrt(方差)
顺便说一句,我认为这里的9.82比96.4 更正确
正如人们指出的那样,错误在于无法直接添加标准偏差。计算标准偏差的更好方法是取(元素从其平均值中减去的)均方根。
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 格式说明符C++有什么问题
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 从 argv[1] 转换为字符 * 字符串后有什么问题?
- 我的堆栈和库存清单程序的结构有什么问题?
- 此工厂功能有什么问题?
- 以下 C++ 代码有什么问题?
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- reinterpret_cast,只读访问,简单的可复制类型,会出什么问题?
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了