WARNING: This example assumes spaces are ignored for the I and F descriptors.
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 ....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
Note that the first position is always for printer control. Note also that we do not know the number of salespersons.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
Click here to download this program.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
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
Does this work? The answer is . It does not print the report in the specified way. The result is:WRITE(*,FormatAvg) " ", "Average", Score1Avg, Score2Avg, Score3Avg, ClassAvg
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.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