题目:某班有三个学生,输入每个学生的若干门成绩之后,求出每个学生的平均成绩,并找到他的最坏和最好成绩。
#include<stdio.h>
#define N 5
struct StudentInfo{
float grade[N];
};
float calculateAvgGrade(struct StudentInfo stu);
float findBestGrade(struct StudentInfo stu);
float findLowestGrade(struct StudentInfo stu);
int main(){
struct StudentInfo stu1, stu2, stu3;
printf("请输入学生1的成绩(%d门):", N);
for(int i = 0; i < N; i++){
scanf("%f", &stu1.grade[i]);
}
printf("请输入学生2的成绩(%d门):", N);
for(int i = 0; i < N; i++){
scanf("%f", &stu2.grade[i]);
}
printf("请输入学生3的成绩(%d门):", N);
for(int i = 0; i < N; i++){
scanf("%f", &stu3.grade[i]);
}
float avgGrade_stu1 = calculateAvgGrade(stu1);
float avgGrade_stu2 = calculateAvgGrade(stu2);
float avgGrade_stu3 = calculateAvgGrade(stu3);
float bestGrade_stu1 = findBestGrade(stu1);
float bestGrade_stu2 = findBestGrade(stu2);
float bestGrade_stu3 = findBestGrade(stu3);
float lowestGrade_stu1 = findLowestGrade(stu1);
float lowestGrade_stu2 = findLowestGrade(stu2);
float lowestGrade_stu3 = findLowestGrade(stu3);
printf("学生1的平均成绩: %.2f\n", avgGrade_stu1);
printf("学生2的平均成绩: %.2f\n", avgGrade_stu2);
printf("学生3的平均成绩: %.2f\n", avgGrade_stu3);
printf("学生1的最好成绩: %.2f\n", bestGrade_stu1);
printf("学生2的最好成绩: %.2f\n", bestGrade_stu2);
printf("学生3的最好成绩: %.2f\n", bestGrade_stu3);
printf("学生1的最坏成绩: %.2f\n", lowestGrade_stu1);
printf("学生2的最坏成绩: %.2f\n", lowestGrade_stu2);
printf("学生3的最坏成绩: %.2f\n", lowestGrade_stu3);
return 0;
}
float calculateAvgGrade(struct StudentInfo stu){
float totalGrade = 0.0;
float avgGrade = 0.0;
for(int i = 0; i < N; i++){
totalGrade += stu.grade[i];
}
avgGrade = totalGrade / N;
return avgGrade;
}
float findBestGrade(struct StudentInfo stu){
float best_grade = stu.grade[0];
for(int i = 0; i < N; i ++){
if(stu.grade[i] > stu.grade[0]){
best_grade = stu.grade[i];
}
}
return best_grade;
}
float findLowestGrade(struct StudentInfo stu){
float lowest_grade = stu.grade[0];
for(int i = 0; i < N; i ++){
if(stu.grade[i] < stu.grade[0]){
lowest_grade = stu.grade[i];
}
}
return lowest_grade;
}