      PROGRAM PGM08
************************************************************************
*    PROGRAMMER: B. Alex Bridges
*    CLASS:      9:00AM
*    PROGRAM:    PGM08.F
*    DUE DATE:   03/11/99
*
* Program Description:
*    This program reads grade information from a data file. It uses
*    the scores to determine the mean and standard deviation.  It also
*    assigns a letter grade to each score.
* Variables:
*    NAMEF   : student's first name
*    NAMEL   : student's last name
*    GRADE   : current letter grade
*    LETTER  : letter grade returned from LETTER function
*    LIMIT   : max. number of scores
*    ACTUAL  : actual number of scores used
*    I       : loop counter/array index
*    SCORES  : set of scores for student
*    MEAN    : mean of the scores
*    STDDEV  : standard deviation of the scores
* Input  : NAMEF,NAMEL,ACTUAL,SCORES
* Output : SCORES,MEAN,STDDEV
************************************************************************

        CHARACTER*5 NAMEF,NAMEL*8,GRADE*1,LETTER*1
        INTEGER LIMIT,ACTUAL,I
        PARAMETER (LIMIT = 100)
        REAL SCORES(LIMIT),MEAN,STDDEV

* open the data + output file
        OPEN (UNIT=12, FILE="pgm08.dat")
        OPEN (UNIT=13, FILE="pgm08.out")

* read in values for data items
        CALL INPUT (LIMIT,NAMEF,NAMEL,ACTUAL,SCORES)

* write the header
c       PRINT '("Student Name: ",A,A,//,
c    1          T30,"PROGRAM - PGM08",/)', NAMEF,NAMEL
        WRITE (13, '("Student Name: ",A,A,//,
     1               T30,"PROGRAM - PGM08",/)') NAMEF,NAMEL

* write the input
c       PRINT '("No. of Scores: ",I2,//,
c    1          "The scores are:")', ACTUAL
        WRITE (13, '("No. of Scores: ",I2,//,
     1               "The scores are:")') ACTUAL

        DO 10 I = 1, ACTUAL
c         PRINT '(4X,F5.1)', SCORES(I)
          WRITE (13, '(4X,F5.1)') SCORES(I)
 10     CONTINUE

* calculate mean and standard deviation
        CALL CALC (ACTUAL,SCORES,MEAN,STDDEV)

* write output part 1
c       PRINT '(/"Mean Score = ",F6.2,/
c    1          "Standard Deviation = "F6.3/)', MEAN,STDDEV
        WRITE (13, '(/"Mean Score = ",F6.2,/
     1               "Standard Deviation = "F6.3/)') MEAN,STDDEV

* calculate letter grade and write output part 2
c       PRINT '(T5,"SCORE",T15,"GRADE",/
c    1          T5,"=====",T15,"=====")'
        WRITE (13, '(T5,"SCORE",T15,"GRADE",/
     1               T5,"=====",T15,"=====")')

        DO 20 I = 1, ACTUAL
          GRADE = LETTER (MEAN,STDDEV,SCORES(I))
c         PRINT '(T5,F5.1,T17,A1)', SCORES(I),GRADE
          WRITE (13, '(T5,F5.1,T17,A1)') SCORES(I),GRADE
 20     CONTINUE

* write end of program message and close the data + output file
c       PRINT '(/,T28,"End of program - PGM08.F",/)'
        WRITE (13, '(/,T28,"End of program - PGM08.F",/)')
        CLOSE(12)
        CLOSE(13)

        END

************************************************************************

************************************************************************
* Subroutine: INPUT                                                    *
* Purpose: Reads in values for data items from the data file.          *
* Input: --PARAMETERS--                                                *
*        => 'LIMIT'  = Max. number of scores.                          *
* Output: --PARAMETERS--                                               *
*        => 'NAMEF'  = Student's first name.                           *
*        => 'NAMEL'  = Student's last name.                            *
*        => 'ACTUAL' = Actual number of scores used.                   *
*        => 'SCORES' = Set of scores.                                  *
************************************************************************
        SUBROUTINE INPUT (LIMIT,NAMEF,NAMEL,ACTUAL,SCORES)

        CHARACTER*5 NAMEF,NAMEL*8
        INTEGER LIMIT,ACTUAL,I
        REAL SCORES(LIMIT)

* read in name
        READ (12, *), NAMEF,NAMEL

* read in actual number of scores
        READ (12, *), ACTUAL

* read in scores
        READ (12, *) (SCORES(I),I=1,ACTUAL)

        END

************************************************************************
* Subroutine: CALC                                                     *
* Purpose: Calculates the mean and standard deviation for the scores.  *
* Input: --PARAMETERS--                                                *
*        => 'ACTUAL' = Actual number of scores used.                   *
*        => 'SCORES' = Set of scores.                                  *
* Output: --PARAMETERS--                                               *
*        => 'MEAN'   = Mean for the scores.                            *
*        => 'STDDEV' = Standard deviation for the scores.              *
************************************************************************
        SUBROUTINE CALC (ACTUAL,SCORES,MEAN,STDDEV)

        INTEGER ACTUAL,I
        REAL SCORES(ACTUAL),MEAN,STDDEV,SUM,VAR

* calculate the mean
        DO 10 I=1, ACTUAL
          SUM = SUM + SCORES(I)
 10     CONTINUE
        MEAN = SUM / ACTUAL

* calculate the mean
        DO 20 I=1, ACTUAL
          VAR = VAR + ( ( SCORES(I) - MEAN )**2. )
 20     CONTINUE
        VAR = VAR / ACTUAL
        STDDEV = SQRT(VAR)

        END

************************************************************************
* Function: LETTER                                                     *
* Purpose: Converts the numeric score to a letter gr                   *
* Input: --PARAMETERS--                                                *
*        => 'MEAN'   = Mean for the scores.                            *
*        => 'STDDEV' = Standard deviation for the scores.              *
*        => 'SCORE'  = The score.                                      *
* Output: --RETURNS--                                                  *
*        => 'LETTER'  = The letter grade.                              *
************************************************************************
        CHARACTER FUNCTION LETTER (MEAN,STDDEV,SCORE)

        REAL MEAN,STDDEV,SCORE

* determine the letter grade
        IF ( SCORE .LT. (MEAN - 1.5 * STDDEV) ) THEN
          LETTER = 'F'
        ELSE IF ( SCORE .LT. (MEAN - 0.5 *STDDEV) ) THEN
          LETTER = 'D'
        ELSE IF ( SCORE .LT. (MEAN + 0.5 *STDDEV) ) THEN
          LETTER = 'C'
        ELSE IF ( SCORE .LT. (MEAN + 1.5 *STDDEV) ) THEN
          LETTER = 'B'
        ELSE
          LETTER = 'A'
        END IF

        END
