! Reads values for an array from a file, sorts with selection sort program sort implicit none integer, parameter :: maxsize = 10 real, dimension(maxsize) :: a logical :: exceed = .false. character(len=20) :: filename integer :: i integer :: iptr integer :: j 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 outer: do i = 1, nvals-1 iptr = i inner: do j = i+1, nvals minval: if (a(j) < a(iptr)) then iptr = j end if minval end do inner swap: if( i/= iptr) then temp = a(i) a(i) = a(iptr) a(iptr) = temp end if swap end do outer 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