! 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.1. (File input.) Count and total weights of male and female cats.
  program D01
    implicit none
    integer, parameter :: MALE = 1, LIMIT = 24
    integer :: Male_Count, Female_Count, Loop, Sex
    real :: Next_Weight, Male_Total, Female_Total
! start program D01
    Male_Count = 0
    Female_Count = 0
    Male_Total = 0.0
    Female_Total = 0.0
    open (unit = 1, file = "da.txt", status = "old", action = "read", position = "rewind")
    do Loop = 1, LIMIT
      read (unit = 1, fmt = *) Next_Weight
      read (unit = 1, fmt = *) Sex ! 1 for male, 2 for female
      if (Sex == MALE) then
        Male_Count = Male_Count + 1  ! This cat is male.
        Male_Total = Male_Total + Next_Weight
      else
        Female_Count = Female_Count + 1  ! This cat is female.
        Female_Total = Female_Total + Next_Weight
      end if
    end do
    write (unit = *, fmt = *) Male_Count, Male_Total, Female_Count, Female_Total
    stop
  end program D01
