一、从键盘输入一个班学生一门课程的成绩(每班最多不超过30人,具体人数从键盘输入),试用函数编程实现输出最高分及学好。
编写的程序如下:
#include<stdio.h>

#defineN30
void find(float score[],long num[],int n,float p_maxscore,long p_maxnum);//声明函数
int main()
{
float score[N],Maxscore;
int n,i;
long num[N],Maxnum;
printf(\"请从键盘上输入学生的人数:\");
scanf(\"%d\",&n);
printf(\"请别以输入学生的学号和成绩:\n\");
for(i=0;i<n;i++)
{
scanf(\"%ld%f\",&num[i],&score[i]);
}
find(score,num,n,&Maxscore,&Maxnum);
printf(\"学号为%ld,最高分为%.0f\",Maxnum,Maxscore);
return 0;
}
//函数的功能:
//计算实型数组score中存储的n个学生成绩的最高分及其所在的学号
//指针变量p_maxscore,指向存储最高分的实型变量
//指针变量p_maxnum,指向存储最高分所在学号的长整型
void find(float score[],long num[],int n,float p_maxscore,long p_maxnum)
{
int i;
p_maxscore=score[0];
p_maxnum=num[0];
for(i=0;i<n;i++)
{
if(score[i]>p_maxscore)
{
p_maxscore=score[i];
p_maxnum=num[i];
}
}
}
程序运行的结果如下:
请从键盘上输入学生的人数:3(按回车)
请别以输入学生的学号和成绩:
201901 89(按回车)
201902 86(按回车)
201903 98(按回车)
学号为201903,最高分为98