C      Programmer: Shawn Bush
C      Course: CS 430
C      Date Due: 9/6/07
C      Language: FORTRAN
C      Compiler used: Force 2.0
C      Source Filename: PA1.f
C      Executable Filename: PA1.exe
C      Professor: Adams
C
C
C      PURPOSE:
C      To read in three lines of digits as input, divide them up into
C      several real numbers, and then average and sort them.
C
C      INPUT:
C      Three lines of 80 digits each.
C
C      OUTPUT:
C      The numbers read in, the average of those numbers, and
C      the numbers in sorted order. Also, text explaining what
C      the output is.
C
C      DEBUGGING MODIFICATIONS:
C      Modifications were made to the SORT subroutine, after debugging
C      and realizing that a TEMP variable was needed so as to not
C      overwrite other values.
C
C      GENERAL PROGRAM COMMENTS:
C      Implied do loops are used to read and write the lines of 80
C      digits. A function is used to return the average value of all
C      numbers in a dimension. A subroutine is used to sort the values
C      of a dimension in place. Insertion sort is used. Also, certain
C      values are incorrectly interpreted from the third line, and I
C      cannot explain why. e.g. 1234567890 is interpreted as
C      12345.67871
C
C      LANGUAGE COMMENTS:
C      Variables such as CARRIAGEONE and TEMP and AVGCARRIAGEONE do not
C      need to be declared as FORTRAN automatically recognizes them as
C      variables for real numbers because they begin with a letter NOT
C      in the integer variable name (I through N). The dimensions are
C      passed to the subroutine by reference, so no second array is
C      needed for the insertion sort, the values are changed in place.

C      Initialize the arrays for each line. Use first letter C because
C      the arrays will hold real numbers.
       DIMENSION CARRIAGEONE(15)
       DIMENSION CARRIAGETWO(10)
       DIMENSION CARRIAGETHREE(5)

C      Output to standard output (6) using format statement 5.
       WRITE (6, 5)

C      Use an implied do loop to read 15 real numbers of length 5
C      with 2 digits after the decimal into the first array. Use
C      format statement 21.
       READ (5,21) (CARRIAGEONE(I), I = 1,15)
       WRITE (6, 6)

C      Similar to the implied do loop above, just writing the read
C      numbers.
       WRITE (6, 31) (CARRIAGEONE(I), I = 1,15)

C      Call to AVG function, with the return value being stored in the
C      AVGCARRIAGEONE variable.
       AVGCARRIAGEONE = AVG (CARRIAGEONE, 15)
       WRITE (6, 41) AVGCARRIAGEONE

C      Call the insertion sort subroutine SORT. The subroutine will
C      change the values of the array in place.
       CALL SORT (CARRIAGEONE, 15)
       WRITE(6, 7)

C      Then display the sorted values again.
       WRITE(6, 31) (CARRIAGEONE(I), I = 1,15)
       
       WRITE (6, 5)
       READ (5,22) (CARRIAGETWO(I), I = 1,10)
       WRITE (6, 6)
       WRITE (6,32) (CARRIAGETWO(I), I = 1,10)
       AVGCARRIAGETWO = AVG (CARRIAGETWO, 10)
       WRITE (6, 42) AVGCARRIAGETWO
       CALL SORT (CARRIAGETWO, 10)
       WRITE (6, 7)
       WRITE (6,32) (CARRIAGETWO(I), I = 1,10)
       
       WRITE (6, 5)
       READ (5, 23) (CARRIAGETHREE(I), I = 1,5)
       WRITE (6, 6)
       WRITE (6, 33) (CARRIAGETHREE(I), I = 1,5)
       AVGCARRIAGETHREE = AVG (CARRIAGETHREE, 5)
       WRITE (6, 43) AVGCARRIAGETHREE
       CALL SORT (CARRIAGETHREE, 5)
       WRITE (6, 7)
       WRITE (6, 33) (CARRIAGETHREE(I), I = 1,5)


C      A list of formatting statements used throughout the program. The
C      forward slash is a new line, and the F means a floating point
C      real number is being printed.
   5   FORMAT(/'Please enter a string of 80 contiguous digits.')
   6   FORMAT(/'The numbers were read in as follows:')
   7   FORMAT(/'Here are the numbers sorted:')
  41   FORMAT(/'The average of those numbers is: ', F6.2)
  42   FORMAT(/'The average of those numbers is: ', F7.3)
  43   FORMAT(/'The average of those numbers is: ', F11.5)
  21   FORMAT(15F5.2)
  22   FORMAT(10F6.3)
  23   FORMAT(5F10.5)
  
  31   FORMAT(15(4(F7.2)/))
  32   FORMAT(10(4(F8.3)/))
  33   FORMAT(5(4(F12.5)/))
  
       STOP
       END

C      The SORT subroutine. The arguments are an array, and the amount
C      of elements in that array. The array is passed by reference, so
C      the insertion sort is performed in place without the need for
C      a temporary array.
       SUBROUTINE SORT(ARRAY, NUM)
       DIMENSION ARRAY(NUM)
       LOC = 1
  10   THELOW = ARRAY(LOC)
       DO 77 I = LOC,NUM,1
          IF (ARRAY(I) .LT. THELOW) GOTO 81
             GOTO 82
  81      TEMP = ARRAY(I)
          ARRAY(I) = THELOW
          THELOW = TEMP
  82      THELOW = THELOW
  77   CONTINUE
       ARRAY(LOC) = THELOW
       LOC = LOC + 1
       IF (LOC .GT. NUM) GOTO 83
          GOTO 10
  83   RETURN
       END
       
C      The AVG function simply adds all of the array elements into the
C      sum variable, then divides that by the amount of elements (NUM).
C      The result is then returned and stored in a variable in the main
C      program.
       FUNCTION AVG(ARRAY, NUM)
       DIMENSION ARRAY(NUM)
       SUM = 0
       DO 66 I = 1,NUM,1
          SUM = SUM + ARRAY(I)
  66   CONTINUE
       AVG = SUM / NUM
       RETURN
       END
