Programming Example 1: Letter Grade Computation

Problem Statement

At the end of a quarter, the average of three marks must be computed. Then, this average is rounded and used to determine the corresponding letter grade. The letter grades are computed as follows:

Range Grade
>= 90 A
< 90 and >= 85 AB
< 85 and >= 80 B
< 80 and >= 75 BC
< 75 and >= 70 C
< 70 and >= 65 CD
< 65 and >= 60 D
< 60 F

Write a program to read three marks, compute its average, round the average and use it to determine the corresponding letter grade.

Solution

! ----------------------------------------------------------
! This program reads three marks, computes their average
! and determines the corresponding letter grade with
! the following table:
!
!         A   : average >= 90
!         AB  : 85 <= average < 90
!         B   : 80 <= average < 84
!         BC  : 75 <= average < 79
!         C   : 70 <= average < 74
!         CD  : 65 <= average < 69
!         D   : 60 <= average < 64
!         F   : average < 60
!
! where 'average' is the rounded average of the three
! marks.  More precisely, if the average is 78.6, then it
! becomes 79 after rounding; or, if the average is 78.4,
! it becomes 78 after truncating.
! ----------------------------------------------------------

PROGRAM  LetterGrade
   IMPLICIT   NONE

   REAL              :: Mark1, Mark2, Mark3
   REAL              :: Average
   CHARACTER(LEN=2)  :: Grade

   READ(*,*)  Mark1, Mark2, Mark3
   Average = (Mark1 + Mark2 + Mark3) / 3.0

   SELECT CASE (NINT(Average))     ! round Average before use
      CASE (:59)                   ! <= 59 -------------> F
         Grade = 'F '
      CASE (60:64)                 ! >= 60 and <= 64 ---> D
         Grade = 'D '
      CASE (65:69)                 ! >= 65 and <= 69 ---> CD
         Grade = 'CD'
      CASE (70:74)                 ! >= 70 and <= 74 ---> C
         Grade = 'C '
      CASE (75:79)                 ! >= 75 and <= 79 ---> BC
         Grade = 'BC'
      CASE (80:84)                 ! >= 80 and <= 84 ---> B
         Grade = 'B '
      CASE (85:89)                 ! >= 84 and <= 89 ---> AB
         Grade = 'AB'
      CASE DEFAULT                 ! >= 90 -------------> A
         Grade = 'A '
   END SELECT

   WRITE(*,*)  'First Mark    : ', Mark1
   WRITE(*,*)  'Second Mark   : ', Mark2
   WRITE(*,*)  'Third Mark    : ', Mark3
   WRITE(*,*)  'Average       : ', Average
   WRITE(*,*)  'Letter Grade  : ', Grade

END PROGRAM  LetterGrade
Click here to download this program.

Program Input and Output

Discussion

This is an easy example and only one place requires further explanation. The concept of rounding is converting a real number to its nearest integer. Therefore, after rounding, 4.5 and 4.4 become 5 and 4, respectively. Moreover, after rounding, -4.5 and -4.4 become -5 and -4, respectively. The Fortran intrinsic function for rounding is NINT(), which was discussed in Fortran intrinsic functions.

In the program, REAL variable Average holds the average of the three input marks. To round the average value to be used in the SELECT CASE statement, NINT(Average) is used. Other parts are obvious.