C Programmer: Shane McNamee
C Course: CS430
C Date Due: 9/12/07
C Language: FORTRAN
C Compiler used: FORCE 2.0
C Source Filename: mcnamee2
C Executable Filename (if any) or None: mcnamee2
C Professor: Professor Adams

C Description of the program's PURPOSE:
C     The program will input 1 integer and 1 decimal. These numbers will
C     be converted into base 2
C Description of program's required INPUT:
C     The first number will be an integer and the 2nd will be a decimal
C     number
C Description of program's expected OUTPUT:
C     The input will be echoed, and shown in base 2 form
C Descriptions of any MODIFICATIONS made in debugging the program:

C General COMMENTS about the PROGRAM
C     There is no exception handling. The user is trusted for proper
C     input. Everything else seems to work fine.

C     INT and DECIMAL are the numbers input by the user and INT2 and
C     DECIMAL2 are integer arrays that hold the base 2 value
      INTEGER INT
      INTEGER INT2     (15)
      REAL    DECIMAL
      INTEGER DECIMAL2 (6)
      
C     Explains to the user what the program does
      WRITE (6,11) 'This program will turn base 10 inputs into base 2'
  11  FORMAT (A52)
  
C     Makes a blank line
      WRITE (6,17)
  17  FORMAT (' ')
  
C     Prompts the user to enter an integer between 1 and 32767
      WRITE (6,12) 'Type an integer number between 1 and 32767: '
  12  FORMAT (1X, A45)
  
C     Reads in an integer from the user
      READ (5,99) INT
  99  FORMAT (I5)
      
C     Makes a blank line
      WRITE (6,17)
      
C     Echoes the users inputed integer with a label
      WRITE (6,13) INT
  13  FORMAT (1X, 'The base 10 value of the input is: ', I5)
  
C     Calls a subroutine to convert the user's int into base 2
      CALL BASE2INT(INT, INT2)
  
C     Outputs the integer array that holds the base 2 value with a label
      WRITE (6,14) (INT2(I), I = 1,15)
  14  FORMAT (1X, 'The base 2 value of the input is: ', 15I1)

C     Makes a blank line
      WRITE (6,17)
      
C     Prompts the user for a decimal number up to 5 digits
      WRITE (6,12) 'Type a decimal number up to 5 digits: '
      
C     Reads in the user's decimal number
      READ (5,98) DECIMAL
  98  FORMAT (F5.5)
      
C     Makes a blank line
      WRITE (6,17)
      
C     Echoes the user's inputed decimal number with a label
      WRITE (6,15) DECIMAL
  15  FORMAT (1X, 'The base 10 value of the input is: ', F6.5)
  
C     Calls a subroutine to convert the user's decimal number into
C     base 2
      CALL BASE2DECIMAL(DECIMAL, DECIMAL2)
  
C     Outputs the base 2 value of the decimal
      WRITE (6,16) (DECIMAL2(I), I = 1,6)
  16  FORMAT (1X, 'The base 2 value of the input is: .', 6I1)
  
C     Makes a blank line
      WRITE (6,17)
      
C     Informs the user that the program has ended successfully
      WRITE (6,12) 'The program has ended successfully'
      
      STOP
      END
      
C-----------------------------------------------------------------------
C     This subroutine changes an input integer into base 2 and writes
C     the base 2 number into an integer array
C     Input: I is the integer that the user input and BASE2 is the
C            integer array that the base 2 value will be stored in
C     Output: this subroutine has no output
C-----------------------------------------------------------------------

      SUBROUTINE BASE2INT (I, BASE2)
      
C     I is the user's integer, BASE2 is the array storing the base 2
C     value, BASE2LOC is the location in the array being written to, and
C     INDEX is the value being compared to the integer
      INTEGER I
      INTEGER BASE2LOC
      INTEGER INDEX
      INTEGER BASE2 (15)
      
C     Sets the location to 0
      BASE2LOC = 0
      
C     A loop that determines the base 2 value of a given integer
      DO 51 M = 14,0,-1
            BASE2LOC = BASE2LOC + 1
            INDEX = 2**M
            IF (INDEX .LE. I) BASE2(BASE2LOC) = 1
            IF (INDEX .GT. I) BASE2(BASE2LOC) = 0
            IF (INDEX .LE. I) I = I - INDEX
  51  CONTINUE
      
      RETURN
      END
      
C-----------------------------------------------------------------------
C     This subroutine changes an input idecimal number into base 2 and
C     writes the base 2 number into an integer array
C     Input: D is the decimal that the user input and BASE2 is the
C            integer array that the base 2 value will be stored in
C     Output: this subroutine has no output
C-----------------------------------------------------------------------
      
      SUBROUTINE BASE2DECIMAL (D, BASE2)
      
C     D is the user's decimal, BASE2 is the array storing the base 2
C     value, BASE2LOC is the location in the array being written to, and
C     INDEX is the value being compared to the integer
      REAL    D
      INTEGER BASE2LOC
      REAL    INDEX
      INTEGER BASE2 (6)
      
C     Sets BASE2LOC to 0
      BASE2LOC = 0
      
C     A loop to determine the base 2 value of a given decimal
      DO 52 M = 1,6,1
            BASE2LOC = BASE2LOC + 1
            INDEX = 1.0 / (2**M)
            IF (INDEX .LE. D) BASE2(BASE2LOC) = 1
            IF (INDEX .GT. D) BASE2(BASE2LOC) = 0
            IF (INDEX .LE. D) D = D - INDEX
  52  CONTINUE
  
      RETURN
      END
      
            
      
      
      
