求出數組周邊元素的平均值並作為函數值返回給主函數中的s


一.下列程序定義了N×N的二維數組,並在主函數中賦值。請編寫函數fun,函數的功能是:求出數組周邊元素的平均值並作為函數值返回給主函數中的s。例如,若a 數組中的值為: 

0 1 2 7 9 

1 9 7 4 5 

2 3 8 3 1 

4 5 6 8 2 

5 9 1 4 1 

則返回主程序后s的值應為3.375。

#include <stdio.h>
#include <stdlib.h>
#define  N  5
double fun ( int w[][N] )
{
    int i,j,n=0;
    double s=0,av;
    for(i=0;i<N;i++)
    {
        s+=w[i][0];
        n++;
        s+=w[i][N-1];
        n++;
    }
    for(j=1;j<N-1;j++)
    {
        s+=w[0][j];
        n++;
        s+=w[N-1][j];
        n++;
    }
    av=s/n;
    
    return av; 
}

main ( )
{  int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
   int i, j;
   double s ;
   printf("***** The array *****\n");
   for ( i =0;  i<N; i++ )
   {  for ( j =0; j<N; j++ )
     {  printf( "%4d", a[i][j] ); }
        printf("\n");
   }
   s = fun ( a );
   printf ("***** THE  RESULT *****\n");
   printf( "The sum is :  %lf\n",s );
}

二.運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM