program sort3 implicit none integer, parameter :: maxsize = 10 real, dimension(maxsize) :: a logical :: exceed = .false. character(len=20) :: filename integer :: i integer :: nvals = 0 integer :: status real :: temp write(*, 1000) 1000 format (1x, 'Enter the name of the file: ') read (*, '(A20)') filename open (unit = 9, file = filename, status='old', action='read', & iostat=status) fileopen: if (status==0) then do read(9, *, iostat=status) temp if (status /= 0) exit nvals = nvals+1 size: if (nvals <= maxsize) then a(nvals) = temp else exceed = .true. end if size end do toobig: if (exceed) then write(*,1020) nvals, maxsize 1020 format('Max array size exceeded: ', I6, '>', I6) else toobig call sort(a, nvals) write(*, '(A)a') 'The sorted data:' write(*, 1040) (a(i),i=1, nvals) 1040 format (4x, f10.4) end if toobig else fileopen write(*,1050) status 1050 format(1x, 'File open failed-status = ', I6) end if fileopen end program subroutine sort(arr, n) implicit none integer, intent(in) :: n real, dimension(n), intent(inout) :: arr integer :: i integer :: iptr integer :: j real :: temp outer: do i = 1, n -1 iptr = i inner: do j = i+1, n minval: if (arr(j) < arr(iptr)) then iptr = j end if minval end do inner swap: if( i/= iptr) then temp = arr(i) arr(i) = arr(iptr) arr(iptr) = temp end if swap end do outer end subroutine sort