C      Programmer: Dan Gritsko

C      Course: CS 430

C      Date Due: September 13, 2007

C      Language: FORTRAN

C      Compiler used: FORCE2.08

C      Source Filename: Program2Fractions.f

C      Executable Filename (if any) or None: None

C      Professor: Elizabeth Adams


C      Purpose: This program takes a single base 10 fraction as input
C         (represented as a decimal) from somewhere in the range of
C         0 to 1 and converts this number to a binary fraction with up
C         to six places after the decimal point. This fractional binary
C         value is then displayed on the screen with an appropriate label.

C      Input: This program takes a single input from the user: any
C         base 10 fraction (as a decimal) from 0 to 1. If the input
C         entered by the user is not a string of digits, the program
C         will display an error message and terminate prematurely.
C         The program will also display a message and terminate if the
C         input is out of range.

C      Output: This program will output both the original (base 10)
C         fraction that was input by the user, as well as the converted
C         (base 2) representation of this decimal value, up to 6 places.

C      General Comments: Converting the base 10 fraction to base 2 was
C         fairly simple. I used the method we used in class - multiplying
C         the number by 2 and appending a zero to a string of digits if the
C         result was less than one, and appending a one to the string if
C         the result was greater than or equal to one.
C         Note that when the original decimal fraction is displayed, some extra
C         trailing zeroes may be output (depending on how many digits
C         were in the input). This will not affect the conversion in any way.

C      Language Information:
C         All the variables in this program are of type character, type
C         type integer, or type real. All variables of type character are
C         declared using the word CHARACTER. All other declarations are
C         unnecessary, since variable names beginning with I-N are
C         assumed by the compiler to be of data type integer and all other
C         variables are assumed to be of type real.
C         = is the assignment operator.
C         + is the addition operator.
C         - is the subtraction operator.
C         * is the multiplication operator.
C         DIMENSION statement is used to define arrays.
C         DO statement is used to declare DO loops. First value after DO
C            statement is the label for the end of the loop. Next value
C            is the name of the loop control value. Next comes an equals
C            sign, then the start value of the loop control value followed
C            by a comma. Next is the end value for the loop control value,
C            followed by a second comma. Finally, the modification value
C            defines how much the loop control value should be changed
C            after each iteration of the loop.
C         CONTINUE statement is non-executable and is used in conjunction
C            with a label to mark the end of a DO loop.
C         WRITE statement is the output statement.
C         6 in WRITE(6,_) is the default output device.
C         The numbers after 6, in the WRITE statements are labels for
C            FORMAT statements.
C         READ statement is the input statement.
C         5 in READ(5,_) is the default input device.
C         The numbers after 5, in the READ statements are lables for
C            FORMAT statements.
C         FORMAT statements are labeled and non-executable, and can
C            therefore be placed anywhere in the program.
C         All WRITE statements should have a 1X as the first element in
C            the format list.
C         The A format in a format list indicates a string of characters
C            is to be output, the F format in a format list indicates
C            a real number is to be output.
C         The 1st number after the F in the FORMAT statement indicates
C            the 10 width of the variable.
C         The 2nd number after the F in the FORMAT statement indicates
C            number of places after the decimal point.
C         STOP statement ends the program execution.
C         END statement marks the end of a compilation unit.

C234567890

C       Declare an array of characters to store the binary representation
C       of the base 10 fraction. The array has 6 indices because the program
C       is required to display up to 6 places of the binary fraction.
        CHARACTER BINFRAC(6)

C       Prompt the user for a base 10 fraction.
        WRITE(6,10) 'Enter a base 10 fraction (in decimal format):'
        
C       Read in a base 10 fraction from the keyboard, and go to label 30 if
C       there was an error in the input (usually happens when characters
C       other than 0-9 were entered).
        READ(5,20,ERR = 30) A

C       Check to ensure that the input was within the correct range. If
C       it was, go to the next portion of the program. If it wasn't,
C       display an error message, then go to the end of the program.
        IF ((A .GT. 0) .AND. (A .LT. 1)) GOTO 1
        WRITE(6,10) 'Input must be within range of 0 to 1'
        GOTO 99
        
C       Write the original base 10 fraction back to the screen.
   1    WRITE(6,11) A

C       Create a binary representation of the base 10 fraction by filling
C       the BINFRAC array with 0's and 1's.
        DO 100 L = 1,6
C          First, set the current index of the array to a blank space.
           BINFRAC(L) = ' '
C          Then, double the value of A.
           A = A * 2
C          Deal with the special case: A = 0. Either 0 was input by the user,
C          or the conversion is finished.
           IF (A .EQ. 0) GO TO 102
           IF (A .LT. 1) GO TO 101
C          Set the current index of the array to the character '1', then
C          subtract 1 from A and go to the next iteration of the loop.
           BINFRAC(L) = '1'
           A = A - 1
           GO TO 100
C          If A = 0 and the current iteration of the loop is greater than
C          1, go to the next iteration of the loop.
  102      IF (L .GT. 1) GO TO 100
C          Set the current index of the array to the character '0'
  101      BINFRAC(L) = '0'
  100   CONTINUE
  

C       Write the contents of the array to the screen; it should now
C       be a string of 0s, 1s, and blank spaces which represent the
C       converted base 10 fraction.
        WRITE(6,12) BINFRAC
        
C       If the program reaches this point, it has executed without
C       any errors and should skip to the end of the program.
        GO TO 99
        
C       Error message which will be displayed if the user enters incorrect
C       input (non-digit characters).
   30   WRITE(6,10) 'Had an error in the input data'

C       Output format statements.
   10   FORMAT(1X, A50)
   11   FORMAT(1X, 'The base 10 value of the input is: ', F7.5)
   12   FORMAT(1X, 'The base  2 value of the input is: 0.', 6(A1))

C       Input format statement for reading in the base 10 fraction.
   20   FORMAT(F7.0)

C       Stop the execution of the program.
   99   STOP
        END
