1.
请补充函数fun(),该函数的功能是:把从主函数中输入的字符串str2接在字符串str1的后面。
例如:str1=“How do”,str2=“ you do?”,结果输出:How do you do?
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
#include
#include
#define N 40
void fun(char *str1,char *str2)
{
int i=0;
char *p1=str1;
char *p2=str2;
while(【1】)
i++;
for( ;【2】;i++)
*(p1+i)=【3】;
*(p1+i)='\0';
}
main()
{
char str1[N],str2[N];
clrscr();
printf("*****Input the string str1 &
str2*****\n");
printf(" \nstr1:");
gets(str1);
printf(" \nstr2:");
gets(str2);
printf("**The string str1 & str2**\n");
puts(str1);
puts(str2);
fun(str1,str2);
printf("*****The new string *****\n");
puts(str1);
}
2.
改错题
下列给定程序中,函数fun()的作用是:将字符串tt中的小写字母都改为对应的大写字母,其他字符不变。例如,若输入"edS,dAd",则输出"EDS,DAD"。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
#include
#include
/**********************found***********************/
char fun(char tt[])
{
int i;
for(i=0;tt[i];i++)
{
/**********************found***********************/
if((tt[i]>='A')&&(tt[i]<= 'Z'))
tt[i]-=32;
}
return(tt);
}
main()
{
int i;
char tt[81];
clrscr();
printf("\nPlease enter a string: ");
gets(tt);
printf("\nThe result string is: \n%s",fun(tt));
}
3.
编程题
请编写函数fun(),该函数的功能是:移动一维数组中的内容,若数组中有n个整数,要求把下标从p到n-1(p≤n-1)的数组元素平移到数组的前面。
例如,一维数组中的原始内容为1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,p的值为6。移动后,一维数组中的内容应为7,8,9,10,10,11,12,13,14,15,1,2,3,4,5,6。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#define N 80
void fun(int *w, int p, int n)
{
}
main()
{
int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i, p, n=15;
printf("The original data:\n");
for(i=0;i
printf("%3d",a[i]);
printf("\n\nEnter p: ");
scanf("%d",&p);
fun(a,p,n);
printf("\nThe data after moving:\n");
for(i=0;i
printf("%3d",a[i]);
printf("\n\n");
}
4.
填空题
请补充函数fun(),该函数的功能是求一维数组x[N]的平均值,并对所得结果进行四舍五入(保留两位小数)。
例如:当x[10]={15.6,19.9,16.7,15.2,18.3,12.1,15.5,11.0,
10.0,16.0},结果为:avg=15.030000。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
#include
#include
double fun(double x[10])
{
int i;
long t;
double avg=0.0;
double sum=0.0;
for(i=0;i<10;i++)
【1】;
avg=sum/10;
avg=【2】;
t=【3】;
avg=(double)t/100;
return avg;
}
main()
{
double avg,x[10]={15.6,19.9,16.7,15.2,
18.3,12.1,15.5,11.0,10.0,16.0};
int i;
clrscr();
printf("\nThe original data is :\n");
for(i=0;i<10;i++)
printf("%6.1f",x[i]);
printf("\n\n");
avg=fun(x);
printf("average=%f\n\n",avg);
}
5.
改错题
下列给定程序中,函数fun()的功能是:先从键盘上输入一个3行3列的矩阵的各个元素的值,然后输出主对角线元素之积。
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
int fun()
{
int a[3][3],mul;
int i,j;
mul=1;
for (i=0;i<3;i++)
{
/**********************found***********************/
for (i=0;j<3;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<3;i++)
/**********************found***********************/
mul=mul*a[i][j];
printf("Mul=%d\n",mul);
}
main()
{
fun();
}