zoukankan      html  css  js  c++  java
  • 求出数组周边元素的平均值并作为函数值返回给主函数中的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 *****
    ");
       for ( i =0;  i<N; i++ )
       {  for ( j =0; j<N; j++ )
         {  printf( "%4d", a[i][j] ); }
            printf("
    ");
       }
       s = fun ( a );
       printf ("***** THE  RESULT *****
    ");
       printf( "The sum is :  %lf
    ",s );
    }

    二.运行结果

  • 相关阅读:
    爬虫学习
    微软命令行安装第三方库
    Python复习(拾遗)3
    Python拾遗2
    Python复习(拾遗)
    python练习 自动绘图
    多分支结构
    turtle
    Python练习
    随便写点…
  • 原文地址:https://www.cnblogs.com/wlei5206/p/12836735.html
Copyright © 2011-2022 走看看