c++结构体输出5个学生平均成绩和3科平均成绩以及平均分最高的学生数据 编译不通过 不知道哪里错了 想知道怎么改


#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();
}

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();
}

img

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632