菜鸡抄代码GetVariance

  • Today I only did a meaningful thing,which was a mathermatic speech.
  • The Data scientist is an old man(Tugluk·Abdurajak),he is so amiable. Thanks for his speech.
  • It took me two and half hours ,but it's worth watching.
#include<stdio.h>
#include<math.h>
int main()
{
	double s = 0, s1=0, x[10];
	printf("Please input 10 numbers:\n");
	for (int i = 0; i < 10; i++)
	{
		scanf("%f", &x[i]);
		s1 += x[i];
	}
	s1 = s1 / 10;
	for (int i = 0; i < 10; i++)
	{
		s = s + ((x[i]-s1) *( x[i]-s1));
	}
	s = s / 10;
	s = pow(s, 0.5);
	printf("The Variance is %lf", s);

	return 0;
}

The only failure there is is the failure to try

  • 勇于尝试,便不叫失败
c
115 views
Comments
登录后评论
Sign In
·

这好像是总体标准偏差,并不是方差(Variance),方差不开根号的

你的scanf中的格式符有点问题

scanf("%lf", &x[i]);

scanflf对应double *,用f会降级为float *的,精度有可能会丢失

不过printf中就随便了,默认按double输出

参考scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s - cppreference.com