_Phoebe__ 2022-02-24 22:13 采纳率: 96.9%
浏览 54
已结题

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

  • 写回答

2条回答 默认 最新

  • 关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • 有问必答小助手 2022-02-24 23:13
    关注
    您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
    PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月4日
  • 已采纳回答 2月24日
  • 创建了问题 2月24日

悬赏问题

  • ¥65 页面调接口时加载卡住不响应
  • ¥35 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多
  • ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
  • ¥15 求减少这个c++程序内存的解决方案
  • ¥30 改正代码,错误比较多