C23456
C      Programmer: Tyler Badorrek

C      Course: CS 430

C      Date Due: September 13, 2007

C      Language: FORTRAN

C      Compiler used: FORCE2.08

C      Source Filename: Program2.f

C      Executable Filename (if any) or None: None

C      Professor: Elizabeth Adams

C     Purpose: This program reads in a base 10 integer and decimal value and
C              converts them to their binary equivalent
C     Input:   A base 10 integer no more than 5 digits long
C              A base 10 decimal
C     Output:  The base 2 conversion of the base 10 integer input
C              The base 2 conversion of the base 10 decimal input

       DIMENSION INT_ARRAY(10)
       INTEGER DEC_ARRAY(6)

       WRITE(6, 4)
   4   FORMAT(1X, 'Enter an integer ')

C      Reads in and writes the base 10 integer input
       READ (5, 1) INT
       WRITE(6, 2) INT
       
   1   FORMAT(I5)
   2   FORMAT(1X, 'The base 10 value of the input is: ', I5)

       CALL BINARY_INT(INT, INT_ARRAY)
C      Writes the contents of the array containing the binary number
       WRITE(6, 3) (INT_ARRAY(J), J = 1, 10)
  3    FORMAT(1X, 'The base 2 value of the input is: ', 10I1)

C      Reads in and writes the base 10 decimal input
       WRITE(6, 5)
  5    FORMAT(/1X, 'Enter a decimal number ')
       READ(5, 6) DEC
       WRITE(6, 7) DEC
   6   FORMAT(F8.6)
   7   FORMAT(1X, 'The base 10 value of the input is: ', F8.6)

C      Writes the contents of the array containing the binary number
       CALL BINARY_DEC(DEC, DEC_ARRAY)
       WRITE(6, 8) (DEC_ARRAY(K), K = 1, 6)
   8   FORMAT(1X, 'The base 2 value of the input is: .', 6I1)

       STOP
       END

C      Takes in a base 10 integer and converts it to base 2,
C      storing the result in the input array
       SUBROUTINE BINARY_INT (INT, INT_ARRAY)
       DIMENSION INT_ARRAY(10)
C      Divides the integer by 2 and stores the modulus in an array
       DO 20 M = 10, 1, -1
          I = MOD(INT, 2)
          INT_ARRAY(M) = I
          INT = INT / 2
  20   CONTINUE
       RETURN
       END

C      Takes in a base 10 decimal and converts it to base 2,
C      storing the result in the input array
       SUBROUTINE BINARY_DEC (DEC, DEC_ARRAY)
       INTEGER DEC_ARRAY(6)
       DO 21 M = 1, 6
          IF(DEC .LT. 1) GOTO 30
          DEC = DEC - 1
  30      DEC = DEC * 2
C         Determines whether a 0 or 1 should be stored
          IF(DEC .GE. 1) GOTO 31
          DEC_ARRAY(M) = 0
          GOTO 21
   31     DEC_ARRAY(M) = 1
   21  CONTINUE
       RETURN
       END

