请编写函数fun,该函数的功能是:求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。 例如,若二维数组中的值为: 1 3 5 7 9 2 9 9 9 4

admin2016-12-06  24

问题 请编写函数fun,该函数的功能是:求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。
    例如,若二维数组中的值为:
    1    3    5    7    9
    2    9    9    9    4
    6    9    9    9    8
    1    3    5    7    0
  则函数值为61。
  注意:部分源程序在文件PROG1.C中。
  请勿改主动函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
    试题程序:
1   #include<conio.h>
2   #include<stdio.h>
3   #include<stdlib.h>
4  #defihe M 4
5  #defihe N 5
6  int fun(int a[M][N])
7   {
8
9   }
10   void main()
11   {
12    FILE*wf;
13   int aa[M][N]={{1,3,5,7,9},{2,9,9,9,4},{6,9,9,9,8},{1,3,5,7,0}};
14   int i,j,y;
15    aystem(’’CLS’’);
16    printf(’’The origina1 data is:\n’’);
17   for(i=0;i18    {for(j=0 ; j19    printf(’’%6d’’,aa[j]);
20    printf(’’\n’’);
21    }
22   y=fun(aa);
23    printf(’’\nThe sun:%d\n’’,y);
24    printf(’’\n’’);
25   /*****************/
26   wf=fopen(’’out.dat’’,’’w’’);
27   fprintf(wf,’’%d’’,y);
28  fclose(wf);
29  /*****************/
30   }

选项

答案1 int fun(int a[M][N]) 2 { 3 int i,j,sum=0; 4 for(i=0;i
解析 本题采用逐一判断的方式。二维数组周边元素的规律是,其两个下标值中一定有一个是0或M-1或N-1。程序中循环语句用来控制数组的行和列,条件语句用来判断数组元素是否为周边元素。
转载请注明原文地址:https://kaotiyun.com/show/2NDp777K
0

最新回复(0)