      PROGRAM CORN
************************************************************************
* Program to find the average yield for each of several hybrids of     *
* corn, using data consisting of a hybrid code and yield obtained from *
* tests of these hybrids on several test plots.  Identifiers used are: *
*     FNAME  : name of data file containing the corn-yield data        *
*     LIMHYB : parameter specifying limit on types of hybrids          *
*     NUMHYB : number of hybrids being tested                          *
*     COUNT  : COUNT(I) = number of tests of hybrid I                  *
*     TYIELD : TYIELD(I) = sum of yields for hybrid I                  *
*     HYBRID : current hybrid code                                     *
*     YIELD  : current yield being processed                           *
*                                                                      *
* Input:  Pairs of hybrid codes and yields (stored in HYBRID and YIELD)*
* Output: For each hybrid, the average yield or a message that no test *
*         results were reported for that particular hybrid             *
************************************************************************

      CHARACTER*10 FNAME
      INTEGER LIMHYB
      PARAMETER (LIMHYB = 4)
      INTEGER COUNT(LIMHYB), HYBRID
      REAL TYIELD(LIMHYB), YIELD
      DATA COUNT, TYIELD/LIMHYB*0, LIMHYB*0.0/

*     Get the name of the the file containing the corn-yield data
*     and open it for input

      PRINT *, 'THIS PROGRAM READS PAIRS OF HYBRID CODES AND YIELDS '
      PRINT *, 'FROM A DATA FILE.  THE FIRST LINE OF THE FILE CONTAINS'
      PRINT *, 'THE NUMBER OF HYBRIDS.  EACH SUBSEQUENT LINE CONTAINS'
      PRINT *, 'A HYBRID CODE AND A YIELD.  THE LAST LINE OF THE FILE'
      PRINT *, 'CONTAINS THE PAIR 0, 0 TO SIGNAL THE END OF DATA.'
      PRINT *
      PRINT *, 'ENTER THE NAME OF THE DATA FILE TO BE USED: '
      READ '(A)', FNAME
      OPEN (UNIT = 10, FILE = FNAME, STATUS = 'OLD')

*     Read number of hybrids from the first line of the file
*     and the first hybrid code and yield from the next line

      READ (10, *) NUMHYB
      READ (10, *), HYBRID, YIELD

* While there is more data, read a hybrid code and yield,
* increment the appropriate counter, and add the yield to the
* appropriate sum

 20   IF (YIELD .GT. 0) THEN
         IF (HYBRID .LE. NUMHYB) THEN
            COUNT(HYBRID) = COUNT(HYBRID) + 1
            TYIELD(HYBRID) = TYIELD(HYBRID) + YIELD
         ELSE
            PRINT *, '*** ILLEGAL HYBRID CODE:', HYBRID
         END IF
         READ (10, *), HYBRID, YIELD
      GO TO 20
      END IF
      
*     Calculate and print average yields
      
      PRINT 100
 100  FORMAT(//1X, 'FOR HYBRID')
      DO 30 HYBRID = 1, NUMHYB
         IF (COUNT(HYBRID) .GT. 0) THEN
            PRINT 101, HYBRID, TYIELD(HYBRID) / REAL(COUNT(HYBRID))
 101        FORMAT(1X, I10, ':  AVERAGE YIELD IS', F6.2)
         ELSE
            PRINT 102, HYBRID
 102        FORMAT(1X, I10, ':  THERE WERE NO TEST RESULTS REPORTED')
         END IF
 30   CONTINUE

      END
