C       Programmer:				Jaycelynn Goldberg
C       Course:					CS 430 Programming Languages
C       Date Due:		online		Wed Sept. 12, 2007  11:59 PM
C                               in class        Thurs Sept. 13
C       Language:				FORTRAN
C       Compiler Used:				FORCE 2.0
C       Source Filename:                        PA2A.f
C       Executable Filename (if any) or *None*: NONE
C       Professor:				Elizabeth Adams
C
C       Description of the program's PURPOSE:
C           To read a base 10 integer from 1-32767, and convert to base 2
C
C       Description of program's required INPUT:
C           An integer in the range 1-32767 (in base 10)
C
C       Description of program's expected OUTPUT
C           Original input: a base 10 integer (1-32767)
C           base 2 format of the input.
C
C       Description of any MODIFICATIONS made in debugging the program
C
C
C       General COMMENTS about the program
C
C
C       General COMMENTS explaining the syntax or features of the LANGUAGE
C           MOD(X,Y)   equivalent to X mod Y (real numbers)
C              (I,J)                 I mod J  (integers)
C              remainder when X or I is divided by Y or J respectively
C
C           READ(5,10,ERR=11) INT1   reads from the standard input device
C                                    according to the format specified by
C                                    label 10.  If an error occurs while
C                                    attempting to read input, the program
C                                    will go to label 11 (ERR=11).

C
C       NARRAY stores the binary values, that when printed in REVERSE order,
C       are the binary representation of the input.
C       i.e. input = 32
C       NARRAY(1) = 0
C       NARRAY(2) = 0
C       NARRAY(3) = 0
C       NARRAY(4) = 0
C       NARRAY(5) = 0
C       NARRAY(6) = 1
C       2nd line of output would be 'The base 2 value of input is: 100000'
        DIMENSION NARRAY(15)
        
C       counter = 0; counts how many places in the array are used
        I=0

C       Get base 10 number from user/terminal
C           Input evaluates the first 5 digits entered
C           If more than 5 digits are entered, but the first 5 evaluate
C           to less than 32767, the input will be the first 5 digits.
C           if the value of the 1st five digits is greater than 32767
C           The program will terminate.If a value that cannot be
C           interpreted is entered (i.e. a string, or character)
C           the program will terminate (ERR= 11)
        READ(5,10,ERR=11) INT1
   10   FORMAT(I5)
   
C       If input is not in valid range, go to label 11
        IF((INT1 .GT. 32767) .OR. (INT1 .LT. 1)) GOTO 11


C       If input is in valid range, convert to binary
        NTEMP = INT1

C       convert to binary
C       while NTEMP > 0, NTEMP mod 2=next digit in binary, NTEMP = NTEMP/2
C       I is a counter for how many digits are required to convert to binary
   12   IF(NTEMP .GT. 0) GOTO 15
   
C       If NTEMP <= 0 (should be equal to 0), exit loop, and print output
        IF(NTEMP .LE. 0) GOTO 16
        
C       If NTEMP > 0,  increase counter, value in array = NTEMP mod 2,
C       NTEMP = NTEMP/2, loop back to line 12
   15      I = I+1
           NARRAY(I) = MOD(NTEMP,2)
           NTEMP = NTEMP/2
           GOTO 12
        
C       print original input, and input converted to binary
   16   WRITE(6,13) INT1
        WRITE(6,14) (NARRAY(J), J= I,1, -1)
   13   FORMAT(1X, 'The base 10 value of the input is: ', I5)
   14   FORMAT(1X, 'The base 2 value of the input is: ',1(15(I1)))

C       terminate program
        GOTO 21

C       If input is not in valid range, terminate program
   11   WRITE(6,20) 'PROGRAM TERMINATED.  INVALID INPUT.'
        WRITE(6,20) 'INPUT MUST BE AN INTEGER 1-32767'
   20   FORMAT(1X,A40)

   21   STOP
        END
