Determining the Minimum and Maximum of Input Data

Problem Statement

Suppose we have a set of non-negative input integers terminated with a negative value. These input values are on separate lines. Write a program to determine the number of input data items, excluding the negative one at the end, and compute the minimum and the maximum. For example, the following input contains 7 data values with the seventh being negative. Of the six non-negative ones, the minimum and maximum are 2 and 9, respectively.

```5
3
9
2
7
4
-1
```

Solution

```! ------------------------------------------------------
! This program reads in a number of integer input until
! a negative one, and determines the minimum and maximum
! of the input data values.
! ------------------------------------------------------

PROGRAM  MinMax
IMPLICIT  NONE

INTEGER :: Minimum, Maximum          ! max and min
INTEGER :: Count                     ! # of data items
INTEGER :: Input                     ! the input value

Count = 0                            ! initialize counter
DO                                   ! for each iteration
READ(*,*) Input                   !   read in a new input
IF (Input < 0)  EXIT              !   if it is < 0, done.
Count = Count + 1                 !   if >= 0, increase counter
WRITE(*,*)  'Data item #', Count, ' = ', Input
IF (Count == 1) THEN              !   is this the 1st data?
Maximum = Input                !     yes, assume it is the
Minimum = Input                !     min and the max
ELSE                              !   no, not the 1st data
IF (Input > Maximum)  Maximum = Input    ! compare against the
IF (Input < Minimum)  Minimum = Input    ! existing min & max
END IF
END DO

WRITE(*,*)
IF (Count > 0) THEN                  ! if at one data item found
WRITE(*,*)  'Found ', Count, ' data items'
WRITE(*,*)  '  Maximum = ', Maximum
WRITE(*,*)  '  Minimum = ', Minimum
ELSE
WRITE(*,*)  'No data item found.' ! no data item found
END IF

END PROGRAM  MinMax
```
Click here to download this program.

Program Input and Output

If this program uses the input shown at the beginning, it will generate the following output:
```Data item #1 = 5
Data item #2 = 3
Data item #3 = 9
Data item #4 = 2
Data item #5 = 7
Data item #6 = 4

Found 6 data items
Maximum = 9
Minimum = 2
```

Discussion

• The data reading scheme is easy and will not be discussed. It has been discussed in an example of general DO.
• Variable Count is used for counting the number of data items.
• Once a non-negative item is read in, the value of Count is increased by 1 and the input value is displayed.
• If this is the first value (i.e., Count being 1), it is the maximum and the minimum value we have seen so far. Therefore, the value of Input is saved in Minimum and Maximum which hold the minimum and maximum values seen so far.
• If it is not the first input value, then Input is compared against Minimum and Maximum. If Input is less than Minimum, then the new value is less than the minimum so far and should become the new minimum. Likewise, if Input is greater than Maximum, then the new value is greater than the maximum so far and should become the new maximum. These activities are handled in the ELSE part.
• After all input are read, Count should be checked again to see if there is any input value at all. If there are, the count, minimum and maximum are displayed; otherwise, a message is displayed.