填空题
1. 下列给定程序中,函数proc()的功能是:对M名学生的学习成绩,按从高到低的顺序找出前m(m≤10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#define M 10
typedef struct ss
{
char num[10];
int order;
}
STU;
STU *proc(STU a[],int m)
{
STU b[M],*tt;
int i,j,k;
______;
for(i=0; i<M; i++)
b[i]=a[i];
for(k=0; k<m; k++)
{
for(i=j=0; i<M; i++)
if(______)
j=i;
tt[k]=b[j];
b[j].order=0;
}
return ______;
}
void outresult(STU a[],FILE*pf)
{
int i;
for(i=0; i<M; i++)
printf(pf,"No=%s Mark=%d\n",
a[i].num,a[i].order);
fprintf(pf,"\n\n");
}
void main()
{
STU stu[M]={{"A01",60},{"A02",90},{"A03",88},{"A04",76},{"A05",63},{"A06",69},{"A07",100},{"A08",87},{"A09",66},{"A10",95}};
STU*porder;
int i,m;
system("CLS");
printf("***The Origial data***\n");
outresult(stu,stdout);
printf("\nGive the numeber of the students who have better score:");
scanf("%d",&m);
while(m>10)
{
printf("\nGive the number of the studets who have better score:");
scanf("%d",&m);
}
porder=proc(stu,m);
printf("***THE RESULT***\n");
printf("***The top students***\n");
for(i=0; i<m; i++)
printf("%s%d\n",
porder[i].num,porder[i].order);
free(porder);
}
【正确答案】
1、tt=(STU*)malloc(sizeof(STU)* m)
b[i].order>b[j].order
tt
【答案解析】 由函数proc()可知,变量tt用来存放前m名学生的数据,首先,要为其分配内存空间,因此,空一处填“tt=(STU*)malloc(sizeof(STU)* m)”;依次找出成绩最高的前m名学生的记录,因此,空二处填“b[i].order>b[j].order”;前m名学生的数据存放在tt指向的存储区中,最后,返回给主函数,因此,空三处填“tt”。