有2个学生3门成绩,利用函数找出6个成绩中的最高分对应的学生和课程,用c语言写出来
时间: 2023-05-17 10:06:20 浏览: 75
以下是C语言代码:
```
#include <stdio.h>
int main() {
int scores[2][3] = {{80, 90, 70}, {85, 75, 95}};
int maxScore = 0;
int maxStudent = 0;
int maxCourse = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (scores[i][j] > maxScore) {
maxScore = scores[i][j];
maxStudent = i + 1;
maxCourse = j + 1;
}
}
}
printf("最高分数为%d,对应的学生为%d,对应的课程为%d\n", maxScore, maxStudent, maxCourse);
return 0;
}
```
这个程序会输出最高分数、对应的学生和对应的课程。
相关问题
计算每个学生的平均分: 定义一个函数,输入参数为一个学生的4门课成绩,返回值为该学生的平均分。在主程序中,分别输入3个学生的成绩,调用该函数计算每个学生的平均分并输出。 计算每门课的平均分: 定义一个函数,输入参数为所有学生在某门课的成绩,返回值为该门课的平均分。在主程序中,分别输入4门课的所有学生成绩,调用该函数计算每门课的平均分并输出。 找出12个分数中最高分数所对应的学生和课程: 定义一个函数,输入参数为3个学生4门课的成绩,返回值为最高分数所对应的学生和课程。在主程序中,输入3个学生4门课的成绩,调用该函数找出最高分数所对应的学生和课程并输出,用c语言,要简短
对于第一个问题,代码如下:
```c
float calcAverage(float score1, float score2, float score3, float score4) {
float average = (score1 + score2 + score3 + score4) / 4;
return average;
}
int main() {
float s1_score1, s1_score2, s1_score3, s1_score4;
float s2_score1, s2_score2, s2_score3, s2_score4;
float s3_score1, s3_score2, s3_score3, s3_score4;
// 输入每个学生的成绩
// ...
// 计算每个学生的平均分并输出
printf("Student 1's average score: %.2f\n", calcAverage(s1_score1, s1_score2, s1_score3, s1_score4));
printf("Student 2's average score: %.2f\n", calcAverage(s2_score1, s2_score2, s2_score3, s2_score4));
printf("Student 3's average score: %.2f\n", calcAverage(s3_score1, s3_score2, s3_score3, s3_score4));
return 0;
}
```
对于第二个问题,代码如下:
```c
float calcCourseAverage(float scores[], int numStudents) {
float sum = 0;
for (int i = 0; i < numStudents; i++) {
sum += scores[i];
}
float average = sum / numStudents;
return average;
}
int main() {
float course1_scores[3], course2_scores[3], course3_scores[3], course4_scores[3];
// 输入每门课的所有学生成绩
// ...
// 计算每门课的平均分并输出
printf("Course 1's average score: %.2f\n", calcCourseAverage(course1_scores, 3));
printf("Course 2's average score: %.2f\n", calcCourseAverage(course2_scores, 3));
printf("Course 3's average score: %.2f\n", calcCourseAverage(course3_scores, 3));
printf("Course 4's average score: %.2f\n", calcCourseAverage(course4_scores, 3));
return 0;
}
```
对于第三个问题,代码如下:
```c
void findMax(float scores[][4], int numStudents, int numCourses, int* maxStudent, int* maxCourse) {
float maxScore = -1;
for (int i = 0; i < numStudents; i++) {
for (int j = 0; j < numCourses; j++) {
if (scores[i][j] > maxScore) {
maxScore = scores[i][j];
*maxStudent = i + 1;
*maxCourse = j + 1;
}
}
}
}
int main() {
float scores[3][4];
// 输入3个学生4门课的成绩
// ...
int maxStudent, maxCourse;
findMax(scores, 3, 4, &maxStudent, &maxCourse);
printf("The highest score is %.2f, achieved by student %d in course %d.\n", scores[maxStudent - 1][maxCourse - 1], maxStudent, maxCourse);
return 0;
}
```
输入3个学生3门课的成绩,分别定义三个函数实现下列功能: 计算每个学生的平均分,并输出平均分; 计算每门课的平均分,并输出平均分; 找出所有分数中的最高分及对应的学生和课程,返回最高分,用全局变量带回学生和课程的下标。 用c语言写
在C语言中,我们可以定义三个函数来实现这些功能。首先,我们需要定义一个结构体`Student`来存储学生的信息,包括姓名和成绩数组。然后,我们定义全局变量来存储最高分及其相关信息。
```c
#include <stdio.h>
#include <stdlib.h>
// 定义学生结构体
typedef struct {
char name[50];
int scores[3]; // 学生的三门课成绩
} Student;
// 计算单个学生平均分
float student_avg(Student *student) {
float sum = 0;
for (int i = 0; i < 3; i++) {
sum += student->scores[i];
}
return sum / 3;
}
// 计算每门课的平均分
void course_avg() {
float total_scores[3] = {0}; // 初始化每门课总分
int students_count = 0;
// 假设学生数组已经存在
Student *students = ...;
for (Student *s = students; s != NULL; s++) {
for (int i = 0; i < 3; i++) {
total_scores[i] += s->scores[i];
students_count++;
}
}
for (int i = 0; i < 3; i++) {
printf("Course %d average score is %.2f\n", i+1, total_scores[i] / students_count);
}
}
// 找出最高分及对应信息
void find_max_score(Student **students, int *max_score, int *student_index, int *course_index) {
*max_score = students[0]->scores[0]; // 初始假设第一个学生的第一个成绩是最高的
*student_index = 0;
*course_index = 0;
for (int i = 0; i < students_count; i++) {
for (int j = 0; j < 3; j++) {
if (students[i]->scores[j] > *max_score) {
*max_score = students[i]->scores[j];
*student_index = i;
*course_index = j;
}
}
}
}
int main() {
// 初始化学生数组和最高分相关信息
Student *students = ...; // 假设这里已经有了学生的数组
int max_score = 0, student_index = 0, course_index = 0;
// 调用函数
printf("Student's average scores:\n");
for (Student *s = students; s != NULL; s++) {
printf("%s: %.2f\n", s->name, student_avg(s));
}
course_avg();
find_max_score(&students, &max_score, &student_index, &course_index);
printf("Highest score: %d, by %s in Course %d\n", max_score, students[*student_index].name, course_index + 1);
return 0;
}
```
请注意,这个示例假定有一个学生数组已存在,并且你需要根据实际情况填充它。此外,为了获取全局变量中的学生和课程索引,函数`find_max_score`的参数应该是指向数组的指针。
阅读全文
相关推荐















