计算C语言中的求和、标准差、方差和标准差等,需要加上头文件:#include <math.h>
#include<stdio.h>
#include "math.h"
double sum = 0;//求和
double array[4] = {1.2,2.1,3.1,4.1};
int length = 0;//数组长度
double average = 0;//求平均数
double var = 0; //求方差
double standard = 0; //求标准差
int main()
{
for (int i = 0; i <= 3;i++)
{
sum += array[i];//求和
}
length = sizeof(array) / sizeof(array[0]);//求数组长度
average = sum / length;//求平均值
for (int j = 0; j <= 3;j++)
{
var += pow(array[j]-average,2)/length;//求方差
}
standard = pow(var,0.5);//求标准差
printf("sum = %g\n",sum);//这边打印也可以使用printf("%f\n",sum);这种打印默认出来的数是小数点后6位
printf("length of array[4] = %d\n",length);
printf("average = %g\n",average);
printf("var = %g\n",var);
printf("standard = %g\n",standard);
while (1);
return 0;
}