C语言数组fun函数逆置数组元素,计算机C语言二级上机考试套题(十七)

第66套

1. 程序填空题:

从键盘输入一组小写字母,保存在字符数组str中。请补充函数fun,该函数的功能是:把字符数组str中字符下标为奇数的小写字母转换成对应的大写字母,结果仍保存在原数组中。

例如,输入"acegikm",则输出"aCeGiKm"。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。

试题程序:

#include

#include

#define N 80

void fun (char s[])

{

int I=0;

while (【1】)

{

if(I%2!=0)

s[I]-=【2】;

【3】;

}

}

void main()

{

char str[N];

system("CLS");

printf("\n Input a string:\n");

gets(str);

printf("\n*** original string ***\n");

puts(str);

fun(str);

printf("\n*** new string ***\n");

puts(str);

}

2. 程序改错题:

下列给定程序中,函数fun的功能是:计算整数n的阶乘。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

试题程序:

#include 

#include 

double fun(int n)

{

double result=1.0;

while(n>1&&n<170)

result*=--n;

return;

}

void main()

{int n;

system("CLS");

printf("Enter an integer: ");

scanf("%d",&n);

printf("\n\n%d!=%1g\n\n ",n,fun(n));

}

3. 程序设计题:

请编写函数fun,该函数的功能是:计算n门课程的平均分,计算结果作为函数值返回。

例如,有5门课程的成绩是90.5,72,80,61.5,55,则函数的值为71.80。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:

#include

float fun (float *a, int

n)

{

}

void main()

{

FILE *wf;

float

score[30]={90.5,72,80,61.5,55},aver;

aver=fun(score,5);

printf("\nAverage score is: %5.2f\n",aver);

wf=fopen("out.dat","w");

fprintf(wf,"%5.2f",aver);

fclose(wf);

}

【参考答案】:1. (1) s[i]!='\0'或s[i]

(2)

32或'a'-'A' (3) i++

2.

(1)错误:result*=--n; 正确:result*=n--;

(2)错误:return; 正确:return result;

3. float fun (float *a, int n)

{

float

av=0.0;

int

i;

for(i=0;i

av=av+a[i];

return

(av/n);

}

第67套

1. 程序填空题:

函数fun的功能是:逆置数组元素中的值。形参n给出数组中数据的个数。

例如,若a所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后为:9、8、7、6、5、4、3、2、1。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。

试题程序:

#include

void fun(int a[],int n)

{

int

I,t;

for(I=0;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("\n\the data after inbert:\n");

for(i=0;i<9;i++)

printf("%4d",b[i]);

printf("\n");

}

2.程序改错题:

下列给定程序中,函数fun的功能是:应用递归算法求某数a的平方根。求平方根的迭代公式如下:

例如,2的平方根为1.414214。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

试题程序:

#include 

#include 

fun(double a,double x0)

{ double x1,y;

x1=(x0+a/x0)/2.0;

if(fabs(x1-x0)>0.00001)

y=fun(a,x1);

else y=x1;

return y;

}

void main()

{ double x;

printf("Enter x: ");

scanf("%lf",&x);

printf("The square root of %lf is %1f\n",x,

fun(x,1.0));

}

3.程序设计题:

请编写函数fun,该函数的功能是:统计一行字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间有若干个空格隔开,一行的开始没有空格。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:

#include

#include

#define N 80

int fun(char *s)

{

}

void main()

{

FILE *wf;

char line[N];

int num=0;

printf("Enter a string:\n ");

gets(line);

num=fun(line);

printf("The number of word is:%d\n\n

",num);

wf=fopen("out.dat","w");

fprintf(wf,"%d",fun("a big car"));

fclose(wf);

}

【参考答案】:1. (1)

n/2 (2)

i (3) a[n-i-1]

2. (1)错误:fun(double a,double x0)

正确:double fun(double a,double x0)

(2)错误:if(fabs(x1-x0)>0.00001)

正确:if(fabs(x1-x0)>=0.00001)

3. int fun(char *s)

{

int

i,j=0;

for(i=0;s[i]!='\0'

if(s[i]!=' '&&(s[i+1]=='

'||s[i+1]== '\0'))

j++;

return

j;

}

第68套

1.

程序填空题:

请补充main函数,该函数的功能是:把一维数组中的元素逆置。结果仍然保存在原数组中。

注意:部分源程序给出如下。

请勿改动函数中的其他任何内容,仅在横线上填入所编写的若干表达式或语句。

试题程序:

#include

#include

#define N 10

void main()

{

int I, j, t;

int bb[N];

system("CLS");

for(I=0;I

bb[I]=I;

printf("\n*** original list ***\n");

for(I=0;I

printf("%4d",bb[I]);

for(j=0,【1】; j<=I;j++,I--)

{

t=bb[j];

【2】;

bb[I]=t;

}

printf("\n****** new list ******\n");

for(I=0;I

printf("%4d",bb[I]);

}

2. 程序改错题:

下列给定程序中,函数fun的功能是根据输入的3个边长(整型值),判断能否构成三角形:若能构成等边三角形,则返回3,若是等腰三角形,则返回2,若能构成三角形则返回1,若不能,则返回0。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

试题程序:

#include 

#include 

int fun(int a,int b,int c)

{

if(a+b>c&&b+c>a&&a+c>b)

{if(a==b&&b==c)

return 1;

else if(a==b||b==c||a==c)

return 2;

else return 3;

}

else return 0;

}

void main()

{

int a,b,c,shape;

printf("\nInput a,b,c: ");

scanf("%d%d%d",&a,&b,&c);

printf("\na=%d, b=%d, c=%d\n",a,b,c);

shape=fun(a,b,c);

printf("\n\nThe shape :%d\n",shape);

}

3.程序设计题:请编写函数fun,其功能是:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位依次放在c数的千位和十位上,b数的十位和个位依次放在c数的百位和个位上。

例如,当a=45,b=12,调用该项函数后,c=4152。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:

#include

#include

void fun(int a ,int b,long *c)

{

}

void 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=10;a<20;a++)

{

fun(a,109-a,&c);

fprintf(out,"%d\n",c);

}

fclose(out);

}

【参考答案】:1. (1) --i或i--或i-=1或i=i-1

(2) bb[j]=bb[i]

2. (1)错误:return

1; 正确:return 3;

(2)错误:return 3; 正确:return 1;

3. void fun(int a,int b,long *c)

{

*c=(a/10)*1000+(b/10)*100+(a%10)*10+b%10;

}

第69套

1.程序填空题:请补充main函数,该函数的功能是:从键盘输入若干字符放到一个字符数组中,当按回车键时结束输入,最后输出这个字符数组中的所有字符。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达式或语句。

试题程序:

#include

#include

#include

void main()

{

int I=0;

char s[81];

char *p=s;

system("CLS");

printf(" Input a string \n");

for(I=0;I<80;I++)

{

s[I]=getchar();

if(s[I]=='\n')

【1】;

}

s[I]=【2】;

printf(" display the string \n");

while(*p)

putchar(【3】);

}

2.程序改错题:

下列给定程序中,函数fun的功能是:计算s所指字符串中含有t所指字符串的数目,并作为函数值返回。

请改正函数fun中的错误或在横线处填上适当的内容并把横线删除,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。

试题程序:

#include 

#include 

#include 

#include 

#define N 80

int fun(char *s,char *t)

{ int n;

char *p, *r;

n=0;

p=&s[0];

*r=t;

while(*p)

{

if(*r==*p)

{

r++;

if(*r=='\0')

{

n++;

【1】;

}

}

p++;

}

return n;

}

void main()

{char a[N],b[N]; int m;

system("CLS");

printf("\nPlease enter string a: ");

gets(a);

printf("\nPlease enter substring b: ");

gets(b);

m=fun(a,b);

m=printf("\nThe result is :m=%d\n",m);

}

3.程序设计题:

请编写函数fun,该函数的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。

例如,若二维数组中的数据为:

W W W W

S S S S

H H H H

则字符串中的内容应是:WSHWSHWSHWSH。

注意:部分源程序给出如下。

请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:

#include

#define M 3

#define N 4

void fun(char (*s)[N],char *b)

{

}

void main()

{

FILE *wf;

char a[100],w[M][N]={{ 'W', 'W', 'W',

'W'},{'S', 'S', 'S', 'S'},{'H', 'H', 'H', 'H'}};

int i,j;

printf("The matrix:\n");

for(i=0;i

{ for(j=0;j

printf("%3c",w[i][j]);

printf("\n");

}

fun(w,a);

printf("The A string:\n");

puts(a);

printf("\n\n");

wf=fopen("out.dat","w");

fprintf(wf,"%s",a);

fclose(wf);

}

【参考答案】:1. (1)

break (2)'\0' (3)*p++

2.

(1)错误:*r=t; 正确:r=t;

(2)应填:r=t;或r=&t[0];

3. void fun(char (*s)[N],char *b)

{

int

i,j,k=0;

for(i=0;i

for(j=0;j

b[k++]=s[j][i];

b[k]='\0';

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值