结构体例题—打印平均成绩,以及最高分

题目:有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入 10个学生数据,要求打印出3门课总平均成绩,以及最高分的学生的数据(包括 学号、姓名、3门课的成绩、平均分数), 要求用 input函数输入10个学生数据;用 average函数求总平均分;用max函 数找出最高分的学生数据;总平均分和最高分学生的数据都在主函数中输出。

#include <stdio.h>
#include <string.h>

struct Student {
    int number;
    char name[20];
    int score[3];
    float average;
};

void input(struct Student *stu) {
    printf("请输入学生的学号、姓名和3门课的成绩:\n");
    scanf("%d %s %d %d %d", &stu->number, stu->name, &stu->score[0], &stu->score[1], &stu->score[2]);
    stu->average = (stu->score[0] + stu->score[1] + stu->score[2]) / 3.0;
}

float average(struct Student stu[], int n) {
    float sum = 0;
    for (int i = 0; i < n; i++) {
        sum += stu[i].average;
    }
    return sum / n;
}

void max(struct Student stu[], int n, struct Student *max_stu) {
    *max_stu = stu[0];
    for (int i = 1; i < n; i++) {
        if (stu[i].average > max_stu->average) {
            *max_stu = stu[i];
        }
    }
}

int main() {
    struct Student stu[10];
    struct Student max_stu;
    float avg;
    for (int i = 0; i < 10; i++) {
        input(&stu[i]);
    }
    avg = average(stu, 10);
    max(stu, 10, &max_stu);
    printf("3门课的总平均成绩为:%.2f\n", avg);
    printf("平均成绩最高的学生的数据为:\n");
    printf("学号:%d 姓名:%s 成绩:%d %d %d 平均分数:%.2f\n", max_stu.number, max_stu.name, max_stu.score[0], max_stu.score[1], max_stu.score[2], max_stu.average);
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值