PROGRAM Temperature_Volume_Readings
!-----------------------------------------------------------------------
! 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:
!   FileName         : name of data file
!   OpenStatus       : status variable for OPEN statement
!   InputStatus      : status variable for READ statement
!   Temperature      : temperature recorded
!   Volume           : volume recorded
!   Count            : count of (Temperature, Volume) pairs
!   SumOfTemps       : sum of temperatures
!   SumOfTemps2      : sum of squares of temperatures
!   SumOfVols        : sum of volumes 
!   SumOfProds       : sum of the products Temperature * Volume
!   MeanTemperature  : mean temperature
!   MeanVolume       : mean volume
!   Slope            : slope of the least-squares line
!   Y_Intercept      : y-intercept of the line
!
! Input (file):    Collection of temperature and volume readings 
! Output (screen): Table of readings and equation of least squares 
!                  line
!-----------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER :: Count = 0, OpenStatus, InputStatus
  CHARACTER(20) :: FileName
  REAL :: Temperature, Volume, SumOfTemps = 0.0 , SumOfTemps2 = 0.0, &
          SumOfVols = 0.0, SumOfProds = 0.0, MeanTemperature, &
          MeanVolume, Slope, Y_Intercept

  ! Open the file as unit 15, set up the input and output
  ! formats, and display the table heading

  WRITE (*, '(T2, A)', ADVANCE = "NO") "Enter name of data file: "
  READ *, FileName
  PRINT *, fILENAME
  OPEN (UNIT = 15, FILE = FileName, STATUS = "OLD", IOSTAT = OpenStatus)
  IF (OpenStatus > 0) STOP "*** Cannot open the file ***"

  100 FORMAT(T5, F4.1, T13, F4.1)
  110 FORMAT(' ', A11, A10)
  120 FORMAT(' ', 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

  DO
    READ (UNIT = 15, FMT = 100, IOSTAT = InputStatus) Temperature, Volume
    IF (InputStatus > 0) STOP "*** Input error ***"
    IF (InputStatus < 0) EXIT  ! end of file

    PRINT 120, Temperature, Volume
    Count = Count + 1
    SumOfTemps = SumOfTemps + Temperature
    SumOfTemps2 = SumOfTemps2 + Temperature ** 2
    SumOfVols = SumOfVols + Volume
    SumOfProds = SumOfProds + Temperature * Volume
  END DO

  ! Find equation of least-squares line

  MeanTemperature = SumOfTemps / REAL(Count)
  MeanVolume = SumOfVols / REAL(Count)
  Slope = (SumOfProds - SumOfTemps * MeanVolume) / &
          (SumOfTemps2 - SumOfTemps * MeanTemperature)
  Y_Intercept = MeanVolume - Slope * MeanTemperature

  PRINT 130, Slope, Y_Intercept
  130 FORMAT(//' ', "Equation of least-squares line is"  &
             /' ', "     y =", F5.1, "x + ", F5.1,  &
             /' ', "where X is temperature and y is volume")

  CLOSE (15)
  STOP
END PROGRAM Temperature_Volume_Readings
