# 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
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
DO i = 1, Number
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
```

### 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

• The format for reading an input line is simple: (A25, 3I5). If you declare the name variable with length 25, then A25 can be changed to A.
• For each student, his/her scores are read into Score1, Score2 and Score3, and average is computed and stored in Average. These scores are also added to Score1Avg, Score2Avg and Score3Avg for computing the class average of each score.
• Note that we have a CHARACTER variable ClassAverage of length 25 whose value is "Average". This value is used to print the last line of the report. This seems redundant, because we can change the last WRITE to the following:
```WRITE(*,FormatAvg)  " ", "Average", Score1Avg, Score2Avg, Score3Avg, ClassAvg
```
Does this work? The answer is no . It does not print the report in the specified way. The result 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
:
:
:
Robert Bogart                  94     100      86    93.33
=========================  ======  ======  ======  =======
Average  83.00   87.90   83.90    84.93
```
More precisely, the alignment is incorrect. Here is the reason: Since "Average" is of length 7 and is printed with A25, it is printed right-justified. Please refer to the CHARACTER Output for the details. In the program above, we use assignment. Since ClassAverage is of length 25, 18 spaces are appended when "Average" is assigned into ClassAverage.