1. 程式人生 > >C語言:計算並輸出給定10個數的方差。

C語言:計算並輸出給定10個數的方差。

include pen printf style i++ double The span col

//計算並輸出給定10個數的方差。

 1 #include<math.h>
 2 #include<stdio.h>
 3 double fun(double x[10])
 4 {
 5     double p = 0.0,f=0.0,g=0.0;
 6     for (int i = 0; i < 10; i++)
 7     {
 8         p += x[i];
 9     }
10     p = p / 10;
11     printf("%f\n", p);
12     for (int j = 0; j < 10; j++)
13 { 14 g = x[j] - p; 15 f += g*g; 16 printf("%f\n", f); 17 } 18 f = f / 10; 19 f = sqrt(f); 20 return f; 21 } 22 void main() 23 { 24 FILE *wf; 25 double s,x[10]={95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0}; 26 int i; 27 printf("\nThe original data is:\n
"); 28 for(i=0;i<10;i++) 29 printf("%6.1f ",x[i]); 30 printf("\n\n "); 31 s=fun(x); 32 printf("s=%f\n\n ",s); 33 /******************************/ 34 wf=fopen("out.dat","w"); 35 fprintf(wf,"%f",s); 36 fclose(wf); 37 /*****************************/ 38 }

C語言:計算並輸出給定10個數的方差。