C      Programmer: Chad Parker

C      Course: CS 430

C      Date Due: September 12th, 2007

C      Language: FORTRAN

C      Compiler used: Force 2.0 (FORTRAN G77)

C      Source Filename: PA2.f

C      Executable Filename: None

C      Professor: Adams


C      Purpose: The purpose of this program is two-fold: 1) The program
C               will read in an integer and will return its binary value.
C               2) The program will read in a decimal number and will
C               return its binary value.

C      Input: The program will accept an integer value between 1 and 32767
C             for Part A and will accept a decimal value between 0 and 1
C             for Part B

C      Output: For Part A, the program will echo the integer value that
C              was inputed by the user. The program will also return the
C              binary value of the inputed integer. For Part B, the program
C              will echo the decimal value that was inputed by the user.
C              The program will then also return its binary value.

C              The output should be labeled as follows:

C              The base 10 value of the input is:  32767   (INTEGER)
C              The base 2 value of the input is: 111111111111111

C              The base 10 value of the input is: .125   (DECIMAL)
C              The base 2 value of the input is: .001

C              The base 10 value of the input is:  .124
C              The base 2 value of the input is:  .000111

C      Modifications: None

C      General Comments: The program should echo the converted decimal
C                        number to 6 places after the decimal if required

C                        32767 requires that the maximum number of binary
C                        (Base 2) digits to be 15

C


C      Language Comments: I could not figure out how to perform an integer
C      to binary (Base 2) conversion without using a do while loop. I
C      attempted to use a do loop but I could not correctly decrement
C      the dividend without using the do while loop

C      The second loop that begins on line 137 contains so many if statements
C      because I could not get the loop to work with GO TO statements

C      The two Format statements at lines 110 and 153 both represent each
C      binary (Base 2) digit as its own number. The numbers are printed in
C      order to give the appearance that they are a single number or
C      character string of digits.

C234567890

C      Creates the arrays to store the binary digits representing an
C      integer (INTTOBINARRAY) and a decimal (IDECTOBINARY) number
       DIMENSION INTTOBINARRAY(15)
       DIMENSION IDECTOBINARY(6)
       
       WRITE(6, 1)
    1  FORMAT(1X, 'Integer/Decimal Number to Binary Converter',/)

       WRITE (6, 2)
    2  FORMAT(1X, 'Enter an integer value between 1 and 32767:', /)

C      Reads in the initial integer value to be converted
       READ(5, 3) NINTEGER
    3  FORMAT(I5)

C      Echoes the initial integer value
       WRITE (6, 4) NINTEGER
    4  FORMAT(/,'The base 10 value of the input is:', I5, ' (INTEGER)')

C      Sets the inputed integer to ITEMP so that it can be manipulated
       ITEMP = NINTEGER
       INDEX = 15

C      This loop transforms an integer to a binary value
C      The loop runs until the dividend reaches 0 and the division
C      operation can no longer be performed. The final dividend before
C      the end of loop is 1
       DO WHILE (ITEMP.GT.0)

C      IREMAIN is equal to ITEMP%2
C      ITEMP is divided by two and the remainder is either 1 or 0
C      That value is then stored in the INTTOBINARRAY
          IREMAIN = (ITEMP - ((ITEMP/2) * 2))
          INTTOBINARRAY(INDEX) = IREMAIN

C      Sets ITEMP's new value to ITEMP/2, which moves the focus to the next
C      integer value that will be translated to its corresponding binary
C      (Base 2) digit
          ITEMP = ITEMP/2

C      Index is decreased by 1
          INDEX = (INDEX - 1)

       END DO

C      Prints out the binary (Base 2) value of the inputed integer
       WRITE(6, 5) (INTTOBINARRAY(I), I = (INDEX + 1), 15)
    5  FORMAT(1X, /, 'The base 2 value of the input is: ', 15(15(I1)))

    
C      Part B  - Enter a decimal number between 0 and 1. Return its binary
C                (Base 2) value.
       WRITE(6, 7)
    7  FORMAT(1X, /, 'Enter a decimal value between 0 and 1:', /)

C      Reads in a decimal value and assigns it to DECBINNUM
       READ(5, 8) DECBINNUM
    8  FORMAT(F7.6)

C      Echoes the inputed decimal value
       WRITE (6, 9) DECBINNUM
    9  FORMAT('The base 10 value of the input is: ',F7.6, ' (DECIMAL)')

C      Sets the inputed decimal to DTEMP so that it can be manipulated
       DTEMP = DECBINNUM

C      This loop examines if a the product of a decimal number times 2
C      is represented by a 1 or 0

C      If the product of 2 times a decimal number is greater than or equal
C      to 1, that decimal number will be represented by a 1 in binary (Base 2).
C      If the product is less than 1, then the decimal number is represented
C      by 0 in binary (Base 2)

       DO 13 I = 1, 6
          PRODUCT = (DTEMP * 2)
          
          IF((PRODUCT - 1).GE.0) IDECTOBINARY(I) = 1
   
          IF((PRODUCT - 1).LT.0) IDECTOBINARY(I) = 0

          IF(IDECTOBINARY(I).EQ.0) DTEMP = PRODUCT
          
          IF(IDECTOBINARY(I).EQ.1) DTEMP = (PRODUCT - 1)
   
   13  CONTINUE
C      END OF DO LOOP

C      Prints the binary (Base 2) value of the inputed decimal value
       WRITE (6, 12) (IDECTOBINARY(I), I = 1, 6)
   12  FORMAT(1X, /,'The base 2 value of the input is: 0.', 6(6(I1)))
    
       STOP
       END
C      End of program
