编程题:
89.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把高于等于平均分的学生数据放在b所指的数组中,高于等于平均分的学生人数通过形参n传回,平均分通过函数值返回。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
#define N 12
typedef struct
{
char num [10];
double s;
}STREC;
double fun ( STREC *a, STREC *b, int *n )
{
}
main ()
{
STREC s[N]={{“GA05”,85}, {“GA03”,76}, {“GA02”,69}, {“GA04”,85},
{“GA01”,91}, {“GA07”,72}, {“GA08”,64}, {“GA06”,87},
{“GA09”,60}, {“GA11”,79}, {“GA12”,73}, {“GA10”,90}};
STREC h [N], t;
FILE *out ;
int i,j,n;
double ave;
ave=fun ( s, h, &n );
printf ( “The %d student data which is higher than %7.3f:n”, n, ave);
for ( i=0; i<n; i++ )
printf (“%s %4.1fn”, h[i]. num, h[i]. s);
printf (“n”);
out=fopen(“out12.dat”,”w”);
fprintf(out,”%dn%7.3fn”, n, ave);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(h[i].s<h[j].s)
{
t=h[i] ;
h[i]=h[j];
h[j]=t;
}
for(i=0;i<n; i++)
fprintf(out, “%4.1fn”,h[i].s);
fclose(out);
}
90.请编写函数fun,其功能是:计算并输出下列多项式值:
Sn= (1-1/2) + (1/3-1/4)+…+(1/2n-1-1/2n)
例如,若主函数从键盘给n输入8后,则输出为S=0.662872。
注意:n的值要求大于1但不大于100。部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
double fun(int n)
{
}
main()
{
int n;
double s;
printf(“nInput n: “);
scanf(“%d”,&n);
s=fun(n);
printf(“ns=%fn”,s);
}
改错题:
12.下列给定程序中函数fun的功能是:求出在字符串中最后一次出现的子字符串的地
址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为NULL。
例如,当字符串中的内容为abcdabfabcdx,t中的内容为ab时,输出结果应是:abcdx。当字符串中的内容为abcdabfabcdx,t中的内容为abd时,则程序输出未找到信息:not be found!。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <conio.h>
#include <stdio.h>
#include <string.h>
char *fun(char *s,char *t)
{
char *p,*r,*a;
a=NuLL;
while(*s)
{
p=s;r=t;
while(*r)
if(r==p)
{
r++;
p++;
}
else
break;
if(*r==’’)
a=s;
s++;
}
return a;
}
main()
{
char s[100],t[100],*p;
clrscr();
printf(“nplease enter string s:”);
scanf(“%s”,s);
printf(“nplease enter substring t:”);
scanf(“%s”,t);
p=fun(s,t);
if(p)
printf(“nthe result is:%sn”,p);
else
printf(“nnot found!n”);
}
20.N个有序整数数列已放在一维数组中,给定下列程序中,函数fun的功能是:利用折半查找算法查找整数m在数组中的位置。若找到,则返回其下标值;反之,则返回-1。
折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low<high),然后把m与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下一次的查找范围放在中间位置之后的元素中;反之,下一次的查找范围落在中间位置之前的元素中。直到low>high,查找结束。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <stdio.h>
#define N 10
void fun(int a[],int m)
{
int low=0,high=N-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(m<a[mid])
high=mid-1;
else
if(m>=a[mid])
low=mid+1;
else
return(mid);
}
return (-1);
}
main()
{
int i,a[N]={-3,4,7,9,13,45,67,89,100,180},k,m;
printf(“a:”);
for(i=0;i<N;i++)
printf(“%d “,a[i]);
printf(“Enter m:”);
scanf(“%d”,&m);
k=fun(a,m);
if(k>=0)
printf(“m=%d,index=%dn”,m,k);
else
printf(“Not be found!n”);
}



