一道c语言的题,我初学想有个参考

linchen9 2007-09-15 10:56:14
已知学生的记录由学号和学习成绩构成,n名学生的数据已存入a结构体数组中.请编写函数fun,函数功能是:找出成绩最低和最高的学生记录及求出学生的平均成绩.提示:#define N 100
struct student
{char stu No;
float stu score;
}a[N]
...全文
308 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxg2006 2007-09-20
  • 打赏
  • 举报
回复
二楼的~!你的程序太扛了·!!
只是用的这样么~!?~!?
zxg2006 2007-09-20
  • 打赏
  • 举报
回复
void fun(struct stu a)
{int i,j,h=0,l=0;
float hs=a[0].stuscore,sum=a[0].stuscore,aver,ls=a[0].stuscore
for(i=1;i<N;i++)
{ sum=sum+a[i].stuscore;
aver=sum/N;
if(a[i].stuscore>hs)h=i;
if(a[i].stuscore<ls)l=i;
}
printf("the data of the student a[%d] with highest score:%s,%f\n",h,a[h].stuno,a[h].stuscore);
printf("the data of the student a[%d] with lowest score:%s,%f,%f\n",l,a[l].stuno,a[l].stuscore);
printf("the average of the class:%f\n",aver);
}
zxg2006 2007-09-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#define N 3
struct stu
{
char stuno[10];
float stuscore;
}a[N]={{"20044590",69.53},{"20044591",70.56},{"20044594",65.90}};
main()
{int n,m;
int i,j,h=0,l=0;
float hs=a[0].stuscore,sum=a[0].stuscore,aver,ls=a[0].stuscore;
clrscr();
printf("the normal data of students:");
printf("\n");
printf(" number score\n");
printf("student 0 data: %s %f\n",a[0].stuno,a[0].stuscore);
for(i=1;i<N;i++)
{ sum=sum+a[i].stuscore;
aver=sum/N;
printf("student %d data: %s %f\n",i,a[i].stuno,a[i].stuscore);
if(a[i].stuscore>hs)h=i;
if(a[i].stuscore<ls)l=i;
}
printf("the data of the student a[%d] with highest score:%s,%f\n",h,a[h].stuno,a[h].stuscore);
printf("the data of the student a[%d] with lowest score:%s,%f,%f\n",l,a[l].stuno,a[l].stuscore);
printf("the average of the class:%f\n",aver);
getch();
return 0;
}
乱编的·!大家帮忙看看哪些地方用得不好的·!编译能通过并且基本功能能实现就是了~!
Gavin001 2007-09-18
  • 打赏
  • 举报
回复
有些学号里面有字母,或者是0打头的,用int可能不行
我看你有戏 2007-09-18
  • 打赏
  • 举报
回复
char stu No;学号还是用int号一点
我看你有戏 2007-09-17
  • 打赏
  • 举报
回复
struct student
{char stu No;
float stu score;
}a[N]

这个结构有问题
SUN116688 2007-09-17
  • 打赏
  • 举报
回复
二楼的 int i=0; flaot sum=0
flaot应该是float 后面要加分号
yunzhe 2007-09-16
  • 打赏
  • 举报
回复
void fun(struct student *a,int *min,int *max,float *avr)
{
int i=0;flaot sum=0
*max=*min=0;
for(i=0;i<N;++i)
{
sum+=a[i].score;
if(a[i].score<a[*min].score)*min=i;
else if (a[i].score>a[*max].score])*max=i;
}
*vr=sum/N;

}
qybao 2007-09-15
  • 打赏
  • 举报
回复
仅当参考

fun() {
float minScore = a[0].stuscore;
float maxScore = a[0].stuscore;
float sum = a[0].stuscore;
int minIndex[N];
int maxIndex[N];
int minCount = 0;
int maxCount = 0;
minIndex[minCount] = 0;
maxIndex[maxCount] = 0;

for(int i=1; i<N; i++) {
sum += a[i].stuscore;

if (a[i].stuscore<minScore) {
minScore = a[i].stuscore;
minCount = 0;
minIndex[minCount] = i;
} else if (a[i].stuscore == minScore) {
minCount++;
minIndex[minCount] = i;
}

if (a[i].stuscore>maxScore) {
maxScore = a[i].stuscore;
maxCount = 0;
maxIndex[maxCount] = i;
} else if (a[i].stuscore == maxScore) {
maxCount++;
maxIndex[maxCount] = i;
}
}

printf("最低分:%f\n", minScore);
printf("成绩最低的学生有:\n");
for (int i=0; i<=minCount; i++) {
printf("学号:%c, 成绩:%f\n", a[minIndex[i]].stuNo, a[minIndex[i]].stuscore);
}

printf("最高分:%f\n", maxScore);
printf("成绩最高的学生有:\n");
for (int i=0; i<=maxCount; i++) {
printf("学号:%c, 成绩:%f\n", a[maxIndex[i]].stuNo, a[maxIndex[i]].stuscore);
}

printf("平均分:%f\n", sum/N);

}

33,319

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧