C language programming exam questions and answers, 2017 computer secondary exam C language programming questions practice questions and answers

2017 Computer Level 2 Exam C language programming questions practice questions and answers

11.3 Programming problems

The records of students are composed of student numbers and grades. The data of N students has been put into the structure array s in the main function. Please write the function fun. Its function is: put the data of the students with the highest scores in the array pointed to by b middle. Note: There may be more than one student with the highest score, the function returns the number of students with the highest score.

【Reference answer】

int fun( STREC *a, STREC *b )

{int i,j=0,max=a[0].s;

for(i=0;i

if(max

for(i=0;i

if(max==a[i].s)

b[j++]=a[i];

return j;

}

12.3 Programming problems

Specifies that the input string contains only letters and *. Please write the function fun, whose function is to delete all other * signs in the string except the leading * sign in the string. When writing functions, the string functions provided by the C language must not be used.

For example, the content in the string is: ****A*BC*DEF*G*******, after deletion, the content in the string should be: ****ABCDEFG.

【Reference answer】

void fun( char *a )

{

int i=0;

char *p=a;

while(*p&&*p=='*')

{a[i]=*p;

i++;

p++;}

while(*p)

{if(*p!='*')

{a[i]=*p;i++;}

p++;}

a[i]='\0';

}

13.3 Programming problems

Write the function void fun(char *tt, int pp[]) to count the number of occurrences of 26 lowercase letters from 'a' to 'z' in the string pointed to by tt, and place them in the array pointed to by pp in turn . For example, when the string abcdefgabcdeabc is entered, the output of the program should be:

3 3 3 2 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

【Reference answer】

void fun(char *tt, int pp[])

{int i;

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

pp [i] = 0;

for(;*tt!='\0';tt++)

if(*tt>='a'&&*tt<='z')

pp[*tt-'a']++;

}

14.3程序设计题

规定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:将字符串尾部的*号全部删除,前面和中间的*号不动。例如,字符串中的内容为:"****A*BC*DEF*G*******",删除后,字符串中的内容应当是:"****A*BC*DEF*G"。在编写函数时,不得使用C语言提供的字符串函数。

【参考答案】

void fun( char *a )

{while(*a!='\0')

a++;

a--;

while(*a=='*')

a--;

*(a+1)='\0';

}

15.3程序设计题

编写函数fun,其功能是:比较字符串的长度,(不得使用C语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符长度相同,则返回第一个字符串。例如,输入beijingshanghai(为回车键),函数将返回shanghai。

【参考答案】

char *fun ( char *s, char *t)

{int i,j;

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

for(j=0;t[j]!= '\0';j++);

if(i<=j) return t;

else return s;

}

16.3程序设计题

学生的记录由学号成绩组成,N名学生的数据已放入主函数中的结构体数组s中,请编写函数fun,其功能是:函数返回该学号的学生数据,指定的学号在主函数中输入。若没找到指定学号,在结构体变量中给学号置空串,给成绩置-1,作为函数值返回。(用于字符串比较的函数是strcmp)。

【参考答案】

STREC fun( STREC *a, char *b )

{int i;

STREC str={"\0",-1};

for(i=0;i

if(strcmp(a[i].num,b)==0)

str=a[i];

return str;

}

17.3程序设计题

函数fun的功能是:将s所指字符串中除下标为偶数同时ASCII码值也为偶数的字符外,其余的全部删除;字符串中剩余字符所形成的新串放在t所指的数组中。例如,若s所指字符串中的内容为"ABCDEFG123456",其中字符A的ASCII码值为奇数,因此应当删除;字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;字符2的ASCII码值为偶数,在数组中

的下标也为偶数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是"246"。

【参考答案】

void fun(char *s, char t[])

{int i,j=0;

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

if(i%2==0 && s[i]%2==0)

t[j++]=s[i];

t[j]='\0'; }

18.3程序设计题

编写函数fun,其功能是:利用下面的简单迭代方法求方程cos(x)-x=0的一个实根。

迭代步骤如下:

(1)取x1初值为0.0;

(2)x0=x1,将x1的值赋给x0;

(3)x1=cos(x0),求出一个新的x1;

(4)若x0-x1的绝对值小于0.000001,

执行步聚(5),否则执行步聚(2);

(5)所求x1就是方程cos(x)-x=0的一

个实根,作为函数值返回。

程序将输出结果Root=0.739086。

【参考答案】

double fun()

{ double x0,x1;

x1=0.0;

do{

x0=x1;

x1=cos(x0);

}while(fabs(x0-x1)>=1e-6);

return x1;}

19.3程序设计题[所属年份:2010.9;2011.3;]

规定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:将字符串中的前导*号全部移到字符串的尾部。例如,字符串中的内容为:"*******A*BC*DEF*G****",移动后,字符串中的内容应当是:"A*BC*DEF*G***********"。在编写函数时,不得使用C语言提供的.字符串函数。

【参考答案】

void fun( char *a )

{int i=0,n=0;

char *p;

p=a;

while (*p=='*')

{n++;p++;}

while(*p)

{a[i]=*p;i++;p++;}

while(n!=0)

{a[i]='*';i++;n--;}

a[i]='\0';

}

20.3程序设计题

学生记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组中,请编写函数fun,其功能是:把分数最低的学生数据放入b所指的数组中,注意:分数最低的学生可能不止一个,函数返回分数最低的学生人数。

【参考答案】

int fun( STREC *a, STREC *b )

{int i,j=0,min=a[0].s;

for(i=0;i

if(min>a[i].s)

min=a[i].s;

for(i=0;i

if(min==a[i].s)

b[j++]=a[i];

return j;

}

21.3程序设计题

编写函数fun,其功能是计算:

s作为函数值返回。在C语言中可调用log(n)函数求ln(n)。log函数的引用说明为:double log(double x)。例如,若m的值为20,则fun函数值为6.506583。

【参考答案】

double fun( int m )

{int i;

double s=0.0;

for(i=1;i<=m;i++)

s=s+log(i);

return sqrt(s);

}

22.3程序设计题[所属年份:2010.9;2011.3;]

规定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:只删除字符前导和尾部的*号,串中字母间的*号都不删除。形参n给出了字符串的长度,形参h给出了字符串中前导*号的个数,形参e给出了字符串中尾部*号的个数。在编写函数时,不得使用C语言提供的字符串函数。例如,字符串中的内容为:"****A*BC*DEF*G*******",删除后,字符串中

的内容应当是:"A*BC*DEF*G"。

【参考答案】

void fun( char *a, int n,int h,int e )

{int i,j=0;

for(i=h;i

a[j++]=a[i];

a[j]='\0'; }

23.3程序设计题

函数fun的功能是:将s所指字符串中下标为偶数的字符删除,剩余字符形成的新串放在t所指数组中。例如,当s所指字符串中的内容为"ABCDEFGHIJK"时,在t所指数组中的内容应是:"BDFHJ"。

【参考答案】

void fun(char *s, char t[])

{int i,j=0,k=strlen(s);

for(i=1;i

t[j++]=s[i];

t[j]='\0';}

24.3程序设计题

编写函数fun,其功能是:将a、b中的两个两位正整数合并成一个新的整数放在c中。合并的方式是:将a中的十位和个位数依次放在变量c的百位和个位上,b中的十位和个位数依次放在变量c的十位和千位上。

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

【参考答案】

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

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

}

25.3程序设计题

假定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:除了尾部的*号之外,将字符中的其它的*号全部删除。形参p已指向字符串中最后的一个字母。在编写函数时,不得使用C语言提供的字符串函数。

例如,字符串中的内容为****A*BC*DEF*G*******,删除后,字符串中的内容应当是ABCDEFG*******。

【参考答案】

void fun( char *a, char *p )

{char *t=a;

for(;t<=p;t++)

if(*t!='*')

*(a++)=*t;

for(;*t!='\0';t++)

*(a++)=*t;

*a='\0';}

26.3程序设计题

学生的记录由学号和成绩组成,N名学生的数据已放入主函数中的结构体数组s中,请编写函数fun,其功能是:按分数降序排列学生的记录,高分在前,低分在后。

【参考答案】

void fun( STREC a[] )

{int i,j;

STREC t;

for(i=1;i

for(j=0;j

if(a[j].s

{t=a[j];a[j]=a[j+1];a[j+1]=t;}

}

27.3程序设计题

学生的记录由学号和成绩组成。N名学生的数据已放入主函数中的结构体数组s中,请编写函数fun,其功能是:把高于等于平均分的学生数据放在b所指的数组中,高于等于平均分的学生人数通过形参n传回,平均分通过函数值返回。

【参考答案】

double fun( STREC *a, STREC *b, int *n )

{int i;

double av=0.0;

*n=0;

for(i=0;i

av=av+a[i].s;

av=av/N;

for(i=0;i

if(av<=a[i].s)

{ b[*n]=a[i];*n=*n+1;}

return av; }

28.3程序设计题

编写函数fun,其功能是:将1到m之间(含m)能被7或11整除的所有整数放在数组a中,并通过n返回这些数的个数。例如,若传给m的值为50,则程序输出:

7 11 14 21 22 28 33 35 42 44 49

【参考答案】

void fun ( int m, int *a , int *n )

{int i,j=0;

for(i=1;i<=m;i++)

if(i%7==0||i%11==0)

a[j++]=i;

*n=j;

}

29.3程序设计题

规定输入的字符串中只包含字母和*号。编写函数fun,其功能是:将字符串中的前导*号全部删除,中间和尾部的*号不删除。

例如,字符串中的内容为:"*******A*BC*DEF*G****",删除后字符串中的内容应当是:"A*BC*DEF*G****"。编写函数时,不得使用C语言提供的字符串函数。

【参考答案】

void fun( char *a )

{char *p=a;

while(*p=='*') p++;

for(;*p!='\0';p++,a++)

*a=*p;

*a='\0';

}

30.3程序设计题

N名学生的成绩已在主函数中放入一个带有头节点的链表结构中,h指向链表的头节点。请编写函数fun,其功能是:找出学生的最高分,并由函数值返回。

【参考答案】

double fun( STREC *h )

{double max=h->s;

while(h!=NULL)

{if(maxs) max=h->s;

h=h->next;}

return max;

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324479460&siteId=291194637