Example: Calculating a Student's GPA

Here is a little program to illustrate some features of FORTRAN:

This program does not have any comments in it, but we should properly have a documentation box at the top for the main program (its name and purpose, etc.) and another for the subroutine.


         Program FindGPA

         Integer :: Ioresult, Hours, Numcourses
         Character :: Grade
         Character(30) :: Student
         Character(9) :: CourseID
         Real :: GPA
         Integer :: TotalHours
         Integer :: I

         Open(Unit=10, File='grades.txt', Status='old', Action='read', &
              IOstat=IOresult)

100      Format(A30, 3X, I1)
         Read(10, 100, IOstat=IOresult) Student, NumCourses

         Print *, 'Student Name:  ', Student
         Print *

         GPA = 0.0
         TotalHours = 0
         I = 1

         Do While (I <= NumCourses)
200        Format(A9, 3X, I1, 3X, A1)
           Read(10, 200) CourseID, Hours, Grade

           Call OneCourse(CourseID, Hours, Grade, GPA, TotalHours)

           I = I + 1
         End Do

         GPA = GPA / TotalHours

250      Format(/,1X, A18, I2, A7)
         Print 250, 'This student took ', TotalHours, ' hours.'

260      Format(/,1X, A26, F4.2)
         Print 260, 'GPA for this student is:  ', GPA

         Close(Unit=10)

         End Program FindGPA

         Subroutine OneCourse(CourseID, Hours, Grade, GPA, TotalHours)
         
         Character(9) :: CourseID 
         Integer :: Hours
         Character :: Grade
         Real :: GPA
         Integer :: TotalHours

         TotalHours = TotalHours + Hours

300      Format(11X, A9, 5X, I1, 5X, A1)
         Print 300, CourseID, Hours, Grade

         Select Case(Grade)
           Case ('A')
             GPA = GPA + 4.0 * Hours
           Case ('B')
             GPA = GPA + 3.0 * Hours
           Case ('C')
             GPA = GPA + 2.0 * Hours
           Case ('D')
             GPA = GPA + 1.0 * Hours
           Case ('F')
             GPA = GPA + 0.0 * Hours
           Case Default
             Print *, 'Bad Data in the Grade value!'
         End Select

         End Subroutine OneCourse


The input file, "grades.txt", contains the following:

Jean Eric Student                4
CSCI230     4   B                         
CHEM210     4   A                         
PHIL231     3   B
ENGL363     3   A

If we wanted to use different data, we could modify grades.txt and rerun the program without needing to recompile it.


The output from running the program is:

 Student Name:  Jean Eric Student             

           CSCI230       4     B
           CHEM210       4     A
           PHIL231       3     B
           ENGL363       3     A

 This student took 14 hours.

 GPA for this student is:  3.50


This program reads from a file named "grades.txt" and prints a few lines of output. The data in the file is expected to be in an exact format.

Although we have the IOstat variable, we are not actually using it to control the loop. We could add a test after the Open statement to check whether the Open operation was successful.

In the subroutine, each formal argument is declared as a local variable. The names do not need to be the same as the names of the actual arguments, but the types should match.

It would be simple enough to modify the program to compute the GPAs for several students.

After the first Read, we would have the beginning of a loop to run while IOresult is 0. After the two summary Print statements, we would insert another Read (like the first one), and then the loop would end.

We could also compute statistics: the highest GPA, the lowest GPA and the average GPA. We would need more variables, of course.

Likewise, we could modify the program to allow for other possible grades: W for Withdrawal, I for Incomplete, O for Audit, A+, B-, etc. We would have to change Grade to be 2 characters long, not only when it is declared but also in Format statements.