C     Programmer: Becca Gaspard
C     Course: CS430
C     Date Due:9-13-07
C     Language: FORTRAN
C     Compiler used: force2.08
C     Source Filename: program2a.f
C     Professor: Adams
C
C     Purpose: This program takes an integer in the range of 0 to 327677
C     and converts it into binary
C     Required input: user must enter an integer in the specified range
C     Expected output: the binary equivalent of the user's input,
C     length will be less than or equal to 15 digits
C     Descriptions of modifications: I made input of 0 a special case
C     because it was not working correctly during debugging
C     General comments: The program assumes the user will input
C     an integer in the specified range (no error checking)


C     a number<327677 can only consist of 15 binary digits
C     the binary solution will be stored in this array of integers
      DIMENSION NARRAY(15)
      
C     2^16=NMAX, user input will be less than integer NMAX
      NMAX=32768
      
C     integer represents the first relevant digit in the binary number
C     so that leading zeros will not be printed
      NSIZE=0
      
C     integer keeps track of if NSIZE has been set (0=false, 1=true)
      NSET=0
      
C     requests input from the user and echos back the input
C     input is stored in variable I(integer)
      WRITE(6,10) 'Enter an integer between 0 and 32767!'
 10   FORMAT(A37)
      READ(5,11) I
 11   FORMAT(I5)
      WRITE(6,12) I
 12   FORMAT('The base 10 value of the input is: ', I5)
 
C     SPECIAL CASE - if the I=0 only the 15th zero will be printed
C     so NSIZE can be set now
      IF(I .NE. 0) GO TO 13
       NSET=1
       NSIZE=15

C     This loop assigns zero to every spot in the arrary, then divides
C     NMAX by 2.  NMAX now represents the value of the J'th position in
C     binary array
 13   DO 60 J =1,15
        NMAX=NMAX/2
        NARRAY(J)=0
C     If I is greater than NMAX, a 1 will be put in the J'th position
C     and the value of the J'th position stored in IMAX will be
C     subtracted from I
        IF(I .LT. NMAX) GO TO 60
          NARRAY(J)=1
          I=I-NMAX
C     This will set NSIZE to the position of the first 1 in the array
C     After NSIZE is set, NSET will equal 1, the if statement
C     prevents it from being set again
          IF(NSET .NE. 0) GO TO 60
          NSIZE=J
          NSET=1
 60   CONTINUE
 
C     The array is printed on one line starting with
C     NSIZE and going to 15
      WRITE(6,14) (NARRAY(K), K=NSIZE,15)
 14   FORMAT('The base 2 value of the input is: ', 15(15(I1)))

C     This text will appear to show the program has ended
      WRITE(6,15) 'The program has ended normally!'
 15   FORMAT(A35)
      
      STOP
      END
