每日一题——第七十九题

输入十个学生的5门成绩,分别用函数实现下列功能

①计算每位学生的平均分 ②计算每门课的平均分 ③找出50个分数中最高的分数所对应的学生和课程

#include <stdio.h>  

#define STUDENTS 10  
#define SUBJECTS 5  

// 定义学生结构体  
typedef struct {
    int scores[SUBJECTS];
    float average;
} Student;

// 计算每个学生的平均分  
void calculateStudentAverage(Student students[], int numStudents) {
    for (int i = 0; i < numStudents; i++) {
        float sum = 0;
        for (int j = 0; j < SUBJECTS; j++) {
            sum += students[i].scores[j];
        }
        students[i].average = sum / SUBJECTS;
    }
}

// 计算每门课的平均分  
void calculateSubjectAverage(Student students[], int numStudents, float subjectAverages[]) {
    for (int j = 0; j < SUBJECTS; j++) {
        float sum = 0;
        for (int i = 0; i < numStudents; i++) {
            sum += students[i].scores[j];
        }
        subjectAverages[j] = sum / numStudents;
    }
}

// 找出50个分数中最高的分数所对应的学生和课程  
void findHighestScore(Student students[], int numStudents, int* studentIndex, int* subjectIndex) {
    int maxIndex = 0, maxSubjectIndex = 0;
    int maxScore = students[0].scores[0];

    for (int i = 0; i < numStudents; i++) {
        for (int j = 0; j < SUBJECTS; j++) {
            if (students[i].scores[j] > maxScore) {
                maxScore = students[i].scores[j];
                maxIndex = i;
                maxSubjectIndex = j;
            }
        }
    }

    *studentIndex = maxIndex;
    *subjectIndex = maxSubjectIndex;
}

int main() {
    Student students[STUDENTS];
    float subjectAverages[SUBJECTS];
    int highestStudentIndex, highestSubjectIndex;

    // 用户输入每个学生的成绩  
    printf("请输入%d个学生的%d门成绩:\n", STUDENTS, SUBJECTS);
    for (int i = 0; i < STUDENTS; i++) {
        printf("学生%d的成绩:", i + 1);
        for (int j = 0; j < SUBJECTS; j++) {
            scanf("%d", &students[i].scores[j]);
        }
    }

    // 计算每个学生的平均分  
    calculateStudentAverage(students, STUDENTS);

    // 计算每门课的平均分  
    calculateSubjectAverage(students, STUDENTS, subjectAverages);

    // 找出最高的分数及其对应的学生和课程  
    findHighestScore(students, STUDENTS, &highestStudentIndex, &highestSubjectIndex);

    // 输出结果  
    printf("学生平均分:\n");
    for (int i = 0; i < STUDENTS; i++) {
        printf("学生%d: %.2f\n", i + 1, students[i].average);
    }

    printf("科目平均分:\n");
    for (int j = 0; j < SUBJECTS; j++) {
        printf("科目%d: %.2f\n", j + 1, subjectAverages[j]);
    }

    printf("最高分:%d(学生%d,科目%d)\n", students[highestStudentIndex].scores[highestSubjectIndex], highestStudentIndex + 1, highestSubjectIndex + 1);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值