使用VC++2010打开考生文件夹下prog1中的解决方案。此解决方案的项目中包含一个源程序文件prog1.c。在此程序中,定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N],int m),该函数的功能是使数组右上半三角元

admin2018-08-11  33

问题 使用VC++2010打开考生文件夹下prog1中的解决方案。此解决方案的项目中包含一个源程序文件prog1.c。在此程序中,定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N],int m),该函数的功能是使数组右上半三角元素中的值乘以m。
例如,若m的值为2,a数组中的值为:

则返回主程序后a数组中的值应为:

注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
试题程序:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N5
void fun(int a[][N],int m)
{

}
main()
{
int a[N][N],m,i,j;
FILE*out;
printf("***The array***\n");
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{a[j]=rand()%20;
printf("%4d",a[j]);
}
printf("\n");
}
m=rand()%4;
printf("m=%4d\n",m);
fun(a,m);
printf("THE RESULT\n");
for(i=0;i<N;i++)
{for(j=0;jj<N;j++)
printf("%4d",a[j]);
printf("\n");
}
/*********found*********/
out=fopen("out.dat","w");
for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[j]=i*j;
fun(a,8);
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
fprintf(out,"%4d",a[j]);
fprintf(out,"\n");
}
fclose(out);
/*********found*********/
}

选项

答案void fun(int a[][N],int m) { int i,j; for(j=0;j<N;j++) for(i=0;i<=j;i++) a[i][j]=a[i][j]*m; /*右上半三角元素中的值乘以m*/ }

解析 本程序实现将矩阵中右上半三角元素中的值乘以m,使用循环语句遍历数组元素,第1个循环用于控制行坐标,第2个循环用于控制列下标。
转载请注明原文地址:https://kaotiyun.com/show/Djxp777K
0

最新回复(0)