//计算输出给定数组中每相邻两个元素的平均值的平方根之和。
1 #include <stdio.h> 2 #include <math.h> 3 double fun(double x[9]) 4 { 5 double a,b,s=0.0; 6 for (int i = 0; i < 8;) 7 { 8 //a = (x[i++] + x[i]) / 2;//不可以这样使用,同一条语句中,i相当于还没有改变。 9 a = x[i]; 10 a+=x[++i]; 11 a /= 2; 12 b = sqrt(a);//求平方根 13 s = s + b; 14 printf("%f\n", a); 15 printf("%f\n", b); 16 printf("%f\n", s); 17 } 18 return s; 19 } 20 21 void main() 22 {double s,a[9]={12.0,34.0,4.0,23.0,34.0,45.0,18.0,3.0,11.0}; 23 int i; 24 FILE *out; 25 printf("\nThe original data is : "); 26 for(i=0;i<9;i++) 27 printf("%6.1f",a[i]); 28 printf("\n\n"); 29 s=fun(a); 30 printf("s=%f\n\n",s); 31 /******************************/ 32 out=fopen("out.dat","w"); 33 fprintf(out,"%f",s); 34 fclose(out); 35 /******************************/ 36 }