      PROGRAM PGM10
************************************************************************
*    PROGRAMMER: B. Alex Bridges
*    CLASS:      9:00AM
*    PROGRAM:    PGM10.F
*    DUE DATE:   03/18/99
*
* Program Description:
*    This program reads a list of numbers from a data file.  It then
*    sorts that list of numbers in ascending sequence.
* Variables:
*    NAMEF   : student's first name
*    NAMEL   : student's last name
*    LIMIT   : max. amount of numbers
*    NUMBERS : list of numbers
*    MEDIAN  : index of median value
* Input  : NAMEF,NAMEL,NUMBERS
* Output : NUMBERS
************************************************************************

        CHARACTER*5 NAMEF,NAMEL*8
        PARAMETER (LIMIT = 20)
        INTEGER NUMBERS(LIMIT)

* open the data + output file
        OPEN (UNIT=12, FILE="pgm10.dat")
        OPEN (UNIT=13, FILE="pgm10.out")

* read in values for name
        READ (12, *), NAMEF,NAMEL

* write the header
c       PRINT '("STUDENT NAME: ",A,A,/,
c    1          "CLASS:        9:00",//,
c    2          T30,"PROGRAM - PGM09.F",/)', NAMEF,NAMEL
        WRITE (13, '("STUDENT NAME: ",A,A,/,
     1               "CLASS:        9:00",//,
     2               T30,"PROGRAM - PGM10.F",/)') NAMEF,NAMEL

* read in and echo input
        CALL INPUT (NUMBERS,LIMIT)

* sort the list of numbers
        CALL SORT (NUMBERS,LIMIT)

* write output
        CALL OUTPUT (NUMBERS,LIMIT)

* determine and write median
*       => even amount of numbers
        IF (MOD(LIMIT,2) .EQ. 0) THEN
          MEDIAN = LIMIT / 2
*       => odd amount of numbers
        ELSE
          MEDIAN = (LIMIT / 2) + 1
        END IF
c       PRINT '("Median = ",I4,/)', NUMBERS(MEDIAN)
        WRITE (13, '("Median = ",I4,/)') NUMBERS(MEDIAN)

* write end of program message and close the data file
c       PRINT '(/,T28,"End of program - PGM10.F",/)'
        WRITE (13, '(/,T28,"End of program - PGM10.F",/)')
        CLOSE(12)
        CLOSE(13)

        END

************************************************************************

************************************************************************
* Subroutine: INPUT                                                    *
* Purpose: Reads in and echos list of numbers.                         *
* Input: --PARAMETERS--                                                *
*        => 'NUMBERS' = List of numbers.                               *
*        => 'LIMIT'   = Max. amount of numbers.                        *
* Output: --PARAMETERS--                                               *
*        => 'NUMBERS' = List of numbers.                               *
************************************************************************
        SUBROUTINE INPUT (NUMBERS,LIMIT)

        INTEGER NUMBERS(LIMIT),LIMIT,I

c       PRINT '("Input Data",/,
c    1          "==========")'
        WRITE (13, '("Input Data",/,
     1               "==========")')
        DO 10 I = 1, LIMIT
          READ (12, *), NUMBERS(I)
c         PRINT '(T5,I2,T15,I5)', I,NUMBERS(I)
          WRITE (13, '(T5,I2,T15,I5)') I,NUMBERS(I)
 10     CONTINUE
c       PRINT '()'
        WRITE (13, '()')

        END

************************************************************************
* Subroutine: SORT                                                     *
* Purpose: Sorts a list of numbers via insertion sort algorithm.       *
* Input: --PARAMETERS--                                                *
*        => 'NUMBERS' = List of numbers.                               *
*        => 'LIMIT'   = Max. amount of numbers.                        *
* Output: --PARAMETERS--                                               *
*        => 'NUMBERS' = Sorted list of numbers in ascending sequence.  *
************************************************************************
        SUBROUTINE SORT (NUMBERS,LIMIT)

        INTEGER NUMBERS(LIMIT),LIMIT,I,J,SMALL,LOCSM

        DO 11 I = 1,LIMIT - 1
*         => find smallest item in sublist NUMBERS(I)...NUMBERS(LIMIT-1)
          SMALL = NUMBERS(I)
          LOCSM = I
          DO 10 J = I + 1,LIMIT
*         => smaller item found
            IF (NUMBERS(J) .LT. SMALL) THEN
              SMALL = NUMBERS(J)
              LOCSM = J
            END IF
 10       CONTINUE

*         => interchange smallest item with NUMBERS(I)
*            at beginning of sublist
          NUMBERS(LOCSM) = NUMBERS(I)
          NUMBERS(I) = SMALL
 11     CONTINUE

        END

************************************************************************
* Subroutine: OUTPUT                                                   *
* Purpose: Writes list of numbers.                                     *
* Input: --PARAMETERS--                                                *
*        => 'NUMBERS' = List of numbers.                               *
*        => 'LIMIT'   = Max. amount of numbers.                        *
* Output: --PARAMETERS--                                               *
*        => 'NUMBERS' = List of numbers.                               *
************************************************************************
        SUBROUTINE OUTPUT (NUMBERS,LIMIT)

        INTEGER NUMBERS(LIMIT),LIMIT,I

c       PRINT '("Sorted List",/,
c    1          "===========")'
        WRITE (13, '("Sorted List",/,
     1               "===========")')
        DO 10 I = 1, LIMIT
c         PRINT '(T5,I2,T15,I5)', I,NUMBERS(I)
          WRITE (13, '(T5,I2,T15,I5)') I,NUMBERS(I)
 10     CONTINUE
c       PRINT '()'
        WRITE (13, '()')

        END