      PROGRAM FREQ2
************************************************************************
* Program to plot a bar graph 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                                 *
*                                                                      *
* Input (keyboard): file name FNAME.                                   *
* Input (file):     number of defects per hour                         *
* Output:           bar graph displaying elements of array COUNT.      *
************************************************************************

      INTEGER MAXDEF
      PARAMETER (MAXDEF = 5)
      INTEGER DEFECT, COUNT(0:MAXDEF), 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
          IF (DEFECT .GT. MAXDEF) THEN
              DEFECT = MAXDEF
          END IF
          COUNT(DEFECT) = COUNT(DEFECT) + 1
          READ (15, *, IOSTAT = EOF) DEFECT
      GO TO 10
      END IF

      CALL BGRAPH(COUNT, MAXDEF, 'DEFECTIVES', 'NUMBER OF HOURS')

      CLOSE (15)
      END

**BGRAPH****************************************************************
* Subroutine to plot a bar graph representation of a frequency         *
* distribution.  Variables used are:                                   *
*     FREQ    :  array of frequencies                                  *
*     NFREQ   :  number of frequencies                                 *
*     VLABEL  :  label for vertical axis                               *
*     HLABEL  :  label for horizontal axis                             *
*     LIMBAR  :  parameter giving the size of the array BAR            *
*     BAR     :  character array used to print one bar of bar graph    *
*     LARGE   :  largest of FREQ(0), FREQ(1), ...                      *
*     I, J    :  indices                                               *
*                                                                      *
* Accepts:  Array FREQ, integer NFREQ, and character strings VLABEL    *
*           and HLABEL                                                 *
* Output:   A bar graph of the elements of FREQ                        * 
************************************************************************

      SUBROUTINE BGRAPH(FREQ, NFREQ, VLABEL, HLABEL)

      INTEGER NFREQ, FREQ(0:NFREQ), LIMBAR, LARGE, I, J
      PARAMETER (LIMBAR = 20)
      CHARACTER*(*), VLABEL, HLABEL
      CHARACTER*3 BAR(LIMBAR)

* Find largest count

      LARGE = FREQ(0)
      DO 10 I = 1, NFREQ
          LARGE = MAX(LARGE, FREQ(I))
 10   CONTINUE

* Print the bar graph
      PRINT 100, VLABEL
100   FORMAT(//1X, A)
      DO 40 I = 0, NFREQ
          DO 20 J = 1, LIMBAR
              BAR(J) = ' '
20        CONTINUE
          DO 30 J = 1, FREQ(I)
              BAR(J) = 'XXX'
30        CONTINUE
          PRINT 101, I, ':', BAR
101       FORMAT(1X, I10, 21A)
40    CONTINUE
      PRINT 102, ('.', I = 0, 3 * LARGE)
      PRINT 103, (I, I = 0, LARGE)
      PRINT 104, HLABEL
102   FORMAT(11X, 80A)
103   FORMAT(9X, 20I3)
104   FORMAT(11X, A)

      END
