www.zhnq.net > C语言方差

C语言方差

C语言里面一个计算方差的函数:double variance(double x[ ],int n,double mean) { double divisor,sum; int k; for(sum=k=0;k<n;k++) sum+=pow(x[k]-mean,2) if(n<20)divorsor=n-1; else divisor=n; return sum/divisor;}

直接上代码:#include <stdio.h>#include <conio.h>#include <math.h> double fangcha(double x[], int n){ //求数组x(具有n个元素)的方差:S=(<x^2>-<x>)^0.5 int i; double xaver=0.0, x2aver=0.0; for(i=0;i<n;++i){ xaver+=x[i]; x2aver+=x[i]*x[i]; }

#include "stdio.h"#include "math.h"#define N 100void main(){int a[N],n,i;float aver,s;float sum=0,e=0;printf("请输入样本量:");scanf("%d",&n);printf("请输入%d个样本:",n);for(i=0;i

int a[9] = {1,2,3,4,5,6,7,8,9}; ar = 5 ///这个是平均数 自己算 to = 0.0f; for(i=0; i<9; i++){ to = (a[i]-ar)*(a[i]-ar);} to = to/9;

#include <stdio.h>#include <math.h> double s(double x[],int n); main() { int n,i; double x[50],vs; scanf("%d",&n); for (i=0;i<n;i++) scanf("%lf",&x[i]); vs=s(x,n); printf("%.6f",vs); return 0; } double s(double x[], int n) { int i; double sum=0,s=0,ave;

//计算平均数,方差,标准差.#include #include #include #include #include #define N 1

//计算平均数,方差,标准差.#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define N 100 void TongJi(double a[],int n,double *aver,double *vari,double *stdDev){ //对长度为n的数组a进行统计,统计

你是不是在做三网上机题啊?假设数字个数是MAXint i;for(i=0;i 评论0 0 0

1.求每一个数与这个样本数列的数学平均值之间的差,称均差;2.计算每一个差的平方,称方差;3.求它们的总和,再除以这个样本数列的项数得到均方差;4.再开根号得到标准方差!/* 方差和 */double num[10]; //用10长度的数组举例double sum = 0; //保存一下10个数的和double pow2_sum = 0; //临时保存方差和double __average; //临时保存均方差double ans; //均方差for(int i=0; i 评论0 0 0

关键代码如下:a[n] = {} //写代码输入整数 n代表整数个数,你自己定义 int nsum = 0; for( int i = 0; i{ nsum += a[i]; } m = nsum / m; //如果要浮点数,就加上强制转换 方差:int ndelta = 0;for (int j =0; j{ ndelta += (a[i] - m) * (a[i] -m);} ndelta /= n; ndelta = sqrt(ndelta);

网站地图

All rights reserved Powered by www.zhnq.net

copyright ©right 2010-2021。
www.zhnq.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com