-2
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

Text File

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.

Master
  • 59
  • 1
  • 2
  • 10
  • the "snippet" is not sufficient to see whats going on. How is `int_val` set? are `a,b` declared integer? What exactly are you reporting? – agentp Nov 18 '16 at 01:23
  • @agentp let me make a quick program that will help – Master Nov 18 '16 at 01:26
  • @agentp I just posted the code. Now, you should be able to help. – Master Nov 18 '16 at 01:38
  • you should have only one loop. as-is the other loops only see the last `int_val` from the first loop. – agentp Nov 18 '16 at 03:06
  • Could you help me with what to do? What you told me me doesn't help. I was expecting more help after creating a code from scratch so that I can put it here. – Master Nov 18 '16 at 03:10
  • 3
    Hi @Master, welcome on Stack Overflow. People here are doing any help in their free time Therefore they only answer questions they find interesting. You cannot be angry to anyone that they do not answer your question in detail. Providing a minimum working example is a necessity, not something extra. – Vladimir F Героям слава Nov 18 '16 at 10:30
  • You do not update int_val. Then there are max and min intrinsics - you don't need to write this code. Google usually helps: http://stackoverflow.com/questions/10371511/min-and-max-of-input-array-file-dat-with-subroutine – Chaosit Nov 18 '16 at 11:02

2 Answers2

1

What this

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

does is that it repeatedly reads one number, then it reads another number and overwrites the previous value and so on. In the end int_val contains the latest value read, because all previous values were overwritten by the later ones.

Therefore when you come here

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

int_val is just the last number that was read and that will be the minimum and the maximum.

You should either store the values in an array or do everything in one loop:

max_int = -(huge(max_int)-1)      !make initial max_int very small
min_int = huge(max_int)           !make initial min_int very big

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

  If (int_val > max_int) Then
      max_int = int_val
  END IF

  If (int_val < min_int) Then
      min_int = int_val
  END IF
END DO
0

Another way is use the intrinsic functions (MAXVAL, MAXLOC, MINVAL, MINLOC) and also to put all you values into an array or vector.

PROGRAM maxmin
IMPLICIT NONE

INTEGER                   :: openStatus, ioStatus     
INTEGER                   :: counter!, counter2, counter3                  
INTEGER, PARAMETER        :: MaxIn = 1000000             
INTEGER, DIMENSION(MaxIn) :: int_val                   
INTEGER                   :: numberOfInts             
INTEGER                   :: 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

!Test array size....
IF(numberOfInts > MaxIn) THEN
  WRITE (*, '(1X,A,I9,A,I7)') "Requested " , numberOfInts," However Array size=",MaxIn
  STOP
ENDIF

DO counter = 1, numberOfInts

  READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val(Counter)

  IF (ioStatus < 0) THEN
    WRITE (*, '(1X, A)')  "End of file"
    EXIT
  END IF

END DO

min_int = MINVAL(int_val(1:numberOfInts))
max_int = MAXVAL(int_val(1:numberOfInts))

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
Holmz
  • 714
  • 7
  • 14