问题 问答题

请补充函数fun(),该函数的功能是:输出一个N×N矩阵,N由键盘输入,矩阵元素的值为随机数,并计算出该矩阵四周边元素的平均值,结果由函数返回。例如:当N=4时:

注章:部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。

试题程序;

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define N 20

double fun(int a[ ][N],int n)

int i,j;

int k;

double s=0.0;

double aver=0.0;

printf("*****The array*****\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

a[i][j]=rand()%10;

printf("%4d",a[i][j]);

if( 【1】 )

s+=a[i][j];

printf("\n");

k= 【2】

aver= 【3】

return aver;

main()

int a[N][N];

int n;

double s;

Clrscr();

printf("*****Input the dimension of array N*****\n");

scanf("%d",&n);

s=fun(a,n);

printf("***** THE RESULT *****\n");

printf("The average is %2,3f\n",s);

答案

参考答案:

[1] i==0||i==n-1||j==0||j==n-1

[2] 4*n-4

[3] s/k

解析:

填空1:用二维数组表示n×n矩阵时,周边元素是行下标为0或n-1,列下标为0或n-1的元素,判断时四个条件中只要有一个条件满足,则该元素就是周边元素,所以用了运算符。填空2:变量k记录周边元素的个数,在四个顶角上的元素会重复加入,所以要减去4。填空3:变量s保存周边元素的累加和,平均值等于周边元素的累加和除以个数。

填空题
判断题