编程题:
61.请编写函数fun,该函数的功能是:统计各年龄段的人数。N个年龄通过调用随机函数获得,并放在主函数的age数组中;要求函数把0至9岁年龄段的人数放在d[0]中,把10至19岁年龄段的人数放在d[1]中,把20至29岁年龄段的人数放在d[2]中,其余依此类推,把100岁(含100)以上年龄的人数都放在d[10]中。结果在主函数中输出。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
#define N 50
#define M 11
void fun(int *a, int *b)
{
}
double rnd()
{
static t=29, c=217, m=1024, r=0;
r=(r*t+c)%m;
return((double)r/m);
}
main()
{
int age[N], i, d[M];
for(i=0; i<N; i++)
age[i]=(int)(115*rnd());
printf(“The original data :n”);
for(i=0; i<N; i++)
printf((i+1)%10==0?”%4dn”:”%4d”,age[i]);
printf(“nn”);
fun( age, d);
for(i=0;i<10;i++)printf(“%4d—%4d : %4dn”, i*10, i*10+9, d[i]);
printf(” Over 100 : %4dn”, d[10]);
}
86.请编写函数fun,其功能是:计算并输出给定10个数的方差:
其中:
例如,给定的10个数为95.0、89.0、76.0、65.0、88.0、72.0、85.0、81.0、90.0、56.0,则输出为S=11.730729。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
#include <math.h>
double fun(double x[10])
{
}
main()
{
doule s, x[10]={95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0};
int i;
printf(“nThe original data is :n”);
for(i=0;i<10;i++)
printf(“%6.1f”,x[i]);
printf(“nn”);
s=fun(x);
printf(“s=%fnn”,s);
}
改错题:
49.下面给定程序中,函数fun的功能是:对N名学生的学习成绩,从高到低的顺序找出前m(m≤0)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdio.h>
#include<alloc.h>
#include<string.h>
#include<conio.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=malloc(sizeof(STU));
for(i=0; i<N; i++)
b[i]=a[i];
for(k=0; k<m; k++)
{
for(i=j=0; i<N; i++)
if(b[i].s > b[j].s)
j=i;
/********found********/
t[k].num=b[j].num;
t[k].s=b[j].s;
b[j].s=0;
}
return t;
}
outresult(STU a[], FILE *pf)
{
int i;
for(i=0; i<N; i++)
fprintf(pf,”No=%s Mark=%dn”,a[i].num,a[i].s);
fprintf(pf,”nn”);
}
main()
{
STU a[N]={ {“A01”,81},{“A02”,89},{“A03”,66},{“A04”,87},{“A05”,77},
{“A06”,90},{“A07”,79},{“A08”,61},{“A09”,80},{“A10”,71}};
STU *pOrder;
int i, m;
clrscr();
printf(“***** The Original data *****n”);
outresult(a, stdout);
printf(“nGive the number of the students who have better score: “);
scanf(“%d”,&m);
while(m>10)
{
printf(“nGinve the number of the students who have better score: “);
scanf(“%d”,&m);
}
pOrder=fun(a,m);
printf(“***** THE RESULT *****n”);
printf(“The top :n”);
for(i=0; i<m; i++)
printf(” %s %dn”,pOrder[i].num,pOrder[i].s);
free(pOrder);
}
55.下列给定程序中,函数fun的功能是:为一个偶数寻找两个素数,这两个素数之和等于该偶数,并将这两个素数通过形参指针传回主函数。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include <stdio.h>
#include <math.h>
void fun(int a,int *b,int *c)
{
int i,j,d,y;
for(i=3;i<a/2;i=i+2)
{
/********found********/
y=0;
for(j=2;j<=sqrt((double)i);j++)
if(i%j==0)
y=0;
if(y==1)
{
/********found********/
d=i-a;
for(j=2;j<=sqrt((double)i);j++)
if(d%j==0)
y=0;
if(y==1)
{
*b=i;
*c=d;
}
}
}
}
main()
{
int a,b,c;
do
{
printf(“nInput a: “);
scanf(“%d”,&a);
}
while(a%2);
fun(a,&b,&c);
printf(“nn%d=%d + %dn”,a,b,c);
}