C语言(用函数写)代码生成,输入10个学生10门课程的成绩,要求包括的功能: 1.求出某个学生的最高成绩; 2.求出某门课程的最高成绩; 3.求出某个学生的平均成绩; 4.求出某门课程的平均成绩; 5.增加一个学生及其N门课程的成绩。
时间: 2023-06-15 12:08:29 浏览: 76
下面是一个使用函数实现的 C 语言代码,实现了你所要求的功能:
```c
#include <stdio.h>
#define MAX_STUDENT 10
#define MAX_COURSE 10
void input_scores(int scores[][MAX_COURSE], int num_students, int num_courses);
void print_scores(int scores[][MAX_COURSE], int num_students, int num_courses);
int max_score_student(int scores[][MAX_COURSE], int student, int num_courses);
int max_score_course(int scores[][MAX_COURSE], int course, int num_students);
float average_score_student(int scores[][MAX_COURSE], int student, int num_courses);
float average_score_course(int scores[][MAX_COURSE], int course, int num_students);
void add_student(int scores[][MAX_COURSE], int *num_students_ptr, int num_courses);
int main(void) {
int scores[MAX_STUDENT][MAX_COURSE];
int num_students = MAX_STUDENT;
int num_courses = MAX_COURSE;
input_scores(scores, num_students, num_courses);
printf("Scores:\n");
print_scores(scores, num_students, num_courses);
int student = 0;
printf("Enter student index (0 to %d) to find max score: ", num_students - 1);
scanf("%d", &student);
printf("Max score for student %d: %d\n", student, max_score_student(scores, student, num_courses));
int course = 0;
printf("Enter course index (0 to %d) to find max score: ", num_courses - 1);
scanf("%d", &course);
printf("Max score for course %d: %d\n", course, max_score_course(scores, course, num_students));
printf("Enter student index (0 to %d) to find average score: ", num_students - 1);
scanf("%d", &student);
printf("Average score for student %d: %.2f\n", student, average_score_student(scores, student, num_courses));
printf("Enter course index (0 to %d) to find average score: ", num_courses - 1);
scanf("%d", &course);
printf("Average score for course %d: %.2f\n", course, average_score_course(scores, course, num_students));
add_student(scores, &num_students, num_courses);
printf("Scores after adding a student:\n");
print_scores(scores, num_students, num_courses);
return 0;
}
void input_scores(int scores[][MAX_COURSE], int num_students, int num_courses) {
for (int i = 0; i < num_students; i++) {
printf("Enter scores for student %d:\n", i);
for (int j = 0; j < num_courses; j++) {
printf("Course %d: ", j);
scanf("%d", &scores[i][j]);
}
}
}
void print_scores(int scores[][MAX_COURSE], int num_students, int num_courses) {
for (int i = 0; i < num_students; i++) {
printf("Student %d: ", i);
for (int j = 0; j < num_courses; j++) {
printf("%d ", scores[i][j]);
}
printf("\n");
}
}
int max_score_student(int scores[][MAX_COURSE], int student, int num_courses) {
int max_score = scores[student][0];
for (int i = 1; i < num_courses; i++) {
if (scores[student][i] > max_score) {
max_score = scores[student][i];
}
}
return max_score;
}
int max_score_course(int scores[][MAX_COURSE], int course, int num_students) {
int max_score = scores[0][course];
for (int i = 1; i < num_students; i++) {
if (scores[i][course] > max_score) {
max_score = scores[i][course];
}
}
return max_score;
}
float average_score_student(int scores[][MAX_COURSE], int student, int num_courses) {
int sum = 0;
for (int i = 0; i < num_courses; i++) {
sum += scores[student][i];
}
return (float) sum / num_courses;
}
float average_score_course(int scores[][MAX_COURSE], int course, int num_students) {
int sum = 0;
for (int i = 0; i < num_students; i++) {
sum += scores[i][course];
}
return (float) sum / num_students;
}
void add_student(int scores[][MAX_COURSE], int *num_students_ptr, int num_courses) {
if (*num_students_ptr >= MAX_STUDENT) {
printf("Cannot add more students.\n");
return;
}
printf("Enter scores for new student:\n");
for (int j = 0; j < num_courses; j++) {
printf("Course %d: ", j);
scanf("%d", &scores[*num_students_ptr][j]);
}
(*num_students_ptr)++;
}
```
这个程序中,`input_scores` 函数用于输入成绩,`print_scores` 函数用于输出成绩,`max_score_student` 函数用于求出某个学生的最高成绩,`max_score_course` 函数用于求出某门课程的最高成绩,`average_score_student` 函数用于求出某个学生的平均成绩,`average_score_course` 函数用于求出某门课程的平均成绩,`add_student` 函数用于增加一个学生及其 N 门课程的成绩。在 `main` 函数中,我们先调用 `input_scores` 函数输入成绩,然后依次调用上述函数计算所需的结果。其中,`add_student` 函数可以增加一个学生及其 N 门课程的成绩,它通过传入一个指向 `num_students` 变量的指针实现了改变 `num_students` 变量的值。
阅读全文