      PROGRAM GRAPH
************************************************************************
* Program to plot the graph of a function Y = F(X).  F is defined by a *
* function subprogram and passed to subroutine PLOT.  Identifiers      *
* used are:                                                            *
*     F          : function to be plotted                              *
*     XMIN, XMAX : minimum and maximum X values                        *
*     YMIN, YMAX : minimum and maximum Y values                        *
*                                                                      *
* Input:  XMIN, XMAX, YMIN, YMAX                                       *
* Output: User prompts and graph of Y = F(X)                           *
************************************************************************
      REAL F, XMIN, XMAX, YMIN, YMAX
      EXTERNAL F

      PRINT *, 'ENTER MINIMUM AND MAXIMUM X VALUES AND'
      PRINT *, 'THE MINIMUM AND MAXIMUM Y VALUES:'
      READ *, XMIN, XMAX, YMIN, YMAX
      CALL PLOT(F, XMIN, XMAX, YMIN, YMAX)

      END

    
**F*********************************************************************
*              Function whose graph is to be plotted                   *
************************************************************************

      FUNCTION F(X)

      REAL X, F
      
      F = X * COS(X)
      END


**PLOT******************************************************************
* Subroutine to plot the graph of a function Y = F(X) for X ranging    *
* from XMIN to XMAX; Y is allowed to range from YMIN to YMAX.  Local   *
* identifiers used are:                                                *
*     FNAME  : name of file containing the output                      *
*     HLIMIT,                                                          *
*     VLIMIT : parameters:  limits on the size of the plotting WINDOW  *
*     WINDOW : two-dimensional character array -- the graphics window  *
*     SYMBOL : plotting character -- represents a point on the graph   *
*     DELX   : X increment                                             *
*     DELY   : Y increment                                             *
*     X, Y   : a point on the graph                                    *
*     XLOC,                                                            *
*     YLOC   : location of a point in the window                       *
*     COUNT  : counts units on Y axis for labeling purposes            *
*                                                                      *
* Accepts:          F, XMIN, XMAX, YMIN, YMAX                          *
* Input (keyboard): FNAME                                              *
* Output (screen):  User prompt                                        *
* Output (file):    The plotting WINDOW                                *
************************************************************************

      SUBROUTINE PLOT(F, XMIN, XMAX, YMIN, YMAX)

      REAL F, XMIN, XMAX, YMIN, YMAX, DELX, DELY, X, Y
      INTEGER HLIMIT, VLIMIT, XLOC, YLOC
      PARAMETER (HLIMIT = 70, VLIMIT = 20)
      CHARACTER*1 WINDOW(0: HLIMIT, 0:VLIMIT), SYMBOL, FNAME*20
      PARAMETER (SYMBOL = '*')

      PRINT *, 'ENTER NAME OF FILE TO CONTAIN THE GRAPH'
      READ '(A)', FNAME
      OPEN (UNIT = 20, FILE = FNAME, STATUS = 'NEW')

      DELX = (XMAX - XMIN) / REAL(HLIMIT)
      DELY = (YMAX - YMIN) / REAL(VLIMIT)

      DO 20 YLOC = 0, VLIMIT
          DO 10 XLOC = 0, HLIMIT
              WINDOW(XLOC, YLOC) = ' '
10        CONTINUE
20    CONTINUE

* Turn on points in WINDOW corresponding to points on graph.

      X = XMIN
      DO 30 XLOC = 0, HLIMIT
          Y = F(X)
          IF (Y .GE. YMIN  .AND.  Y .LE. YMAX) THEN
              YLOC = NINT((Y - YMIN)/ DELY + 0.5)
              WINDOW(XLOC, YLOC) = SYMBOL
          END IF
          X = X + DELX
30    CONTINUE

* Draw the WINDOW in the file together with labeled Y axis.

      Y = YMAX
      COUNT = 5 * (VLIMIT / 5)
      DO 40 YLOC = VLIMIT, 0, -1
          IF (MOD(COUNT, 5) .EQ. 0) THEN
              WRITE(20, 100) Y, (WINDOW(XLOC,YLOC), XLOC = 0, HLIMIT)
100           FORMAT(1X, F8.2, ':', 200A)
          ELSE
              WRITE(20, 101) (WINDOW(XLOC,YLOC), XLOC = 0, HLIMIT)
101           FORMAT(9X, ':', 200A)
          END IF
          COUNT = COUNT - 1
          Y = Y - DELY
40    CONTINUE

* Draw a labeled X axis in the file.

      WRITE (20, 102), ('.', XLOC = 0, HLIMIT)
102   FORMAT(9X, 200A)
      WRITE(20, 103) (XMIN + XLOC*DELX, XLOC = 0, HLIMIT, 10)
103   FORMAT(3X, 50F10.3)

      END
