C      PROGRAMMER: Andrey Korsak
C      COURSE: CS 430
C      LANGUAGE: FORTRAN
C      COMPILER USED: FORCE 2.0
C      SOURCE FILENAME: BasePartB.f
C      EXECUTABLE FILENAME (IF ANY): BasePartB.f(NOT sumbmitted)
C      PROFESSOR: Professor Elizabeth S. Adams
C
C      DESCRIPTION OF THE PROGRAM'S PURPOSE:
C      Program which will read in a decimal number and print out
C      its base 2 representation.
C
C      DESCRIPTION OF THE PROGRAM'S REQUIRED INPUT:
C      Decimal number to be converted to base 2 fraction
C
C      DESCRIPTION OF THE PROGRAM'S EXPECTED OUTPUT:
C      * Original number echoed
C      * Base 2 fraction representing the original number
C      * Base 2 fraction will have at maximum 6 places
C
C      DESCRIPTION OF ANY MODIFICATIONS MADE IN DEBUGGING THE PROGRAM
C      None
C
C      GENERAL COMMENTS ABOUT THE PROGRAM
C      Algorithm to convert from decimal fraction to binary is:
C
C      step 1. multiply the fraction part by the base 2
C      step 2. keep the whole part of the result as the next digit
C              in the base 2 representation
C      step 3. (a) if decimal fraction terminates after multiplication
C                  then program outputs the binary number. We are done.
C              (b) continue to multiply the fraction part by 2 and
C                  recording the whole part of the result.
C
C      GENERAL COMMNETS EXPLAINING THE FEATURES OF THE LANGUAGE
C      * FORMAT(/, 1X, A,1X, F5.3) - Means that the output line will
C            begin with new line, then fill follow space, then a string
C            then another space, and finally a float with 3 digits after
C            the decimal point. The 5 is for '0.' part of fraction.
C
C      *INT(FRACTION) - The function INT returns the whole part of the
C            fraction and we are recording it in an array of integers.
C
C      * FRACTION = FRACTION - BINARY(I) - this part is to subtract the
C            whole part from multiplied fraction.
C
C      *(BINARY(I), I = 1, COUNT, 1) - This part is an implied do loop
C            which prints the binary fraction only to the 6 places or
C            if it terminated earlier then to the COUNT places.


       PROGRAM BASEPARTB
       
       INTEGER BINARY(6)
       REAL FRACTION
       INTEGER COUNT

       
C      SAY HELLO TO USER AND GIVE INSTRUCTIONS
       WRITE(6, 99) 'Enter decimal fraction such as 0.XXX:'
  99   FORMAT(1X, A)


C      READING THE INTEGER NUMBER FROM 1 TO 32767
       READ(5, 10) FRACTION
  10   FORMAT(F5.3)
 
C      ECHO THE ORIGINIAL NUMBER
       WRITE(6, 44) 'The base 10 value of the input is:', FRACTION
  44   FORMAT(/, 1X, A,1X, F5.3)

C      LOOP TO CONVERT DECIMAL INTO BINARY FRACTION. LOOPS STOPS AFTER
C      6 BINARY FRACTION PLACES HAVE BEEN OPTAINED
       DO 100 I = 1, 6, 1
       FRACTION = FRACTION * 2.0
       BINARY(I) = INT(FRACTION)
       FRACTION = FRACTION - BINARY(I)
       COUNT = I
C      TERMINATE THE LOOP IF THE FRACTION TERMINATES BEFORE 6 PLACES
       IF (FRACTION .LT. 0.000001) GOTO 101
 100   CONTINUE

C      OUTPUT THE BASE 2 VALUE OF THE DECIMAL FRACTION
 101   WRITE(6, 57) 'The base 2 value of the input is: 0.', (BINARY(I),
     6 I = 1, COUNT, 1)
  57   FORMAT(1X, A, 6(6(I1)),/)
  
       WRITE(6, 98) 'Goodbye!'
  98   FORMAT(/, 1X, A)
  
       STOP
       END
