编写求十名学生三个科目 每个科目的总分 平均分 每科第一名的学生号的程序C语言二维数组
时间: 2024-12-02 13:15:29 浏览: 67
编写C语言程序来计算十名学生三个科目的总分、平均分以及每科的第一名学生的学号,可以使用二维数组来存储学生的成绩信息。以下是基本的步骤和伪代码:
首先,我们需要创建一个二维数组,大小为10x3,表示10位学生有3门课程的成绩。
```c
#include <stdio.h>
#define MAX_STUDENTS 10
#define MAX_SUBJECTS 3
int student_scores[MAX_STUDENTS][MAX_SUBJECTS];
// 函数声明
void input_scores();
double calculate_total(int student[], int subjects);
double calculate_average(double total);
int find_first_place(int scores[], int subject);
int main() {
// 输入学生分数
input_scores();
// 计算并打印总分和平均分
for (int i = 0; i < MAX_STUDENTS; i++) {
double total = calculate_total(student_scores[i], MAX_SUBJECTS);
printf("学生%d的总分: %.2f\n", i+1, total);
printf("学生%d的平均分: %.2f\n", i+1, calculate_average(total));
}
// 找出各科第一名并打印学号
for (int subject = 0; subject < MAX_SUBJECTS; subject++) {
int first_student = find_first_place(student_scores[0] + ... + student_scores[MAX_STUDENTS - 1], subject); // 通过展开数组求和
printf("科目%d的第一名学生号: %d\n", subject + 1, first_student + 1);
}
return 0;
}
// 输入函数
void input_scores() {
for (int i = 0; i < MAX_STUDENTS; i++) {
for (int j = 0; j < MAX_SUBJECTS; j++) {
scanf("%d", &student_scores[i][j]);
}
}
}
// 总分计算
double calculate_total(int scores[], int subjects) {
double sum = 0;
for (int i = 0; i < subjects; i++) {
sum += scores[i];
}
return sum;
}
// 平均分计算
double calculate_average(double total) {
return total / MAX_SUBJECTS;
}
// 查找第一名
int find_first_place(int scores[], int subject) {
int max_score = scores[subject];
int first_student = 0;
for (int i = 1; i < MAX_STUDENTS; i++) {
if (scores[i + MAX_SUBJECTS * first_student] > max_score) {
max_score = scores[i + MAX_SUBJECTS * first_student];
first_student = i;
}
}
return first_student;
}
```
这个程序会读取每个学生的三门课程成绩,然后分别计算每个学生、每门学科的总分和平均分,并找出每门学科的第一名学生的学号。
阅读全文