统计分析代码

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int id; // 学生ID
    char name[50]; // 学生姓名
    float scores[SUBJECT_COUNT]; // 科目成绩数组,SUBJECT_COUNT是科目数量
} Student;

// 计算及格人数
int calculatePassCount(Student students[], int n, float passingGrade) {
    int passCount = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < SUBJECT_COUNT; ++j) {
            if (students[i].scores[j] >= passingGrade) {
                passCount++;
                break; // 只要一门课及格就认为该学生及格
            }
        }
    }
    return passCount;
}

// 统计分析函数
void analyze_scores(Student students[], int n, float passingGrade) {
    float total_grade = 0.0f;
    float max_score = students[0].scores[0];
    float min_score = students[0].scores[0];
    int pass_count = 0;

    // 计算总分、最高分、最低分
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < SUBJECT_COUNT; ++j) {
            total_grade += students[i].scores[j];
            if (students[i].scores[j] > max_score) {
                max_score = students[i].scores[j];
            }
            if (students[i].scores[j] < min_score && students[i].scores[j] != 0.0f) {
                min_score = students[i].scores[j];
            }
        }
        // 按照每名学生至少有一门课及格来计算及格率
        if (calculatePassCount(&students[i], 1, passingGrade) > 0) {
            pass_count++;
        }
    }

    float average_score = total_grade / (n * SUBJECT_COUNT);
    float pass_rate = (float) pass_count / n * 100.0f;

    printf("总分: %.2f\n", total_grade);
    printf("平均分: %.2f\n", average_score);
    printf("最高分: %.2f\n", max_score);
    printf("最低分: %.2f\n", min_score);
    printf("及格率: %.2f%%\n", pass_rate);
}

int main() {
    const int STUDENT_COUNT = 100; // 假设有100名学生
    const int SUBJECT_COUNT = 5; // 假设有5门科目
    const float PASSING_GRADE = 60.0f; // 假设及格线为60分

    // 初始化学生数据...

    analyze_scores(students, STUDENT_COUNT, PASSING_GRADE);

    return 0;
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值