请用c语言实现以下题目: 分别用函数和数组实现,输入10个学生5门课的成绩并完成如下 (1)求每个学生的平均分。 (2)求每门课程的平均分。 (3)输出一张包括10个学生成绩的成绩单。 (4)找出最高分数所对应的学生(学号)和课程。 (5)分别统计5门课程都高于90分的学生
时间: 2024-05-20 09:17:34 浏览: 101
#include <stdio.h>
#define STUDENT_NUM 10
#define COURSE_NUM 5
void get_scores(float scores[][COURSE_NUM]);
void get_student_avg(float scores[][COURSE_NUM], float student_avg[]);
void get_course_avg(float scores[][COURSE_NUM], float course_avg[]);
int get_max_score(float scores[][COURSE_NUM], int *student_num, int *course_num);
void print_report(float scores[][COURSE_NUM], float student_avg[], float course_avg);
int main(void) {
float scores[STUDENT_NUM][COURSE_NUM];
float student_avg[STUDENT_NUM];
float course_avg[COURSE_NUM];
int max_score_student_num, max_score_course_num;
get_scores(scores);
get_student_avg(scores, student_avg);
get_course_avg(scores, course_avg);
print_report(scores, student_avg, course_avg);
get_max_score(scores, &max_score_student_num, &max_score_course_num);
printf("The highest score is %f, it belongs to student %d and course %d.\n",
scores[max_score_student_num][max_score_course_num], max_score_student_num+1, max_score_course_num+1);
int high_score_count[COURSE_NUM] = {0};
for (int i = 0; i < STUDENT_NUM; i++) {
int count = 0;
for (int j = 0; j < COURSE_NUM; j++) {
if (scores[i][j] > 90.0) {
count++;
}
}
if (count == COURSE_NUM) {
high_score_count[COURSE_NUM]++;
}
}
printf("%d students get over 90 points in all courses.\n", high_score_count[COURSE_NUM]);
return 0;
}
void get_scores(float scores[][COURSE_NUM]) {
printf("Please input 10 students' scores for 5 courses:\n");
for (int i = 0; i < STUDENT_NUM; i++) {
printf("Student %d: ", i+1);
for (int j = 0; j < COURSE_NUM; j++) {
scanf("%f", &scores[i][j]);
}
}
}
void get_student_avg(float scores[][COURSE_NUM], float student_avg[]) {
for (int i = 0; i < STUDENT_NUM; i++) {
float sum = 0.0;
for (int j = 0; j < COURSE_NUM; j++) {
sum += scores[i][j];
}
student_avg[i] = sum / COURSE_NUM;
}
}
void get_course_avg(float scores[][COURSE_NUM], float course_avg[]) {
for (int i = 0; i < COURSE_NUM; i++) {
float sum = 0.0;
for (int j = 0; j < STUDENT_NUM; j++) {
sum += scores[j][i];
}
course_avg[i] = sum / STUDENT_NUM;
}
}
int get_max_score(float scores[][COURSE_NUM], int *student_num, int *course_num) {
float max_score = scores[0][0];
for (int i = 0; i < STUDENT_NUM; i++) {
for (int j = 0; j < COURSE_NUM; j++) {
if (scores[i][j] > max_score) {
max_score = scores[i][j];
*student_num = i;
*course_num = j;
}
}
}
return max_score;
}
void print_report(float scores[][COURSE_NUM], float student_avg[], float course_avg[]) {
printf("Student scores:\n");
for (int i = 0; i < STUDENT_NUM; i++) {
printf("Student %d: ", i+1);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%.2f ", scores[i][j]);
}
printf("Average: %.2f\n", student_avg[i]);
}
printf("Course averages:\n");
for (int i = 0; i < COURSE_NUM; i++) {
printf("Course %d: %.2f\n", i+1, course_avg[i]);
}
}
阅读全文