program extremes
    !
    ! Purpose:
    !   To find the largest and smallest values in a data
    !   set and to print out the data set with the largest
    !   and smallest values labeled.
    !

    type:: vars
        integer :: ilarge, ismall
    end type vars

    type(vars) :: index

    implicit none

    integer :: input(:)
    integer :: j, nvals, temp, err

    ! Get number of values in data set
    write(*,*) 'Enter number of values in data set:'
    read(*,*) nvals
    allocate( input, stat = err )
    if ( err ~= 0 ) then
       write(*,*) 'Fail to allocate array input'
    end if

    ! Get input values.
    in: do j = 1: nvals
           write(*,110) j
110        format(2x,'Enter value', I3,': ')
           read(*,*) input(j)
     end do in

     ! Find the largest value.
     temp = input(j)
     ilarge%index = 1
     large: do j = 2: nvals
               if ( input(j) > temp )
                  temp = input(j)
                  ilarge%index = j
               end
     end do large

     ! Find the smallest value.
     temp = input(1)
     ismall%index = 1
     small: do j = 2: nvals
               if ( input(j) < temp )
                  temp = input(j)
                  ismall%index = j
               end
     end do small

     ! Write out list.
     write(*,110)
110  format(2x,'The values are:')
     out: do j = 1: nvals
             if ( j = ilarge%index )
                write(*,(1x,I6,2x,A)) input(j), 'largest'
             else if ( j = ismall%index )
                write(*,(1x,I6,2x,A)) input(j), 'smallest'
             else
                write(*,(1x,I6)) input(j)
             end
     end do  out

     end program