编辑代码

#include <stdio.h>
#define STUDENTS 3
#define EXAMS 4

// function prototypes
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests);
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests);
double average(const int setOfGrades[], size_t tests);
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests);

// function main begins program execution
int main(void)
{
   // initialize student grades for three students (rows)
   int studentGrades[STUDENTS][EXAMS] =
      { { 77, 68, 86, 73 },
        { 96, 87, 89, 78 },
        { 70, 90, 86, 81 } };

   // output array studentGrades
   puts("The array is:");
   printArray(studentGrades, STUDENTS, EXAMS);

   // determine smallest and largest grade values
   for(int i=0;i<STUDENTS;i++){
   printf("\n\nThe student [%d]'s Lowest grade: %d\nThe student [%d]'s Highest grade: %d\n",i,
      minimum(studentGrades, i, EXAMS),i,
      maximum(studentGrades, i, EXAMS));
   }
   puts("");

   // calculate average grade for each exam
   int examgrades[4][3];
   for(int i=0;i<3;i++)
   {
       for(int j=0;j<4;j++)
       {
           examgrades[j][i]=studentGrades[i][j];
       }
   }
   for (size_t exam = 0; exam < EXAMS; ++exam) {
      printf("The average grade for exam %u is %.2f\n",
         exam, average(examgrades[exam], STUDENTS));
   }
}

// Find the minimum grade
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
   int lowGrade = 100; // initialize to highest possible grade


      // loop through columns of grades
      for (size_t j = 0; j < tests; ++j) {

         if (grades[pupils][j] < lowGrade) {
            lowGrade = grades[pupils][j];
         }
      }


   return lowGrade; // return minimum grade
}

// Find the maximum grade
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
   int highGrade = 0; // initialize to lowest possible grade


      // loop through columns of grades
      for (size_t j = 0; j < tests; ++j) {

         if (grades[pupils][j] > highGrade) {
            highGrade = grades[pupils][j];
         }
      }

   return highGrade; // return maximum grade
}

// Determine the average grade for a particular student
double average(const int setOfGrades[], size_t tests)
{
   int total = 0; // sum of test grades

   // total all grades for one student
   for (size_t i = 0; i < tests; ++i) {
      total += setOfGrades[i];
   }

   return (double) total / tests; // average
}

// Print the array
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests)
{
   // output column heads
   printf("%s", "                 [0]  [1]  [2]  [3]");

   // output grades in tabular format
   for (size_t i = 0; i < pupils; ++i) {

      // output label for row
      printf("\nstudentGrades[%u] ", i);

      // output grades for one student
      for (size_t j = 0; j < tests; ++j) {
         printf("%-5d", grades[i][j]);
      }
   }
}