2015年计算机二级C语言上机操作题及答案(25)
发布时间:2011/7/8 11:35:31 来源:城市学习网 编辑:ziteng
填空题
函数FUN的功能是:逆置数组元素中的值。形参N给出数组中的数据的个数。
例如:若A所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后依次为:9、8、7、6、5、4、3、2、1。
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序:#include <stdio.h>
void fun(int a[], int n)
{
int i, t;
for (i=0; i<___1___; i++)
{
t = a[i];
a[i] = a[n-1-___2___];
___3___ = t;
}
}
main()
{
int b[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, i;
printf("\nThe original data :\n");
for (i=0; i<9; i++)
printf("%4d ", b[i]);
printf("\n");
fun(b, 9);
printf("\nThe data after invert :\n");
for (i=0; i<9; i++)
printf("%4d ", b[i]);
printf("\n");
}
第1处填空:n/2
第2处填空:i
第3处填空:a[n-i-1] [NextPage] 改错题
下列给定程序中,函数fun的功能是:y计算n!.例如给n输入5,则输出120.000000.
请改正程序中的错误,使程序能输出 正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include <stdio.h>
#include <conio.h>
double fun(int n)
{
double result = 1.0;
/********found********/
if n == 0
return 1.0;
while (n>1 && n<170)
/********found********/
result = n--;
return result;
}
main()
{
int n;
printf("Input N:");
scanf("%d", &n);
printf("\n\n%d!=%1f\n\n", n, fun(n));
}
第1处:if n==0应改为if (n==0)
第2处:resylt =n--;应改为result *=n--;[NextPage] 编程题
编写函数fun,它的功能是:
比较两 个字符串的长度,(不得调用C语言中提供的求字符串长度的函数),函数返回较长的字符串,若两个字符串长度相同,则返回第一个字符串
例如,输入beijing shanghai<CR>(<CR>为回车键),函数将返回shanghai
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <stdio.h>
char *fun ( char *s, char *t)
{
}
main( )
{
char a[20],b[10],*p,*q;
int i;
FILE *out;
printf("Input 1th string:");
gets(a);
printf("Input 2th string:");
gets( b);
printf("%s\n", fun(a, b ));
out=fopen("out.dat", "w");
fprintf(out, "%s", fun("hunan", "changsha"));
fclose(out);
}
答案是:
char *fun(char *s,char *t)
{
char *p,*t1=t,*s1=s;
int n=0,m=0;
while(*s1)
{
n++;
s1++;
}
while(*t1)
{
m++;
t1++;
}
if(n>=m)
p=s;
else
p=t;
return p;
}