Computing the Class Average

WARNING: This example assumes spaces are ignored for the I and F descriptors.

Problem Statement

Suppose we have an input like the following. The first line gives the number of input lines (i.e. students). Each of the input line has four fields. Positions 1 to 25 has a student's name, followed by three test scores with five positions each.
         1    1    2    2    3    3    4
....5....0....5....0....5....0....5....0
10
Jon Rokne                100  95   98
Mukesh Prasad            78   89   82
Jack Morrison            94   98   95
Ken Musgrave             100  100  96
Douglas Voorhies         87   95   89
Dale Schumacher          76   85   73
John Schlag              56   62   50
James Hall               60   78   84
Alan Lindgren            85   77   86
Robert Bogart            94   100  86
Write a Fortran program that reads in the above input, computes the average of each student, the average of each test score and the class average, and prints the following table:
         1    1    2    2    3    3    4    4    5    5    6
....5....0....5....0....5....0....5....0....5....0....5....0
 Name                       Score1  Score2  Score3  Average
 =========================  ======  ======  ======  =======
 Jon Rokne                     100      95      98    97.67
 Mukesh Prasad                  78      89      82    83.00
 Jack Morrison                  94      98      95    95.67
 Ken Musgrave                  100     100      96    98.67
 Douglas Voorhies               87      95      89    90.33
 Dale Schumacher                76      85      73    78.00
 John Schlag                    56      62      50    56.00
 James Hall                     60      78      84    74.00
 Alan Lindgren                  85      77      86    82.67
 Robert Bogart                  94     100      86    93.33
 =========================  ======  ======  ======  =======
 Average                     83.00   87.90   83.90    84.93
Note that the first position is always for printer control. Note also that we do not know the number of salespersons.

Solution

PROGRAM  Input_1
   IMPLICIT  NONE
   CHARACTER(LEN=25) :: Name, ClassAverage
   CHARACTER(LEN=30) :: FormatIn, FormatOut, FormatAvg
   INTEGER           :: Number, i
   INTEGER           :: Score1, Score2, Score3
   REAL              :: Average
   REAL              :: Score1Avg, Score2Avg, Score3Avg, ClassAvg

   FormatIn  = "(A25, 3I5)"
   FormatOut = "(A, A25, 3I8, F9.2)"
   FormatAvg = "(A, A25, 3F8.2, F9.2)"
   WRITE(*,"(2A)")  " ", "Name                       Score1  Score2  Score3  Average"
   WRITE(*,"(2A)")  " ", "=========================  ======  ======  ======  ======="
   ClassAverage = "Average"
   Score1Avg    = 0.0
   Score2Avg    = 0.0
   Score3Avg    = 0.0
   READ(*,*)  Number
   DO i = 1, Number
      READ(*,FormatIn)  Name, Score1, Score2, Score3
      Average = REAL(Score1 + Score2 + Score3) / 3.0
      WRITE(*,FormatOut)  " ", Name, Score1, Score2, Score3, Average
      Score1Avg = Score1Avg + Score1
      Score2Avg = Score2Avg + Score2
      Score3Avg = Score3Avg + Score3
   END DO
   Score1Avg = Score1Avg / Number
   Score2Avg = Score2Avg / Number
   Score3Avg = Score3Avg / Number
   ClassAvg  = (Score1Avg + Score2Avg + Score3Avg) / 3.0
   WRITE(*,"(2A)")     " ", "=========================  ======  ======  ======  ======="
   WRITE(*,FormatAvg)  " ", ClassAverage, Score1Avg, Score2Avg, Score3Avg, ClassAvg
END PROGRAM  Input_1
Click here to download this program.

Program Input and Output

The output of the program is:
         1    1    2    2    3    3    4    4    5    5    6
....5....0....5....0....5....0....5....0....5....0....5....0
 Name                       Score1  Score2  Score3  Average
 =========================  ======  ======  ======  =======
 Jon Rokne                     100      95      98    97.67
 Mukesh Prasad                  78      89      82    83.00
 Jack Morrison                  94      98      95    95.67
 Ken Musgrave                  100     100      96    98.67
 Douglas Voorhies               87      95      89    90.33
 Dale Schumacher                76      85      73    78.00
 John Schlag                    56      62      50    56.00
 James Hall                     60      78      84    74.00
 Alan Lindgren                  85      77      86    82.67
 Robert Bogart                  94     100      86    93.33
 =========================  ======  ======  ======  =======
 Average                     83.00   87.90   83.90    84.93

Discussion