2015年计算机二级C语言上机操作题及答案(65)
发布时间:2011/7/20 13:13:00 来源:城市学习网 编辑:ziteng
填空题
请补充main函数,该函数的功能是:输出一个N*N矩阵,要求非周边元素赋值0,周边元素赋值1。
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他内容。
#include
#define N 10
main()
{
int bb[N][N];
int i, j, n;
printf(" \nInput n:\n");
scanf("%d", &n);
for (i=0; i for (j=0; j {
if (i==0||i==n-1||j==0||j==n-1)
___1___;
else
___2___;
}
printf("\n ***** the result ******* \n");
for (i=0; i {
printf("\n\n");
for (j=0; j printf("M", bb[i][j]);
}
}
答案:
第1处填空:bb[i][j]=1
第2处填空:bb[i][j]=0 [NextPage] 改错题
下列给定程序中函数fun的功能是:从低位开始取出长整型变量s中奇数位上的数,依次构成一个新数放在t中,例如,当s中的数为7654321时,t中的数为7531。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
/********found********/
void fun(long s, long t)
{
long s1 = 10;
*t = s;
while (s > 0)
{
s = s/100;
*t = s*s1 + *t;
/********found********/
s1 = s1*100;
}
}
main()
{
long s, t;
printf("\nPlease enter s:");
scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t);
}
答案:
第1处:void fun(long s, long t)应改为 void fun(long s, long *t)
第2处:s1=s1*100;应改为s1=s1*10; [NextPage] 编程题
请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的百位和个位上,b数的十位和个位数依次放在c数的十位和千位上。
例如,当a=45,b=12,调用该函数后,c=2415。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
void fun (int a, int b, long *c )
{
}
main ()
{
int a, b;
long c;
FILE *out ;
printf ("Input a, b;");
scanf ("%d%d", &a, &b);
fun (a, b, &c);
printf ("The result is : %ld\n", c);
out=fopen ("out.dat","w");
for (a = 20; a < 50; a+=3)
{
fun(a, 109-a, &c);
fprintf(out, "%ld\n", c);
}
fclose (out );
}
答案:
void fun (int a, int b, long *c)
{
*c=(b)1000+(a/10)*100+(b/10)*10+a;
}