下列给定程序中,函数fun()的功能是:对N名学生的学习成绩,按从低到高的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <string.h>
include <stdio.h>
include <alloc.h>
define N 10
typedef struct SS
{
char num[10];
int s;
}STU;
STU *fun(STU a[],int m)
{
STU b[N],*t;
int i, j,k;
/*************found*************/
*t=calloc(m,sizeof(STU));
for(i=0;i<N;i++)
b[i]=a[i];
for(k=0;k<m;k++)
{
/*************found*************/
for(i=j=0;i<N;j++)
if(b[i].s<b[j],s)
j=i;
/*************found*************/
t[k].s=b[j].s;
b[j].s=100;
}
return t;
}
outresult(STU a[],FILE *pf)
{
int i;
for(i=0;i<N;i++)
fprintf(pf,"NO=%S Mark=%d\n",
a[i].num,a[i].S);
fprintf(pf,"\n\n");
}
main()
{
STU a[N]={{“A01”,77},{“A02”,85},
{“A03”,96},{“A04”,65},{“A05”,75},
{“A06”,96},{“A07”,76},{“A08”,63},
{“A09”,69},{“A10”,78}};
STU *porder;
int i,m;
clrscr();
printf(“*****THE RESULT*****\n”);
outresult(a,stdout);
printf(“\nGive the number of the students who have lower score:”);
scanf(“%d”,&m);
while(m>10)
{
printf(“\nGive the number of the students who have lower score:”);
scanf(“%d”,&m);
}
porder=fun(a,m);
printf(“*****THE RESULT*****\n”);
printf(“The low:\n”);
for(i=0;i<m;i++)
printf(“%s %d\n”,porder[i].num,
porder[i].s);
free(porder);
}
第1题
符串所包含的指定字符的个数是奇数,则不予删除,如果其数目是偶数,则删除原串后半部分的指定字符。其中,a指向原字符串,删除后的字符串存放在b所指的数组中,c中存放指定的字符。
例如:当a输入“abababa”,c=‘a’时,b的输出为“ababb”;
如果a的输入为“ababa”,则b的输出为“ababa”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define LEN 80
void fun(char a[],char b[],char C)
{
int i=0,j=0;
int n=0;
int m=0;
while(a[i]!=‘\0’)
{
if(a[i]==c)
n++;
i++;
}
【 】;
if(n%2)
{
while(a[j]!='\0')
{
b[j]=a[j];
j++;
}
b[j]='\0';
}
else
while(a[i]!='\0')
{
b[j++]=a[i];
if(a[i]==c)
m+十;
if((m>n/2)&&(a[i]==c))
【 】;
i++;
}
【 】;
}
}
main()
{
char a[LEN],b[LEN];
char c;
clrscr();
printf("Enter the string:\n");
gets(a);
printf("Enter the character of the string deleted:");
scanf("%c",&c);
fun(a,b,c);
printf("The new string iS:%s\n",b);
}
第2题
请编写函数fun(),其功能是:计算并输出下列多项式值。
S=(1+1/2)+(1/3+1/4)+…+(1/(2n-1)+l/2n)
例如,若主函数从键盘给n输入12后,则输出为 S=3.775958。
n的值要求大于1但不大于100。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<stdio.h>
double fun(int n)
{
}
main()
{
int n;
double s;
printf("\nlnput n:");
scanf("%d",&n);
s=fun(n);
printf("\ns=%f\n",s);
}
第3题
下列给定程序中,函数fun()的功能是:用冒泡法对6个字符串按由大到小的顺序进行排序。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <stdio.h>
define MAXLINE 20
void fun(char *pstr[6])
{
int i,j;
char *p;
for(i=0;i<5;i++)
for(j=i+1;j<6;j++)
/*************found*************/
if(strcmp((pstr+i),(pstr+j))<0)
{
p=*(pstr+i);
*(pstr+i)=*(pstr+j);
/*************found*************/
*(pstr+j)=*p;
}
}
main()
{
int i;
char*pstr[6],str[6][MAXLINE];
clrscr();
for(i=0;i<6;i++)
pstr[i]=str[i];
printf("/nEnter 6 string(1 string at each line):\n");
for(i=0;i<6;i++)
scanf("%s",pstr[i]);
fun(pstr);
printf("The strings after sorting:\n");
for(i=0;i<6;i++)
printf("%s\n",pstr[i]);
}
第4题
已知字符A的ASCⅡ代码值为65,以下程序运行时若从键盘输入:B33<回车>.则输出结果是( )。
include
main()
{ char a,b;
a=getchar();scanf("%d",&B);
a=a-'A'+'0';b=b*2;
printf("%c%c\n",a,B);
}
第5题
以下sum函数的功能是计算下列级数之和:
请给函数中的各变量正确赋初值。
double sum(douLle x,int n)
{ int i;
double a,b,s;
【 】
for(i=1;i<=n;i++) {a=a*x;b=b*i;s=s+a/b;}
return s;
}
第6题
后输出这个字符数组中的所有字符。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<ctype.h>
main()
{
int i=0;
char a [81];
char *p=s;
clrscr ();
printf{" Input a string \n");
for (i=0; i<80; i++)
{
s [i] =getchar ( );
if (s [i]=='\n')
【 】;
}
s[i]=【 】
printf(" display the string \n");
while (*p)
putchar (【 】);
}
第7题
功能是:把分数最高的学生数据放在h所指的数组中。注意:分数高的学生可能不只一个,函数返回分数最高学生的人数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <stdio.h>
define N 16
typedef struct
{ char num[10];
int s;
}STREC;
int fun(STREC *a,STREC *b)
{
}
main ()
{
STREC s [N] = { { "GA005", 85 }, { "GA003", 76 },
{ "GA002", 69 }, { "GA004", 85 }, { "GA001", 91 },
{"GA007", 72}, {"GA008", 64}, {"GA006", 87},
{ "GA015", 85), ( "GA013", 91 }, { "GA012", 64 },
{ "GA014", 91 ), { "GA011", 66}, { "GA017", 64 },
{"GA018", 64 }, {"GA016", 72 } };
STREC h[N];
int i, n;
FILE *out;
n=fun(s,h);
printf("The %d highest score :\n",n);
for (i=0; i<n; i++)
printf("%s %4d\n ",h[i].num, h[i].s);
/*输出最高分学生的学号和成绩* /
printf("\n");
ut=fopen("out45.dat", "w");
fprintf(out, "%d\n",n);
for(i=0; i<n; i++)
fprintf(out, "%4d\n ",h[i].s);
fclose(out);
}
第8题
回这些数的个数。
例如,若传给m的值为50,则程序输出:
7 11 14 21 X 28 33 35 42 44 49
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<conio.h>
include<stdio.h>
define M 100
void fun(int m, int *a, int *n)
{
}
main()
{
int aa[M],n,k;
clrscr();
fun(50,aa,&n);
for(k=0;k<n; k++)
if((k+1)%20==0) /*每行输出20个数*/
{printf("%4d",aa[k]);
printf("\n");
}
else
printf("%4d",aa[k]);
printf("\n");
}
第9题
下列给定程序中函数fun()的功能是计算1/n!的值。
例如:给n输入5,则输出0.0083330
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <stdio.h>
include <conio.h>
/******+****found*************/
int fun(int n)
{double result=1.0;
if(n==0)
return 1.0;
while(n>1 && n<170)
/**********found************/
result *=n++;
result=1/result;
return result;
}
main()
{
int n;
printf("Input N:");
scanf("%d",&n);
printf("\n1/%d!=%If\n",n,fun(n));
}
第10题
请补充main函数,该函数的功能是求方程ax2+bx+c=0的根(方程的系数a,b,c从键盘输入)。
例如, 当a=1,b=2,c=1时, 方程的两个根分别是:
x1=-1.00,x2=-1.00。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
include <conio.h>
include <math.h>
main()
{
float a,b,c,disc,x1,x2,p,q;
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
clrscr();
printf("****** the result ****+*+\n");
if(disc>=0)
{
x1=【 】;
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%6.2f,x2=%6.2f\n",x1,x2);
}
else
{
p=【 】;
q=【 】;
printf("x1=%6.2f+%6.2f i\n",p,q);
printf("x2=%6.2f-%6.2f i\n",p,q);
}
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!