! 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 3.7. (File input.) Count large males, small males, and females.
  program D07
    implicit none
    integer, parameter :: LIMIT = 24
    integer :: Hi_Male_Count, Low_Male_Count, Female_Count, Loop
    real, parameter :: BOUNDARY = 4.3  ! Boundary for small vs. large
    real :: Male_Total, Female_Total, Next_Weight
    character (len = 1) :: Sex
! start program D07
    Hi_Male_Count = 0
    Low_Male_Count = 0
    Female_Count = 0
    Male_Total = 0.0
    Female_Total = 0.0
    open (unit = 1, file = "d07.txt", status = "old", action = "read", position = "rewind")
    do Loop = 1, LIMIT
      read (unit = 1, fmt = *) Sex ! M or F
      if ((Sex == "M") .or. (Sex == "m")) then  ! Cat is male.
        read (unit = 1, fmt = *) Next_Weight
        Male_Total = Male_Total + Next_Weight
        if (Next_Weight > BOUNDARY) then
          Hi_Male_Count = Hi_Male_Count + 1
        else
          Low_Male_Count = Low_Male_Count + 1
        end if
      else  ! Cat is female if Sex is not "M" or "m"
        read (unit = 1, fmt = *) Next_Weight
        Female_Total = Female_Total + Next_Weight
        Female_Count = Female_Count + 1
      end if
    end do
    write (unit = *, fmt = *) Hi_Male_Count, Low_Male_Count, Male_Total
    write (unit = *, fmt = *) Female_Count, Female_Total
    stop
  end program D07
