      PROGRAM TEMVOL
************************************************************************
* Program to read temperatures and volumes from a file containing      *
* time, temperature, pressure, and volume readings made by some        *
* monitoring device.  The temperature and volume measurements are      *
* displayed in tabular form, and the equation of the least squares     *
* line y = mx + b (x = temperature, y = volume) is calculated.         *
* Variables used are:                                                  *
*     FNAME   : name of data file                                      *
*     EOF     : end-of-file indicator                                  *
*     TEMP    : temperature recorded                                   *
*     VOLUME  : volume recorded                                        *
*     COUNT   : count of (TEMP, VOLUME) pairs                          *
*     SUMT    : sum of temperatures                                    *
*     SUMT2   : sum of squares of temperatures                         *
*     SUMV    : sum of volumes                                         *
*     SUMTV   : sum of the products TEMP * VOLUME                      *
*     TMEAN   : mean temperature                                       *
*     VMEAN   : mean volume                                            *
*     SLOPE   : slope of the least squares line                        *
*     YINT    : y-intercept of the line                                *
*                                                                      *
* Input (file):    Collection of temperature and volume readings       *
* Output (screen): Table of readings and equation of least squares     *
*                  line                                                *
************************************************************************

      INTEGER COUNT, EOF
      CHARACTER*20 FNAME
      REAL TEMP, VOLUME, SUMT, SUMT2, SUMV, SUMTV, TMEAN, VMEAN,
     +     SLOPE, YINT
      DATA COUNT, SUMT, SUMT2, SUMV, SUMTV /0, 4*0.0/


* Open the file as unit 15, set up the input and output
* formats, and print the table heading.

      PRINT *, 'ENTER NAME OF DATA FILE:'
      READ '(A)', FNAME
      OPEN (UNIT = 15, FILE = FNAME, STATUS = 'OLD')
100   FORMAT(4X, F4.1, T13, F4.1)
110   FORMAT(1X, A11, A10)
120   FORMAT(1X, F8.1, F12.1)
      PRINT *
      PRINT 110, 'TEMPERATURE', 'VOLUME'
      PRINT 110, '===========', '======'

* While there is more data, read temperatures and volumes,
* display each in the table, and calculate the necessary sums

      READ (UNIT = 15, FMT = 100, IOSTAT = EOF) TEMP, VOLUME
10    IF (EOF .GE. 0) THEN
          PRINT 120, TEMP, VOLUME
          COUNT = COUNT + 1
          SUMT = SUMT + TEMP
          SUMT2 = SUMT2 + TEMP ** 2
          SUMV = SUMV + VOLUME
          SUMTV = SUMTV + TEMP * VOLUME
          READ (UNIT = 15, FMT = 100, IOSTAT = EOF) TEMP, VOLUME
      GO TO 10
      END IF

* Find equation of least squares line

      TMEAN = SUMT / COUNT
      VMEAN = SUMV / COUNT
      SLOPE = (SUMTV - SUMT * VMEAN) / (SUMT2 - SUMT * TMEAN)
      YINT = VMEAN - SLOPE * TMEAN
      PRINT 130, SLOPE, YINT
130   FORMAT(//1X, 'EQUATION OF LEAST SQUARES LINE IS'
     +        /1X, '     Y =', F5.1, 'X + ', F5.1,
     +        /1X, 'WHERE X IS TEMPERATURE AND Y IS VOLUME')

      CLOSE (15)
      END

