_Phoebe__ 2022-01-06 01:04 采纳率: 96.9%
浏览 34
已结题

结构体问题 后面两个要求写不动了 想知道怎么写

本题使用求平均分函数、分数查找函数、二维数组及其指针方法完成。一个班有5个学生,4门课程:
① 求每门课程的平均分,并输出;
② 找出有两门以上课程不及格的学生,输出其学号、姓名和全部课程成绩及平均成绩;
③ 找出每门课程成绩均在85分以上的学生,输出其学号、姓名和全部课程成绩及平均成绩。
提示:用二维数组存放学生和课程,直接初始化二维数组以简化调试(即不使用scanf语句)。
学生成绩的测试数据如下:
stuNo name math eng phys chem
3001 Zhang 82 78 88 93
3002 Wang 46 68 62 57
3003 Li 92 86 85 87
3004 Sun 52 77 55 47
3005 Zhao 58 90 85 77

#include<stdio.h>
struct student{
    int num;
    char name[20];
    float score[4];
    float average;
    float total;
};
struct student s[]{
{3001,Zhang,82,78,88,93},
{3002,Wang,46,68,62,57},
{3003,Li,92,86,85,87},
{3004,Sun,52,77,55,47},
{3005,Zhao,58,90,85,77}
}; 
void average(struct student s[],int n){
    int i,j;
    float sum;
    for(i=0;i<5;i++){
        sum=0;
        for(j=0;j<4;j++){
            sum+=s[i].score[j];
        }
            s[i].total=sum;
            s[i].aver=sum/4;
        }
    } 

```

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-01-06 06:03
    关注
    #include <stdio.h>
    
    struct student
    {
        int num;
        char name[20];
        float score[4];
    };
    
    void compute_average_scores(const struct student *s, int n)
    {
        float total[4] = {};
        for (int i = 0; i < n; i++)
            for (int j = 0; j < 4; j++)
                total[j] += s[i].score[j];
        printf("Avg Math: %.1f\n", total[0] / n);
        printf("Avg Eng: %.1f\n", total[1] / n);
        printf("Avg Phys: %.1f\n", total[2] / n);
        printf("Avg Chem: %.1f\n", total[3] / n);
    }
    
    void print(const struct student *s)
    {
        printf("Student No: %d\n", s->num);
        printf("Student Name: %s\n", s->name);
        printf("Math: %g\n", s->score[0]);
        printf("Eng: %g\n", s->score[1]);
        printf("Phys: %g\n", s->score[2]);
        printf("Chem: %g\n", s->score[3]);
        printf("Average: %.1f\n", (s->score[0] + s->score[1] + s->score[2] + s->score[3]) / 4);
    }
    
    int main()
    {
        struct student students[] = {
            {3001, "Zhang", {82, 78, 88, 93}},
            {3002, "Wang", {46, 68, 62, 57}},
            {3003, "Li", {92, 86, 85, 87}},
            {3004, "Sun", {52, 77, 55, 47}},
            {3005, "Zhao", {58, 90, 85, 77}}};
        int n = sizeof(students) / sizeof(struct student);
    
        compute_average_scores(students, n);
    
        printf("Students with at least two scores < 60:\n");
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            for (int j = 0; j < 4; j++)
                if (students[i].score[j] < 60)
                    count++;
            if (count >= 2)
                print(&students[i]);
        }
    
        printf("Students with all scores >= 85:\n");
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            for (int j = 0; j < 4; j++)
                if (students[i].score[j] >= 85)
                    count++;
            if (count == 4)
                print(&students[i]);
        }
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月14日
  • 已采纳回答 1月6日
  • 创建了问题 1月6日

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?