      PROGRAM FREQ1
************************************************************************
* Program to generate a frequency distribution of the number of 1-hour *
* periods in which there were 0, 1, 2, ... defective parts produced by *
* a machine.  The data is read from a file.  Identifiers used are:     *
*     MAXDEF  :  parameter representing maximum # of defective parts   *
*     COUNT   :  COUNT(I) = # of 1-hour periods with I defective parts *
*     DEFECT  :  # of defective parts read from file                   *
*     FNAME   :  name of the data file                                 *
*     EOF     :  end-of-file indicator                                 *
*     I       :  subscript                                             *
*                                                                      *
* Input (keyboard): File name FNAME                                    *
* Input (file):     Number of defects per hour                         *
* Output:           Frequency distribution -- elements of array COUNT  *
************************************************************************

      INTEGER MAXDEF
      PARAMETER (MAXDEF = 5)
      INTEGER DEFECT, COUNT(0:MAXDEF), I, EOF
      CHARACTER*20 FNAME
      DATA COUNT/0, MAXDEF*0/

* Get file name, open the file as unit 15

      PRINT *, 'ENTER NAME OF DATA FILE'
      READ '(A)', FNAME
      OPEN (UNIT = 15, FILE = FNAME, STATUS = 'OLD')

* While there is more data, read # of defective parts and
* increment appropriate counter
      READ (15, *, IOSTAT = EOF) DEFECT
 10   IF (EOF .GE. 0) THEN
         DEFECT = MIN(DEFECT, MAXDEF)
         COUNT(DEFECT) = COUNT(DEFECT) + 1
         READ (15, *, IOSTAT = EOF) DEFECT
      GO TO 10
      END IF

* Print the frequency distribution

      PRINT 101
      PRINT 102
 101  FORMAT(1X, '# OF DEFECTIVES   # OF HOURS')
 102  FORMAT(1X, '===============   ==========')
      DO 20 I = 0, MAXDEF
         PRINT 103, I, COUNT(I)
 103     FORMAT(1X, I9, I15)
 20   CONTINUE

      CLOSE (15)

      END

