用C语言编程,输入10个学生5门课的成绩,用函数计算每个学生的平均分,每门课的平均分,找出最高法对应的学生和课程,,计算平均分方差
时间: 2024-01-14 13:03:06 浏览: 26
好的,我可以为您解答这道题目。
以下是C语言代码实现:
```c
#include <stdio.h>
#include <math.h>
#define STUDENTS 10
#define COURSES 5
void calculate_averages(float scores[][COURSES], float student_averages[], float course_averages[]) {
for(int i=0; i<STUDENTS; i++) {
float sum = 0;
for(int j=0; j<COURSES; j++) {
sum += scores[i][j];
course_averages[j] += scores[i][j];
}
student_averages[i] = sum / COURSES;
}
for(int j=0; j<COURSES; j++) {
course_averages[j] /= STUDENTS;
}
}
void find_highest_score(float scores[][COURSES], int *max_student, int *max_course) {
float max_score = 0;
for(int i=0; i<STUDENTS; i++) {
for(int j=0; j<COURSES; j++) {
if(scores[i][j] > max_score) {
max_score = scores[i][j];
*max_student = i;
*max_course = j;
}
}
}
}
float calculate_variance(float data[], float mean) {
float variance = 0;
for(int i=0; i<STUDENTS; i++) {
variance += pow(data[i] - mean, 2);
}
variance /= STUDENTS;
return variance;
}
int main() {
float scores[STUDENTS][COURSES];
float student_averages[STUDENTS] = {0};
float course_averages[COURSES] = {0};
// 输入10个学生5门课的成绩
printf("请输入10个学生的5门课的成绩:\n");
for(int i=0; i<STUDENTS; i++) {
printf("请输入第%d个学生的成绩:", i+1);
for(int j=0; j<COURSES; j++) {
scanf("%f", &scores[i][j]);
}
}
// 计算每个学生的平均分,每门课的平均分
calculate_averages(scores, student_averages, course_averages);
printf("每个学生的平均分:\n");
for(int i=0; i<STUDENTS; i++) {
printf("学生%d的平均分为:%.2f\n", i+1, student_averages[i]);
}
printf("每门课的平均分:\n");
for(int j=0; j<COURSES; j++) {
printf("第%d门课的平均分为:%.2f\n", j+1, course_averages[j]);
}
// 找出最高分对应的学生和课程
int max_student, max_course;
find_highest_score(scores, &max_student, &max_course);
printf("最高分为:%.2f,对应学生为%d,对应课程为%d\n", scores[max_student][max_course], max_student+1, max_course+1);
// 计算平均分方差
float student_variance = calculate_variance(student_averages, calculate_averages(student_averages, student_averages+STUDENTS)/STUDENTS);
float course_variance = calculate_variance(course_averages, calculate_averages(course_averages, course_averages+COURSES)/COURSES);
printf("每个学生平均分的方差为:%.2f\n", student_variance);
printf("每门课平均分的方差为:%.2f\n", course_variance);
return 0;
}
```
希望能够帮助到您!