PROGRAM maxmin
IMPLICIT NONE
INTEGER :: openStatus, ioStatus
INTEGER :: counter, counter2, counter3
INTEGER :: numberOfInts
INTEGER :: int_val, max_int, min_int
OPEN (UNIT=12, FILE="INT.DAT", STATUS="OLD", ACTION="READWRITE", IOSTAT=openStatus)
IF (openStatus > 0) THEN
WRITE (*, '(1X, A)') "Can't open file"
STOP
END IF
WRITE (*, '(1X, A)') "How many integers do you want to read?"
READ (*, '(I6)') numberOfInts
DO counter = 1, numberOfInts
READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val
IF (ioStatus < 0) THEN
WRITE (*, '(1X, A)') "End of file"
EXIT
END IF
END DO
DO counter2 = 1, numberOfInts, 1
If (counter2 == 1) Then
max_int = int_val
END IF
If (int_val > max_int) Then
max_int = int_val
END IF
END DO
DO counter3 = 1, numberOfInts, 1
If (counter3 == 1) Then
min_int = int_val
END IF
If (int_val < min_int) Then
min_int = int_val
END IF
END DO
WRITE (*, '(1X, A)', ADVANCE = "NO") "The max is:"
WRITE (*, '(1X, I4)') max_int
WRITE (*, '(1X, A)', ADVANCE = "NO") "The min is:"
WRITE (*, '(11X, I4)') min_int
CLOSE (12)
END PROGRAM maxmin
From the image above, when i print the max and min values, they always print the last number in that text file, which is 7. I do not understand how to solve the problem.
I know the entire code is correct, but that max min part of the code is definitely not. I am so confused.