3. 編程題
下列程序定義了n×n的二維數(shù)組,并在主函數(shù)中自動賦值。請編寫函數(shù)fun(int a[][n]),該函數(shù)的功能是:使數(shù)組右上半三角元素中的值全部置成0。例如a數(shù)組中的值為
a=4 5 6
1 7 9
3 2 6,
則返回主程序后a數(shù)組中的值應為
0 0 0
1 0 0
3 2 0
注意:部分源程序給出如下。
請勿改動主函數(shù)main和其他函數(shù)中的任何內容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。
試題程序:
#include
#include
#include
#define n 5
int fun (int a[][n])
{
}
main()
{
int a[n][n],i,j;
clrscr();
printf("*****the array*****\n");
for(i=0;i { for(j=0;j {a[i][j]=rand()%10;
printf("%4d", a[i][j]);
}
printf("\n");
}
fun(a);
printf("the result\n");
for(i=0;i { for(j=0;j printf("%4d",a[i][j]);
printf("\n");
}
}
3. 編程題解析
int fun (int a[][n])
{
int i,j;
for(i=0;i for(j=i;j a[i][j]=0; /*將數(shù)組右上半三角元素中的值全部置成0*/
}
【解析】本題旨在考查控制數(shù)組中右上半三角元素的算法,也就是兩個千篇一律的循環(huán)語句,希望學習者能夠掌握消化。
下列程序定義了n×n的二維數(shù)組,并在主函數(shù)中自動賦值。請編寫函數(shù)fun(int a[][n]),該函數(shù)的功能是:使數(shù)組右上半三角元素中的值全部置成0。例如a數(shù)組中的值為
a=4 5 6
1 7 9
3 2 6,
則返回主程序后a數(shù)組中的值應為
0 0 0
1 0 0
3 2 0
注意:部分源程序給出如下。
請勿改動主函數(shù)main和其他函數(shù)中的任何內容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。
試題程序:
#include
#include
#include
#define n 5
int fun (int a[][n])
{
}
main()
{
int a[n][n],i,j;
clrscr();
printf("*****the array*****\n");
for(i=0;i
printf("%4d", a[i][j]);
}
printf("\n");
}
fun(a);
printf("the result\n");
for(i=0;i
printf("\n");
}
}
3. 編程題解析
int fun (int a[][n])
{
int i,j;
for(i=0;i
}
【解析】本題旨在考查控制數(shù)組中右上半三角元素的算法,也就是兩個千篇一律的循環(huán)語句,希望學習者能夠掌握消化。