2015年计算机二级C语言上机操作题及答案(18)
发布时间:2011/7/4 11:03:46 来源:城市学习网 编辑:ziteng
第18套
填空题
Str为一个字符序列.请补充FUN函数的功能是:查找STR中值为X 的元素,返回找到值为X的元素个数,并把这些值为X的元素下标依次保存在数组BB中.
例如,在”abcdefahij”中查找‘A’,结果为:2个‘A’,下标依次为0、6。
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序:#include <stdio.h>
#include <conio.h>
#define N 20
int bb[N];
int fun(char *str, char ch)
{
int i = 0, n = 0;
char t = ch;
char *p = str;
while (*p)
{
if (___1___)
___2___;
p++;
i++;
}
return ___3___;
}
main()
{
char str[N];
char ch;
int i, n;
printf("******* Input the original string*******\n ");
gets(str);
printf("******* The Original string *******\n");
puts(str);
printf("******* Input character *******\n");
scanf("%c", &ch);
n = fun(str, ch);
printf(" \nThe number of character is: %d\n", n);
printf("******* The suffix of character *******\n");
for (i=0; i<n; i++)
printf(" %d ", bb[i]);
}
第1处填空:*p==t或t==*p
第2处填空:bb[n++]=i
第3处填空:n [NextPage] 改错题
下列给定程序中,函数FUN的功能是:实现两个整数的交换。例如给A和B分别输入60和65,输出为:A=65 B=60。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
试题程序:#include <stdio.h>
#include <conio.h>
/********found********/
void fun(int a, int b)
{
int t;
/********found********/
t = b; b = a; a = t;
}
main()
{
int a, b;
printf("Enter a,b: ");
scanf("%d%d", &a, &b);
fun(&a, &b);
printf("a=%d b=%d\n", a, b);
}
第1处:void fun(int a,int b)应改为void fun(int *a,int *b)
第2处:t=b;b=a;a=t;应改为t=*b;*b=*a;*a=t; [NextPage] 编程题
编写程序,实现矩阵(3行列)的转置(即行列互换)
例如,输入如下的矩阵:
100 200 300
400 500 600
700 800 900
则程序输出:
100 400 700
200 500 800
300 600 900
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <stdio.h>
#include <conio.h>
void fun(int array[3][3])
{
}
main()
{
int i,j;
int array[3][3]={{100,200,300},
{400,500,600},
{700,800,900}};
FILE *out;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%7d",array[i][j]);
printf("\n");
}
fun(array);
printf("Converted array:\n");
out = fopen("out.dat", "w");
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%7d",array[i][j]);
fprintf(out, "%7d",array[i][j]);
}
printf("\n");
fprintf(out, "\n");
}
fclose(out);
}
答案是:
void fun(int array[3][3])
{
int i,j,temp;
for(i=0;i<3;i++)
for(j=0;j<I;j++)
{
temp=array[i][j];
array[i][j]=array[j][i];
array[j][i]=temp;
}
}