C Programmer: Zane Kreider
C Course: Programming Languages 430
C Date Due: Midnight September 12th
C Language: Fortran
C Compiler used: Force 2.0
C Source Filename: PA2a.f
C Executable Filename (if any) or None: none
C Professor: Professor Adams

C Description of the program's PURPOSE:
C     To read in a base 10 integer between 1 and 32767 convert it to binary,
C     and then echo the converted number

C Description of program's required INPUT:
C     an integer in base 10 between 1 and 32767

C Description of program's expected OUTPUT:
C     The program will echo the input, with a label
C     Then it will print a label for the output, and then print the converted
C     Base 2 number

C Descriptions of any MODIFICATIONS made in debugging the program:
C     NONE

C General COMMENTS about the PROGRAM
C     Numbers are assumed to be positive integers without commas, spaces, or decimals
C     No error checking is performed
C     This program uses the fixed column format required by early FORTRAN

C General COMMENTS explaining the syntax or features of the LANGUAGE
C     C in column 1 for comments
C     Labels are numbers in columns 1 through 5
C     Continuation card entry in column 6 uses an upper case C
C     Variable names which begin with any letter other than I - N are by defaults, real.
C     Real variables beginning with any letter other than I - N do not have to be declared
C     When using READ statements, the first number 5 represents keyboard input
C     When using WRITE statements, the first number 6 represents screen output
C     Functions return values that must be stored
C     Subroutines change the values of the variables passed in, and do not return a value
C     Inside loops, control variables cannot be altered

C     This is the array to hold the converted binary number
      DIMENSION I(15)

C     ******Block 1******
C     Prompting for input
      WRITE (6,1) "Enter an integer between 1 to 32767"
C     Read in input
      READ (5,2) J
C     Labels and echos the input
      WRITE (6,3) "The base 10 value of the input is: " , J
      
C     Block 1 format statments
   1  FORMAT (A35)
   2  FORMAT (I5)
   3  FORMAT (/1X, A35, I5)

C     ******Block 2******
C     Setting a variable for use in calling subroutine
      IVALUE = 15
C     Calling subroutine CONVERT passing array, size of array, and input
      CALL CONVERT (I,IVALUE,J)
C     Labels and writes the converted number
      WRITE (6,4) "The base 2 value of the input is: " , I
C     signifies end of program
      WRITE (6,5) "The program has completed successfully!"

C     Block 2 format statements
   4  FORMAT (1X, A34, 15(15(I1)))
   5  FORMAT (/A39)
   
      STOP
      END

C     begining of subroutine CONVERT
      SUBROUTINE CONVERT (IARRAY, NUM, IVALUE)
C     Setting array size since it was passed
      DIMENSION IARRAY(NUM)

C     Loop over the possible 15 digits starting at num, and decrementing by 1
      DO 7 M = NUM, 1, -1
C     Set ITEMP = 2 to the power of M minus 1
      ITEMP = 2**(M-1)

C     if itemp is greater than I value, do not subtract from ivalue
      IF (ITEMP.GT.IVALUE) GO TO 6
C     subtract itemp from ivalue
      IVALUE = IVALUE - ITEMP
C     place a 1 in the correct array location
      IARRAY(NUM - (M-1)) = 1
C     skip over placing of 0
      GO TO 7
C     number too large, place zero in location
   6  IARRAY(NUM - (M-1)) = 0
C     end of loop
   7  CONTINUE

      RETURN
C required to signify the end of the subroutine
      END
