C       Programmer: Blaine Whited
C       Course: 430
C       Date Due: September 12th, 2007
C       Language: FORTRAN
C       Compiler used: FORCE 2.0
C       Source Filename: PA2A.f
C       Executable Filename (if any) or None: PA2A.exe
C       Professor:  Dr. Elizabeth S. Adams
C
C       Description of the program's PURPOSE:
C           This program will read in an integer, echo it, convert it
C           to base two, and then display it. This program is part one
C           of a whole, Program #2.
C       Description of program's required INPUT:
C           An integer between 1 and 32767
C       Description of program's expected OUTPUT:
C           An echo of the inputted value and a message containing the
C           base two representation of the integer input. (Values
C           between 1 and 1111111111)
C       Descriptions of any MODIFICATIONS made in debugging the program:
C           After some work I realized that an Integer can only hold
C           10 digits, realized that I needed up to 15, I changed the
C           variable to an array.
C       General COMMENTS about FORTRAN
C           It is annoying to work around FORTRAN's shortcomings in
C           Integer storage.
C       General COMMENTS explaining the syntax or features of the
C         LANGUAGE
C           Fortran formatting is not very intuitive. It seems like the
C           formats for writes are hit or miss when it gets complicated.
C
C     initialize the storing array (15 for 32767's binary value length)
      DIMENSION IRESULT(15)
C     initialize the input storing variable
      INT = 0

C     Write the opening message
      WRITE(6,1)
    1 FORMAT(1X,'Welcome to the magnificent base converter.')
C     Line label is used to check for proper inputs
   51 WRITE(6,2)
    2 FORMAT(1X,'This program will attempt to convert an integer')
      WRITE(6,3)
    3 FORMAT(1X,'between 1 and 32767 into its binary equivalent.')
      WRITE(6,4)
    4 FORMAT(1X,'Please enter an integer now.')

C     Reads the inputted value and stores it in the variable INT
      READ (5,5) INT
    5 FORMAT (I6)

C     Says "If the inputted value is greater than 32767(the max value)
C     or the inputted value is less than 1 (the minimum value) goto 52"
      IF (INT .GT. 32767 .OR. INT .LT. 1) GO TO 52
C     Says "When the IF fails, skip the next printout"
      GO TO 60
C     Says "When the IF passes, print out an error message and ask for
C     another input."
   52 WRITE(6,6)
    6 FORMAT(1X,'Please, only enter numbers between 1 and 32767')
      GO TO 51

C     Echos the inputted value
   60 WRITE(6,7) INT
    7 FORMAT(1X,'You input: ', I5)
   
C     Begin conversion
C     Loops though all 15 binary digits
      DO 112 I = 1,15

C     Initializes each integer of the array
      IRESULT(16-I) = 0

C     Speeds things up by skipping the last of the binary digits when
C     there is nothing left of the inputted value (i.e. no more
C     conversion is needed)
      IF(INT .LT. 1) GO TO 112

C     Resets the binary value holder to 0
      IB = 0

C     Does the mathematics to the inputted value one binary digit at a
C     time, the first line will return either a 0 or a 1, and the next
C     line will move to the next digit correctly
      IB =  MOD(INT,2)
      INT = INT / 2

C     Saves the result in the array, backwards so the first conversion
C     is for 2**1
      IRESULT(16-I) = IB
C     End of DO Loop
  112 CONTINUE

C     Prints out the binary number, with labeling.
      WRITE (6, 24) (IRESULT(K), K=1,15)
   24 FORMAT (1X, 'That number in binary is: ',15(I1))

C     Finishes the program with an exit print out
      WRITE(6,100)
  100 FORMAT(1X, 'Thank you for using our software.')
  
C     End of the program
      STOP
      END
