PROGRAM array2
  !// A program calculating centigrades from farenheit
  IMPLICIT NONE

  !// Declare the arrays for the temeratures in Farenheit and
  !// Centigrades
  REAL,DIMENSION(7,2)    :: farenheit
  REAL,DIMENSION(7,2)    :: centigrade
  !// Index variables
  INTEGER                :: i, j
  !// Character strings to hold the filenames
  CHARACTER(LEN=80)      :: filename
  CHARACTER(LEN=80)      :: farenheitfile
  CHARACTER(LEN=80)      :: centigradefile
  !// Constant values for the Logical Unit Number
  !// for referencing the files for opening, reading
  !// and writing
  INTEGER, PARAMETER     :: lun = 10
  INTEGER, PARAMETER     :: olun = 11
  !// A status variable to heold the result of file
  !// operations
  INTEGER                :: res
  !// External function(s)
  REAL, EXTERNAL         :: f2c
  REAL, EXTERNAL         :: c2f
  !// A character string for a prompt
  CHARACTER(LEN=80)      :: prompt
  CHARACTER              :: answer

  !// Give the input filename for Farenheit temp.
  farenheitfile = "farenheit.txt"
  !// Give the output filename for Centigrade temp.
  centigradefile = "centigrade.txt"

  !// Ask the user if we are converting from farenheit to
  !// centigrade or from centigrade to farenheit
  prompt = 'Convert from Farenheit to Centigrade (F/C)?'
  PRINT *, TRIM(prompt)
  READ(*,*) answer

  !// Check if the answer is F or f for Farenheit
  IF(answer .EQ. 'F' .OR. answer .EQ. 'f') THEN
    !// Yes, open the Farenheit input file
    OPEN(UNIT=lun,FILE=farenheitfile,FORM="FORMATTED",IOSTAT=res)
    !// Test if the operation was successful
    IF(res /= 0) THEN
      !// No, an error occurred, print a message to
      !// the screen
      PRINT *, "Error in opening file, status: ", res
      !// Stop the program
      STOP
    END IF

    !// Loop and read each line of the file
    DO i = 1, 7
      !// Read the current line
      READ(UNIT=lun,FMT='(F4.1,X,F4.1)',IOSTAT=res) farenheit(i,1), &
                                                    farenheit(i,2)
      !// Was the read successful?
      IF(res /= 0) THEN
        !// No, test if we have reached End Of File (EOF)
        IF(res /= -1) THEN
          !// No, an error has occurred, print a message
          PRINT *, "Error in reading file, status: ", res
          !// Close the file
          CLOSE(UNIT=lun)
          !// Stop the program
          STOP
        END IF
      END IF
    END DO
  ELSE
    !// No, open the Centigrade input file
    OPEN(UNIT=lun,FILE=centigradefile,FORM="FORMATTED",IOSTAT=res)
    !// Test if the operation was successful
    IF(res /= 0) THEN
      !// No, an error occurred, print a message to
      !// the screen
      PRINT *, "Error in opening file, status: ", res
      !// Stop the program
      STOP
    END IF

    !// Loop and read each line of the file
    DO i = 1, 7
      !// Read the current line
      READ(UNIT=lun,FMT='(F4.1,X,F4.1)',IOSTAT=res) centigrade(i,1), &
                                                    centigrade(i,2)
      !// Was the read successful?
      IF(res /= 0) THEN
        !// No, test if we have reached End Of File (EOF)
        IF(res /= -1) THEN
          !// No, an error has occurred, print a message
          PRINT *, "Error in reading file, status: ", res
          !// Close the file
          CLOSE(UNIT=lun)
          !// Stop the program
          STOP
        END IF
      END IF
    END DO
  END IF
  !// Close the input file
  CLOSE(UNIT=lun)

  !// Which way to convert ?
  IF(answer .EQ. 'F' .OR. answer .EQ. 'f') THEN
    !// Loop and convert from Farenheit to Centigrade
    DO j = 1, 2
      DO i = 1, 7
        centigrade(i,j) = f2c(farenheit(i,j))
      END DO
    END DO
  ELSE
    !// Loop and convert from Farenheit to Centigrade
    DO j = 1, 2
      DO i = 1, 7
        farenheit(i,j) = c2f(centigrade(i,j))
        !PRINT *, centigrade(i,j)
        !PRINT *, c2f(centigrade(i,j))
      END DO
    END DO
  END IF

  !// Which file to write to ?
  IF(answer .EQ. 'F' .OR. answer .EQ. 'f') THEN
    !// Open the Centigrade output file
    OPEN(UNIT=olun,FILE=centigradefile,FORM="FORMATTED",IOSTAT=res)
    !// Test if the operation was successful
    IF(res /= 0) THEN
      !// No, an error occurred, print a message to
      !// the screen
      PRINT *, "Error in opening output file, status: ", res
      !// Stop the program
      STOP
    END IF

    DO i = 1, 7
      WRITE(UNIT=olun,FMT='(F4.1,A1,F4.1)',IOSTAT=res) centigrade(i,1), ' ', &
                                                       centigrade(i,2)
      !// Test if the operation was successful
      IF(res /= 0) THEN
        !// No, an error occurred, print a message to
        !// the screen
        PRINT *, "Error in writing file, status: ", res
        !// Exit the loop
        EXIT
      END IF
    END DO
  ELSE
    !// Open the Farenheit output file
    OPEN(UNIT=olun,FILE=farenheitfile,FORM="FORMATTED",IOSTAT=res)
    !// Test if the operation was successful
    IF(res /= 0) THEN
      !// No, an error occurred, print a message to
      !// the screen
      PRINT *, "Error in opening output file, status: ", res
      !// Stop the program
      STOP
    END IF

    DO i = 1, 7
      WRITE(UNIT=olun,FMT='(F4.1,A1,F4.1)',IOSTAT=res) farenheit(i,1), ' ', &
                                                       farenheit(i,2)
      !// Test if the operation was successful
      IF(res /= 0) THEN
        !// No, an error occurred, print a message to
        !// the screen
        PRINT *, "Error in writing file, status: ", res
        !// Exit the loop
        EXIT
      END IF
    END DO

  END IF
  !// Close the output file
  CLOSE(UNIT=olun)
END PROGRAM array2
