求出数组周边元素的平均值并作为函数值返回给主函数中的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