用c语言写:输人10个学生5门课的成绩,分别用函数实现下列功能: ①计算每个学生的平均分; ②计算每门课的平均分; ③找出所有50个分数中最高的分数所对应的学生和课程; ④计算平均分方差: , 其中,xi为某一学生的平均分。
时间: 2023-04-08 11:05:01 浏览: 202
C语言程序设计-有一个一维数组score,内放10个学生的成绩,用一个函数来求平均成绩;
5星 · 资源好评率100%
可以回答这个问题。以下是代码示例:
#include <stdio.h>
#define STUDENTS 10
#define COURSES 5
void input_scores(float scores[][COURSES]);
void average_per_student(float scores[][COURSES], float averages[]);
void average_per_course(float scores[][COURSES], float averages[]);
void highest_score(float scores[][COURSES], int *student, int *course);
float variance(float averages[], float mean);
int main(void)
{
float scores[STUDENTS][COURSES];
float student_averages[STUDENTS];
float course_averages[COURSES];
int highest_student, highest_course;
float mean, var;
input_scores(scores);
average_per_student(scores, student_averages);
average_per_course(scores, course_averages);
highest_score(scores, &highest_student, &highest_course);
mean = course_averages[0];
var = variance(student_averages, mean);
printf("每个学生的平均分:\n");
for (int i = 0; i < STUDENTS; i++) {
printf("学生%d:%.2f\n", i + 1, student_averages[i]);
}
printf("\n");
printf("每门课的平均分:\n");
for (int i = 0; i < COURSES; i++) {
printf("课程%d:%.2f\n", i + 1, course_averages[i]);
}
printf("\n");
printf("最高分数所对应的学生和课程:\n");
printf("学生%d,课程%d,分数%.2f\n", highest_student + 1, highest_course + 1, scores[highest_student][highest_course]);
printf("\n");
printf("平均分方差:%.2f\n", var);
return 0;
}
void input_scores(float scores[][COURSES])
{
for (int i = 0; i < STUDENTS; i++) {
printf("请输入学生%d的成绩:\n", i + 1);
for (int j = 0; j < COURSES; j++) {
scanf("%f", &scores[i][j]);
}
}
}
void average_per_student(float scores[][COURSES], float averages[])
{
for (int i = 0; i < STUDENTS; i++) {
float sum = 0;
for (int j = 0; j < COURSES; j++) {
sum += scores[i][j];
}
averages[i] = sum / COURSES;
}
}
void average_per_course(float scores[][COURSES], float averages[])
{
for (int i = 0; i < COURSES; i++) {
float sum = 0;
for (int j = 0; j < STUDENTS; j++) {
sum += scores[j][i];
}
averages[i] = sum / STUDENTS;
}
}
void highest_score(float scores[][COURSES], int *student, int *course)
{
float highest = scores[0][0];
for (int i = 0; i < STUDENTS; i++) {
for (int j = 0; j < COURSES; j++) {
if (scores[i][j] > highest) {
highest = scores[i][j];
*student = i;
*course = j;
}
}
}
}
float variance(float averages[], float mean)
{
float sum = 0;
for (int i = 0; i < STUDENTS; i++) {
sum += (averages[i] - mean) * (averages[i] - mean);
}
return sum / STUDENTS;
}
阅读全文