请补充函数fun(),该函数的功能是:计算N×N维矩阵元素的方差,结果由函数返回。维数N在主函数中输入。例如: 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或

admin2009-02-15  82

问题 请补充函数fun(),该函数的功能是:计算N×N维矩阵元素的方差,结果由函数返回。维数N在主函数中输入。例如:
   
   注意:部分源程序给出如下。
   请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
   试题程序:
   #include <stdio.h>
   #include <conio.h>
   #include <stdlib.h>
   #include <math.h>
   #define N 20
   double fun(【  】,int n)
   {
      int i,j;
      int k;
      double s=0.0;
      double f=0.0;
      double aver=0.0;
      double sd=0.0;
      for(i=0;i<n;i++)
         for(j=0;j<n;j++)
            s+=a[j];
   aver=【  】;
   for(i=0;i<n;i++)
      for(j=0;i<n;j++)
      f+=(a[j]-aver)*(a[j]-aver);
   f/(n*n);
   sd=【  】;
   return sd;
   }
   main()
   {
      int a[N][N];
      int n;
      int i,j;
      double s;
      clrscr();
      printf("***+Input the dimension of
         array  N*****\n");
      scanf("%d",&n);
      printf("*****  The  array  *****\n");
      for(i=0;i<n;i++)
      {
         for(j=0;j<n;j++)
         {
             a[j]=rand()%50;
             while(a[j]=0)
              a[j]=rand()%60;
           printf("%4d",a[j]);
           }
        printf("\n\n");
     }
     s=fun(a,n);
     printf("******* THE RESULT *******\n");
     printf("%4.3f\n",s);
   }

选项

答案int a[][N] s/(n*n) sqrt(f)

解析 第一空;由主函数main()中对函数fun()的调用格式,可以知道,函数fun()的第一个参数是N×N的二维整型数组。第二空:平均值等于所有元素的累加和除以个数。第三空;根据公式,方差sd等于对变量f开平方,这里注意对数学库函数的调用。
转载请注明原文地址:https://kaotiyun.com/show/2TID777K
0

最新回复(0)