Letter Grade Computation Using Assumed-Shape Arrays

Problem Statement

Suppose there are n score values x1, x2, ..., xn. Let its mean and standard deviation be m and s. A letter grade is assigned to each score based on the following scale:

The mean, variance and standard deviation are computed with the following formulae:

Write a program that uses assumed-shape arrays.

Solution

! --------------------------------------------------------------------
! PROGRAM  Grading:
!    This program reads in a set of values, computes its mean,
! variance, and standard deviation, and use the mean and standard
! deviation to compute a letter grade.
!    This program shows you passing arrays as arguments with assumed-
! shape arguments.  Since all lower bounds are 1, no extent
! information are passed.
! --------------------------------------------------------------------

PROGRAM  Grading
   IMPLICIT  NONE

   INTEGER, PARAMETER :: MAX_SIZE = 100
   REAL, DIMENSION(1:MAX_SIZE) :: InputData
   INTEGER                     :: ActualSize

   CALL  ReadArray(InputData, ActualSize)
   CALL  DisplayResult(InputData, ActualSize)

CONTAINS

! --------------------------------------------------------------------
! SUBROUTINE  ReadArray():
!    This subroutine reads in the number of data and the data set.
! --------------------------------------------------------------------

   SUBROUTINE ReadArray(x, n)
      IMPLICIT  NONE
      INTEGER, INTENT(OUT)             :: n
      REAL, DIMENSION(1:), INTENT(OUT) :: x
      INTEGER                          :: i

      READ(*,*)  n
      READ(*,*)  (x(i), i = 1, n)
   END SUBROUTINE  ReadArray

! --------------------------------------------------------------------
! SUBROUTINE  DisplayResult():
!    This subroutine calls MeanVariance() to compute the mean,
! variance and standard deviation, and prints a grade report.  The
! letter grade is determined by CHARACTER function LetterGrade().
! --------------------------------------------------------------------

   SUBROUTINE  DisplayResult(Data, n)
      IMPLICIT  NONE
      INTEGER, INTENT(IN)             :: n
      REAL, DIMENSION(1:), INTENT(IN) :: Data
      INTEGER                         :: i
      REAL                            :: Mean, Var, Std

      CALL  MeanVariance(Data, n, Mean, Var, Std)
      WRITE(*,*)  "Grading Report"
      WRITE(*,*)
      DO i = 1, n
         WRITE(*,*) Data(i), "  ", LetterGrade(Data(i), Mean, Std)
      END DO
      WRITE(*,*)
      WRITE(*,*)  "No. of students          = ", n
      WRITE(*,*)  "Class average            = ", Mean
      WRITE(*,*)  "Class variance           = ", Var
      WRITE(*,*)  "Class standard deviation = ", Std
   END SUBROUTINE  DisplayResult

! --------------------------------------------------------------------
! CHARACTER FUNCTION  LetterGrade():
!    This function receives a score and the mean and standard deviation
! values, and returns a letter grade.
! --------------------------------------------------------------------

   CHARACTER FUNCTION  LetterGrade(x, Mean, StdDev)
      IMPLICIT  NONE
      REAL, INTENT(IN) :: x, Mean, StdDev

      IF (x < Mean - 1.5*StdDev) THEN
         LetterGrade = "F"
      ELSE IF (x < Mean - 0.5*StdDev) THEN
         LetterGrade = "D"
      ELSE IF (x < Mean + 0.5*StdDev) THEN
         LetterGrade = "C"
      ELSE IF (x < 1.5*StdDev) THEN
         LetterGrade = "B"
      ELSE
         LetterGrade = "A"
      END IF
   END FUNCTION  LetterGrade

! --------------------------------------------------------------------
! SUBROUTINE  MeanVariance():
!    This subroutine computes the mean, variance and standard
! deviation.
! --------------------------------------------------------------------

   SUBROUTINE  MeanVariance(Data, n, Mean, Variance, StdDev)
      IMPLICIT  NONE
      INTEGER, INTENT(IN)             :: n
      REAL, DIMENSION(1:), INTENT(IN) :: Data
      REAL, INTENT(OUT)               :: Mean, Variance, StdDev
      INTEGER                         :: i

      Mean = 0.0
      DO i = 1, n
         Mean = Mean + Data(i)
      END DO
      Mean = Mean / n

      Variance = 0.0
      DO i = 1, n
         Variance = Variance + (Data(i) - Mean)**2
      END DO
      Variance = Variance / n
      StdDev   = SQRT(Variance)
   END SUBROUTINE  MeanVariance

END PROGRAM  Grading
Click here to download this program.

Program Input and Output

If the input data consist of the following:
10
78.0  90.0  55.0  95.0  64.0
87.0  73.0  92.0  85.0  80.0
The output of the program is:
Grading Report

78.  C
90.  A
55.  F
95.  A
64.  D
87.  A
73.  D
92.  A
85.  C
80.  C

No. of students          = 10
Class average            = 79.9000015
Class variance           = 147.690002
Class standard deviation = 12.1527777