C      PROGRAMMER: Andrey Korsak
C      COURSE: CS 430
C      LANGUAGE: FORTRAN
C      COMPILER USED: FORCE 2.0
C      SOURCE FILENAME: readlines.f (submitted as: korsak01.f)
C      EXECUTABLE FILENAME (IF ANY): readlines.exe (none submitted)
C      PROFESSOR: Professor Elizabeth S. Adams
C
C      DESCRIPTION OF THE PROGRAM'S PURPOSE:
C      Program is to get input from user consisting of three lines of
C      80 numbers, sort them, average them, and output back
C      to the user.
C
C      DESCRIPTION OF THE PROGRAM'S REQUIRED INPUT:
C      The input will be three lines with 80 numbers.
C      The first line of input data represents fifteen (15) five-digit
C      numbers with two of the digits after the decimal point.
C      The second line of data represents ten  (10) six-digit numbers
C      with three of the digits after the decimal point.  The third
C      line of data represents five (5) ten-digit numbers with five
C      of the digits after the decimal point.
C
C      DESCRIPTION OF THE PROGRAM'S EXPECTED OUTPUT:
C      The output will be:
C       1) Original numbers
C       2) Sorted numbers in descending order
C       3) Average of numbers
C      This pattern will be used for displaying all of the 3 lines.
C
C      DESCRIPTION OF ANY MODIFICATIONS MADE IN DEBUGGING THE PROGRAM
C      None
C
C      GENERAL COMMENTS ABOUT THE PROGRAM
C      I descriped the basic features of the language used below.
C      there are some comments in the code to make it more readable.
C      this program contains one subroutine and one function.
C
C      GENERAL COMMNETS EXPLAINING THE FEATURES OF THE LANGUAGE
C      * READ (5, 10) (LINE1(I), I = 1, 15) - this statements reads the
C               data from user into an array. It uses implied do loop
C               to accomplish that. In addition to that it uses format
C               label 10. The meaning explained below
C      * FORMAT (15F5.2) - this statements allows to format the input,
C               this particular means that 15 floats will be read and
C               that each number will have 5 digits with two of them
C               after the floating point.
C      * WRITE (6, 11) (LINE1(I), I = 1, 15) - this statement writes
C               data in the array to the default output device number 6
C               and it uses label 11 to format that output.
C               This also uses implied do loop to write numbers instead
C               of reading them.
C      * CALL BUBBLE_SORT(LINE2, 10) - this statements calls the
C               function BUBBLE_SORT with two parameters LINE2(an array)
C               and 10 which is the size of the array.
C      * FORMAT - this statement used to format the output and intput.
C               For input this program uses conditions to format input
C               to requirements listed above. For ouput this program
C               uses 'A' format to output characters, 'F' format to
C               output float numbers with number right after it such
C               as 5.2 which means that there will be 5 digits total and
C               that two of them will be after decimal point.
C      * DO 77, X = 1, SIZE, 1 - this statements is a "do" loop.
C               first number 77 indicates the label with continue
C               statement to mark where loop ends. X=1 means that
C               counter variables will be 1. SIZE variable means to
C               which number the loop will count up. Finally number 1
C               indicates what is the incremental value is.
C      * IF (NUMBERS(Y) .LT. NUMBERS(Y+1)) THEN - this statements is
C               an example of if statemen. The condition in the
C               parentheses evaluates to true when left side of .LT. is
C               less that the right side. The THEN keyword means that
C               multiple statements are in this if condition so
C               compiler looks for keyword END to end the if.
C      * '' - the empty string in the WRITE statement are used to
C               space out the output to make it more readable.

       PROGRAM READLINES
       
C      DECLARING THE ARRAYS OF REAL NUMBERS TO BE FILLED WITH INPUT
       REAL LINE1(15)
       REAL LINE2(10)
       REAL LINE3(5)

C      WRITTING THE HELLO MESSAGE
       WRITE(6, 99) "WELCOME. PLEASE ENTER 3 LINES WITH 80 NUMBERS"
       
C      READING THE FIRST LINE
       READ (5, 10) (LINE1(I), I = 1, 15)
 10    FORMAT (15F5.2)
C      READING THE SECOND LINE
       READ (5, 20) (LINE2(I), I = 1, 10)
 20    FORMAT (10F6.3)
C      READING THE THIRD LINE
       READ (5, 30) (LINE3(I), I = 1, 5)
 30    FORMAT (5F10.5)
 

C      PROCESSING EACH LINE SEPARATELY

C      --FIRST LINE--
C      PRINT THE ORIGINAL INPUT ON LINE 1
       WRITE (6, 99) '','', "ORIGINAL INPUT ON LINE 1: "
       WRITE (6, 11) (LINE1(I), I = 1, 15)
       
C      PRINT THE SORTED INPUT ON LINE 1
       WRITE (6, 99) "SORTED INPUT ON LINE 1: "
       CALL BUBBLE_SORT(LINE1, 15)
       WRITE (6, 11) (LINE1(I), I = 1, 15)
       
C      PRINT THE AVERAGE OF SORTED NUMBERS
       WRITE (6, 100) "THE AVERAGE OF NUMBERS IS: ",AVERAGE(LINE1,15),''


C      --SECOND LINE--
C      PRINT THE ORIGINAL INPUT ON LINE
       WRITE (6, 99) '', "ORIGINAL INPUT ON LINE 2: "
       WRITE (6, 22) (LINE2(I), I = 1, 10)

C      PRINT THE SORTED INPUT ON LINE
       WRITE (6, 99) "SORTED INPUT ON LINE 2: "
       CALL BUBBLE_SORT(LINE2, 10)
       WRITE (6, 22) (LINE2(I), I = 1, 10)

C      PRINT THE AVERAGE OF SORTED NUMBERS
       WRITE (6, 102) "THE AVERAGE OF NUMBERS IS: ",AVERAGE(LINE2,10),''
       
       
C      --THIRD LINE--
C      PRINT THE ORIGINAL INPUT ON LINE
       WRITE (6, 99) '', "ORIGINAL INPUT ON LINE 3: "
       WRITE (6, 33) (LINE3(I), I = 1, 5)

C      PRINT THE SORTED INPUT ON LINE
       WRITE (6, 99) "SORTED INPUT ON LINE 3: "
       CALL BUBBLE_SORT(LINE3, 5)
       WRITE (6, 33) (LINE3(I), I = 1, 5)

C      PRINT THE AVERAGE OF SORTED NUMBERS
       WRITE (6, 103) "THE AVERAGE OF NUMBERS IS: ",AVERAGE(LINE3,5),''

C      SAY GOODBYE TO USER. PROGRAM ENDED.
       WRITE (6, 99) '', "GOODBYE!"

C      DIFFERENT FORMAT STATEMENTS FOR DESCTIPTION LOOK IN THE HEADER.
 99    FORMAT(1X, A)
 100   FORMAT(1X, A, F7.2)
 102   FORMAT(1X, A, F8.3)
 103   FORMAT(1X, A, F12.5)
 
 11    FORMAT(1X, 15(4(F7.2, 1X)/1X))
 22    FORMAT(1X, 10(4(F8.3, 1X)/1X))
 33    FORMAT(1X, 5(4(F12.5, 1X)/1X))
       
       STOP
       END
       
C      END OF MAIN PROGRAM BODY. BELOW IS FUNCTIONS AND SUBROUTINES.

C      SUBROUTINE TO SORT THE NUMBER IN A SINGLE ARRAY. BUBBLE SORT.
       SUBROUTINE BUBBLE_SORT(NUMBERS, SIZE)

C      DECLARING THE TYPE OF VARIABLES WE ARE USING
       INTEGER X, Y, SIZE
       REAL NUMBERS(SIZE)
       REAL TEMP

C      FIRST LOOP
       DO 77, X = 1, SIZE, 1
C      SECOND LOOP
       DO 88, Y = 1, SIZE - 1, 1

C      SWAP NUMBERS
       IF (NUMBERS(Y) .LT. NUMBERS(Y+1)) THEN
       TEMP = NUMBERS(Y+1)
       NUMBERS(Y+1) = NUMBERS(Y)
       NUMBERS(Y) = TEMP
       END IF
       
 88    CONTINUE
 77    CONTINUE
       RETURN
       END
C      END OF BUBBLE_SORT
       
       
C      FUNCTION THAT CALCULATES AVERAGE
       FUNCTION AVERAGE(INPUT_ARRAY, LENGTH)
       
C      DECLARING THE TYPES OF VARIABLES WE ARE USING
       REAL INPUT_ARRAY(LENGTH)
       INTEGER LENGTH

C      INITIALIZING THE AVERAGE
       AVERAGE = 0

C      DO LOOP TO ADD ALL THE NUMBERS UP
       DO 101, I = 1, LENGTH, 1
       AVERAGE = AVERAGE + INPUT_ARRAY(I)
 101   CONTINUE

C      FIND THE AVERAGE FROM THE SUM ABOVE.
       AVERAGE = AVERAGE / LENGTH
       
       RETURN
       END
C      END OF FUNCTION AVERAGE

