C语言求五个人三门课的平均成绩
时间: 2024-10-23 19:04:54 浏览: 19
在C语言中,我们可以创建一个结构体来存储每个人的成绩,然后计算五个人三门课程的总成绩和平均分。这里是一个简单的示例:
```c
#include <stdio.h>
// 定义学生结构体
typedef struct {
char name[50];
int math_score;
int science_score;
int english_score;
} Student;
// 求单科平均分函数
double calculate_average(int score) {
return (double)score / 100; // 假设满分是100分
}
int main() {
const int num_students = 5;
const int num_courses = 3;
Student students[num_students];
for (int i = 0; i < num_students; i++) {
printf("请输入第%d位学生的姓名、数学、科学和英语成绩:\n", i + 1);
scanf("%s %d %d %d", students[i].name, &students[i].math_score, &students[i].science_score, &students[i].english_score);
}
double total_scores[num_courses] = {0}; // 存储各科总分
for (int j = 0; j < num_courses; j++) {
for (int k = 0; k < num_students; k++) {
total_scores[j] += students[k].scores[j]; // 提供了一个scores数组作为实际分数访问
}
}
double overall_average = 0;
for (int l = 0; l < num_courses; l++) {
overall_average += calculate_average(total_scores[l]);
}
overall_average /= num_courses;
printf("五个人三门课程的平均成绩是: %.2f\n", overall_average);
return 0;
}
```
阅读全文