C     Programmer: Derek Hittie
C     Course: CS 430
C     Date Due: September 13, 2007

C     Language: FORTRAN
C     Compiler used: FORCE2.08
C     Source Filename: PA2A.f
C     Executable Filename (if any) or None: PA2A.EXE
C     Professor: Elizabeth Adams

C     Purpose:  This program will read in an integer and output its base 2
C               representation.
C     Input:    This program requires an integer in the range of 1 to 32,767
C     Output:   The output will be an appropriate label, followed by an
C               echo of the integer input by the user.  Another appropriate
C               label followed by the binary representation of the initial input.


C     Language  Information

C         Does not allow programmer to change value of DO Loop initial variable inside loop
C         Loop ending value is set at the time the loop is entered
C         A reserved word cannot be used as an identifier in a program
C         Predefined terms - can be redefined by programmer -> original def. is lost
C         Understands reserved words by their location/use
C             - i.e. variable named IF is distinguished from IF statement -> IF = ?? vs. IF (condition)
C         Functions -> return single value
C         Subroutines -> return 0, 1, or multiple values
C             - parameters are passed by reference
C             - CALL Subroutine(parameters)
C         Functions & Subroutines both have RETURN statement -> returns control to line following original call
C         Cannot put constants in parameter list -> passed by reference
C         Call to function appears on right side of assignment
C             - i.e. variable = FUNCTION(paramters)
C         Single quotes vs. double quotes makes no difference -> consistency
C         Arrays start at 1, not 0.

      DIMENSION KONVERT(15)
C     Get input and assign values to an integer variable
      WRITE (6, 29) "Please enter an integer (between 1-32767)"
      READ  (5, 30) INUM

C     ECHO input
      WRITE (6, 48) INUM
      
C     Convert base 10 integer to base 2
      CALL CLEARARRAY(KONVERT)
      CALL IDECTOBIN(INUM, KONVERT)
      
C     Output newly convered base 2 integer
      WRITE (6, 49) (KONVERT(I), I = 1,15)
      
      WRITE (6, 29) "Program ended successfully"
      
      
C     Format statements for entire program
  29  FORMAT (/1X, A42)
  30  FORMAT (I5)
  48  FORMAT(/1X, 'The base 10 value of the input is: ', 1X, I5)
  49  FORMAT(/1X, 'The base  2 value of the input is: ', 1X, 15I1)
      STOP
      END


C     CLEARARRAY - initializes all elements of an array to zero
C     Input: array of integers
C     Returns: an array with each element equal to zero
      SUBROUTINE CLEARARRAY(IARRAY)
      DIMENSION IARRAY(15)
      DO 20 I = 1, 15
           IARRAY(I) = 0
  20  CONTINUE
      RETURN
      END


C     DECTOBIN - converts a base 10 integer to a base 2 integer.
C     Input: Base 10 integer
C     Returns: Base 2 integer
      SUBROUTINE IDECTOBIN(NUM, IARRAY)
      DIMENSION IARRAY(15)
      DO 21 I = 15, 0, -1
          NWHOLE = NUM/2
          NREMAINDER = NUM - NWHOLE*2
          IARRAY(I) = NREMAINDER
          NUM = NWHOLE
          IF(NUM.EQ.0) GOTO 10
  21  CONTINUE
  10  RETURN
      END
