有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输人10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩、平均分数)。
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define N 5 struct student { int id; char name[20]; float chinese, math, english,avg; }; void max_and_avg(struct student* p); int main(void) { struct student stu[N] = { {1, "Alice", 85.5, 90.0, 88.0}, {2, "Bob", 78.0, 84.0, 80.5}, {3, "Charlie", 92.0, 95.5, 89.0}, {4, "David", 88.0, 76.0, 84.0}, {5, "Eva", 85.0, 88.0, 91.5} }; max_and_avg(stu); return 0;
}// puts("");
void max_and_avg(struct student* p) { for (int i = 0; i < N; i++) { p[i].avg =(p[i].chinese+p[i].math+p[i].english)/ 3; } float max = p[0].avg; int flag; for (int i = 0; i < N; i++) { printf("%s 的平均成绩为: %f\n",p[i].name,p[i].avg); } for (int i = 1; i < N; i++) { if (max < p[i].avg) { max=p[i].avg; flag = i; } } printf("最高成绩为: %d 号 %s 语文:%f 数学:%f 英语:%f avg=%f", p[flag].id, p[flag].name, p[flag].chinese, p[flag].math, p[flag].english, p[flag].avg); }
#include <st...
用户登录可进行刷题及查看答案
#include <stdio.h> #define NAMLEN 20 #define STUCNT 10 typedef struct student_t{ int num; char name[NAMLEN]; int score[3]; } student; int main(){ student students[STUCNT]; int maxi = 0, maxsum = 0; double aver_0 = 0, aver_1 = 0, aver_2 = 0; for (int i = 0; i < STUCNT; i++){ scanf("%d%s%d%d%d", &students[i].num, students[i].name, &students[i].score[0], &students[i].score[1], &students[i].score[2]); int sum = students[i].score[0] + students[i].score[1] + students[i].score[2]; if (sum > maxsum){ maxsum = sum; maxi = i; } aver_0 += students[i].score[0]; aver_1 += students[i].score[1]; aver_2 += students[i].score[2]; } aver_0 /= STUCNT; aver_1 /= STUCNT; aver_2 /= STUCNT; printf("%lf %lf %lf\n", aver_0, aver_1, aver_2); printf("%d %s %d %d %d %lf\n", students[maxi].num, students[maxi].name, students[maxi].score[0], students[maxi].score[1], students[maxi].score[2], (students[maxi].score[0] + students[maxi].score[1] + students[maxi].score[2]) / 3.0); return 0; }
登录后提交答案