成绩统计.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdio.h>
  2. struct Student {
  3. int id;
  4. char name[50];
  5. int score[3];
  6. };
  7. void input_students(int n, struct Student students[]) {
  8. int i,j;
  9. for (i = 0; i < n; ++i) {
  10. scanf("%d %s %d %d %d", &students[i].id, students[i].name,
  11. &students[i].score[0], &students[i].score[1], &students[i].score[2]);
  12. }
  13. }
  14. void calculate_student_average(int n, struct Student students[]) {
  15. int i;
  16. for (i = 0; i < n; ++i) {
  17. float average = (students[i].score[0] + students[i].score[1] + students[i].score[2]) / 3.0;
  18. printf("%d %s %.2f\n", students[i].id, students[i].name, average);
  19. }
  20. }
  21. void calculate_course_average(int n, struct Student students[]) {
  22. float sum[3] = {0};
  23. int i,j;
  24. for (i = 0; i < n; ++i) {
  25. for (j = 0; j < 3; ++j) {
  26. sum[j] += students[i].score[j];
  27. }
  28. }
  29. for (i = 0; i < 3; ++i) {
  30. float average = sum[i] / n;
  31. printf("%.2f ", average);
  32. }
  33. printf("\n");
  34. }
  35. int main() {
  36. int n;
  37. scanf("%d", &n);
  38. struct Student students[n];
  39. input_students(n, students);
  40. calculate_student_average(n, students);
  41. calculate_course_average(n, students);
  42. return 0;
  43. }