[二级C试题天天练]C语言考试试题5
3. 编程题
下列程序定义了n×n的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][n]),该函数的功能是:使数组右上半三角元素中的值全部置成0。例如a数组中的值为
a=4 5 6
1 7 9
3 2 6,
则返回主程序后a数组中的值应为
0 0 0
1 0 0
3 2 0
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define n 5
int fun (int a[][n])
{
}
main()
{
int a[n][n],i,j;
clrscr();
printf("*****the array*****\n");
for(i=0;i<n;i++) /*产生一个随机的5*5矩阵*/
{ for(j=0;j<n;j++)
{a[i][j]=rand()%10;
printf("%4d", a[i][j]);
}
printf("\n");
}
fun(a);
printf("the result\n");
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
3. 编程题解析
int fun (int a[][n])
{
int i,j;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
a[i][j]=0; /*将数组右上半三角元素中的值全部置成0*/
}
【解析】本题旨在考查控制数组中右上半三角元素的算法,也就是两个千篇一律的循环语句,希望学习者能够掌握消化。