2015年计算机二级C语言上机操作题及答案(28)
发布时间:2011/7/8 11:37:44 来源:城市学习网 编辑:ziteng
第28套
填空题
程序的功能是计算。
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序:#include
long fun(int n)
{
int i;
long s;
s = ___1___;
for (i=1; i<=n; i++)
s = ___2___;
return s;
}
main()
{
long s;
int k, n;
scanf("%d", &n);
s = ___3___;
for (k=0; k<=n; k++)
s = ___4___;
printf("%ld\n", s);
}
第1处填空:1
第2处填空:s*i或i*s
第3处填空:0
第4处填空:s+fun(k)或fun(k)+s [NextPage] 改错题
在字符串的最前端加入n个*号,形成新串,
注意:字符串的长度允许为79。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include
#include
#include
void fun(char s[], int n)
{
char a[80], *p;
int i;
/********found********/
s = p;
for (i=0; i a[i] = '*';
do
{
a[i] = *p;
i++;
/********found********/
___ì???___
} while (*p);
/********found********/
a[i] = '0';
strcpy(s, a);
}
main()
{
int n;
char s[80];
printf("\nEnter a string :");
gets(s);
printf("\nThe string %s\n", s);
printf("\nEnter n(number of *): ");
scanf("%d", &n);
fun(s, n);
printf("\nThe string after inster: %s\n", s);
}
第1处:s=p;应改为p=s;
第2处:应填p++;或++p;或p+=1或p=p+1
第3处:a[i]=’0’;应改为a[i]=’\0’;或a[i]=0; [NextPage] 编程题
编写函数fun,它的功能是:
找出一维整数型数组元素中最大的值和它所在的下标,最大值和它的下标通过形参传回。数组元素中的值已在主函数中赋予,
主函数中x是数组名,n 是x 中的数据个数,max存放最大值,index中存放最大值所在元素的下标。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include
#include
#include
void fun ( int a[], int n, int *max, int *d )
{
}
main()
{
int i, x[20], max, index, n=10;
FILE *out;
for (i=0; i < n; i++)
{
x[i] = rand()P;
printf("M", x[i]) ;
}
printf("\n");
fun( x, n , &max, &index);
printf("Max=], Index=M\n", max, index);
out=fopen("out.dat", "w");
memcpy(x, "3.141592653589793238462643383279", 32);
fun( x, 8 , &max, &index);
fprintf(out, "Max=], Index=M", max, index);
fclose(out);
}
答案是:
void fun(int a[], int n,int *max, int *d)
{
int i;
*max=a[0];
*d=0;
for(i=0;iif(a[i]>*max)
{
*max=a[i];
*d=I;
}
}