From: marwk@levels.unisa.edu.au
Newsgroups: unisa.cis.prog.f.02462
Subject: PRACTICAL 7: Solution
Date: 9 Nov 92 11:44:48 +1030
Organization: University of South Australia

      PROGRAM P7

*     Reads in student ID's and marks and produces a count, the max, min
*     and average marks, the pass mark and distinction mark,
*     and on separate pages, the students within the given ranges.

      INTEGER DAT_UNIT, REP_UNIT
      INTEGER COUNT, MAX_MARK, MIN_MARK, PASS_MARK, DIST_MARK
      REAL AVERAGE

      PARAMETER (DATA_UNIT = 1, REP_UNIT = 2)

      OPEN (UNIT = DAT_UNIT, FILE = 'EXAM.DAT', STATUS = 'OLD')
      OPEN (UNIT = REP_UNIT, FILE = 'EXAM.REP', STATUS = 'NEW')

      CALL CALC_VALUES(COUNT, AVERAGE, MAX_MARK, MIN_MARK)

      PASS_MARK = NINT(3 * AVERAGE / 4 + MIN_MARK / 4.0)

      DIST_MARK = NINT(AVERAGE / 4 + 3 * MAX_MARK / 4.0)

      WRITE(UNIT = REP_UNIT, FMT = 100) COUNT, AVERAGE, MAX_MARK,
     +                                  MIN_MARK, DIST_MARK, PASS_MARK

  100 FORMAT('1', 20X, 'PRAC 7: EXAMINATION GRADES'//
     +       ' ', 20X, '  by RAYMOND KENNINGTON'///
     +       ' ', 'COUNT      ', I6//
     +       ' ', 'AVERAGE    ', F8.1//
     +       ' ', 'MAXIMUM    ', I6//
     +       ' ', 'MINIMUM    ', I6//
     +       ' ', 'DIST MARK  ', I6//
     +       ' ', 'PASS MARK  ', I6 )


      CALL WRITE_LIST(DAT_UNIT, REP_UNIT, 'DISTINCTION STUDENTS',
     +                DIST_MARK, MAX_MARK)

      CALL WRITE_LIST(DAT_UNIT, REP_UNIT, 'PASSED STUDENTS',
     +                PASS_MARK, DIST_MARK - 1)

      CALL WRITE_LIST(DAT_UNIT, REP_UNIT, 'FAILED STUDENTS',
     +                MIN_MARK, PASS_MARK - 1)

      CLOSE(UNIT = DAT_UNIT)
      CLOSE(UNIT = REP_UNIT)

      END


      SUBROUTINE CALC_VALUES(COUNT, AVERAGE, MAX_MARK, MIN_MARK)
      INTEGER COUNT, MAX_MARK, MIN_MARK
      REAL AVERAGE

      INTEGER SUM
      CHARACTER *8 ID
      INTEGER EOD, MARK

      COUNT = 0
      SUM   = 0

      MAX_MARK = -1
      MIN_MARK = 101

      READ(UNIT=IN_UNIT, FMT = 200, IOSTAT = EOD) ID, MARK

***   WHILE more data
   10 IF (EOD .GE. 0) THEN

          COUNT = COUNT + 1
          SUM = SUM + MARK

          MAX_MARK = MAX(MAX_MARK, MARK)
          MIN_MARK = MIN(MIN_MARK, MARK)

          READ(UNIT=IN_UNIT, FMT = 200, IOSTAT = EOD) ID, MARK

      GOTO 10
      ENDIF
***   END WHILE

      IF (COUNT .GT. 0) THEN
          AVERAGE = REAL(SUM)/ COUNT
      ELSE
          AVERAGE = 0
      ENDIF

  200 FORMAT(A8,I3)

      END


      SUBROUTINE WRITE_LIST(IN_UNIT, OUT_UNIT, HEADING, LOW, HIGH)

      INTEGER IN_UNIT, OUT_UNIT, LOW, HIGH
      CHARACTER *(*) HEADING

      CHARACTER *8 ID
      INTEGER EOD, MARK, COUNT

      REWIND (UNIT = IN_UNIT)

      WRITE (UNIT = OUT_UNIT, FMT = 100) HEADING

      COUNT = 0

      READ (UNIT = IN_UNIT, FMT = 200, IOSTAT = EOD) ID, MARK

***   WHILE more data
   10 IF (EOD .GE. 0) THEN

          IF (MARK .GE. LOW .AND. MARK .LE. HIGH) THEN
              COUNT = COUNT + 1

              WRITE (UNIT = OUT_UNIT, FMT = 300) ID, MARK
          ENDIF

          READ (UNIT = IN_UNIT, FMT = 200, IOSTAT = EOD) ID, MARK

      GOTO 10
      ENDIF
***   END WHILE

      WRITE(UNIT = OUT_UNIT, FMT = 400) COUNT

  100 FORMAT('1', A//)
  200 FORMAT(A8,I3)
  300 FORMAT(' ', A8,5X,I3)
  400 FORMAT('0', 'NUMBER:  ', I3)

      END
--
Raymond Kennington
LECTURER
University of South Australia   | Act in haste and repent at leisure
P.O. Box 1                      | Code too soon and debug forever
Ingle Farm                      | Knobs, knobs everywhere,
South Australia                 |              just vary a knob to think!
#! rnews 5022