#include<bits/stdc++.h>
using namespace std;
struct student{
char name[20];
double score[3];
double stuavg[5];
}stu[5];
void input(){
for(int i=0;i<5;i++){
cout<<"please input the student's name"<<endl;
cin>>stu[i].name;
cout<<"please input the student's scores for the three cources"<<endl;
cin>>stu[i].score[0]>>stu[i].score[1]>>stu[i].score[2];
}
}
void cal1(struct student *stu){//计算每个学生的平均分
for(int i=0;i<5;i++){
stu[i].stuavg=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
printf("NO %d 's avrage score is %lf",i+1,stu[i].stuavg);
}
}
void cal2(struct student *stu){//计算每门课的平均分
double courseavg[3];
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
courseavg[i]=stu[j].score[0];
courseavg[i]/=5.0;
}
printf("The average score of this course is %lld",courseavg[i]);
}
}
void beststu(){
int i;
for(i=0;i<4;i++){
if(stu[i].stuavg<stu[i++].stuavg){
stu[i].stuavg=stu[i++].stuavg;
int k;
i=k;
}
}
cout<<stu[i].name<<endl;
cout<<stu[i].score[0]<<endl;
cout<<stu[i].score[1]<<endl;
cout<<stu[i].score[2]<<endl;
}
int main(){
input();
cal1();
cal2();
beststu();
}

c++结构体输出5个学生平均成绩和3科平均成绩以及平均分最高的学生数据 编译不通过 不知道哪里错了 想知道怎么改
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-天际的海浪 2022-02-24 22:29关注
stu是全局变量, cal1()和cal2()不需要struct student *stu参数
double stuavg; //去掉[5]
计算每门课的平均分和平均分最高的学生数据也不对
你题目的解答代码如下:
#include <bits/stdc++.h> using namespace std; struct student { char name[20]; double score[3]; double stuavg; //去掉[5] } stu[5]; void input() { for (int i = 0; i < 5; i++) { cout << "please input the student's name" << endl; cin >> stu[i].name; cout << "please input the student's scores for the three cources" << endl; cin >> stu[i].score[0] >> stu[i].score[1] >> stu[i].score[2]; } } void cal1() //去掉struct student *stu { //计算每个学生的平均分 for (int i = 0; i < 5; i++) { stu[i].stuavg = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0; printf("NO %d 's avrage score is %lf\n", i + 1, stu[i].stuavg); } } void cal2() //去掉struct student *stu { //计算每门课的平均分 double courseavg[3]; for (int i = 0; i < 3; i++) { courseavg[i] = 0; for (int j = 0; j < 5; j++) { courseavg[i] += stu[j].score[i]; } courseavg[i] /= 5.0; printf("The average score of this course is %lf\n", courseavg[i]); } } void beststu() { int i,k=0; for (i = 0; i < 5; i++) { if (stu[k].stuavg < stu[i].stuavg) { k = i; } } cout << stu[k].name << endl; cout << stu[k].score[0] << endl; cout << stu[k].score[1] << endl; cout << stu[k].score[2] << endl; } int main() { input(); cal1(); cal2(); beststu(); }
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥65 页面调接口时加载卡住不响应
- ¥35 用C语言解决编程问题
- ¥15 unity硬件连接与使用
- ¥15 鱼缸加热棒的数据分析或者实际案例也行
- ¥15 postgresql11安装完成后,pgadmin无法启动
- ¥15 (标签-无人机|关键词-Matlab代码)
- ¥15 执行shell脚本提示参数太多
- ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
- ¥15 求减少这个c++程序内存的解决方案
- ¥30 改正代码,错误比较多