The mean, variance and standard deviation are computed with the following formulae:
Write a program that uses the easy way for passing array arguments. More precisely, the whole extent information is passed.
Click here to download this program.! -------------------------------------------------------------------- ! 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 the easy way of passing arrays as ! arguments. Lower bound and upper bound are passed. ! -------------------------------------------------------------------- PROGRAM Grading IMPLICIT NONE INTEGER, PARAMETER :: MAX_SIZE = 100 REAL, DIMENSION(1:MAX_SIZE) :: InputData INTEGER :: ActualSize CALL ReadArray(InputData, MAX_SIZE, ActualSize) CALL DisplayResult(InputData, MAX_SIZE, ActualSize) CONTAINS ! -------------------------------------------------------------------- ! SUBROUTINE ReadArray(): ! This subroutine reads in the number of data and the data set. ! -------------------------------------------------------------------- SUBROUTINE ReadArray(x, SIZE, n) IMPLICIT NONE INTEGER, INTENT(IN) :: SIZE INTEGER, INTENT(OUT) :: n REAL, DIMENSION(1:SIZE), 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, SIZE, n) IMPLICIT NONE INTEGER, INTENT(IN) :: SIZE INTEGER, INTENT(IN) :: n REAL, DIMENSION(1:SIZE), INTENT(IN) :: Data INTEGER :: i REAL :: Mean, Var, Std CALL MeanVariance(Data, SIZE, 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 < Mean + 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, SIZE, n, Mean, Variance, StdDev) IMPLICIT NONE INTEGER, INTENT(IN) :: SIZE INTEGER, INTENT(IN) :: n REAL, DIMENSION(1:SIZE), 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
The output of the program is:10 78.0 90.0 55.0 95.0 64.0 87.0 73.0 92.0 85.0 80.0
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