! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

! Example 5.26. Binary search in ordered array.
  program F26
    use F26M
    implicit none
    integer, parameter :: ARRAY_SIZE = 20, NAME_LENGTH = 20
    character (len = NAME_LENGTH), dimension(0: ARRAY_SIZE + 1) :: Data_Array
    character (len = NAME_LENGTH) :: X
    integer :: Loop, EoF
    integer, dimension(2) :: Ans
! start program F26
    open (unit = 1, file = "words.txt", status = "old", action = "read", position = "rewind")
    do Loop = 1, ARRAY_SIZE
      read (unit = 1, fmt = *, iostat = EoF) Data_Array(Loop)
      if (EoF < 0) then
        exit
      end if
    end do
    Data_Array(0) = " "
    Data_Array(Loop) = "zzzzzzzzzzzzzzzzzzzz"
    read (unit = *, fmt = *) X
    Ans = Search( Data_Array(: Loop - 1), X )
    write (unit = *, fmt = *) Data_Array(Ans(1) - 1), X, Data_Array(Ans(2) - 1)
    stop
  end program F26
