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: PA2B.f
C     Executable Filename (if any) or None: PA2B.EXE
C     Professor: Elizabeth Adams

C     Purpose:  This program will read in a decimal number and output its base 2
C               representation.
C     Input:    This program requires a decimal number
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               correct to 6 places.


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 IARRAY(6)
      
C     Get input and assign values to an integer variable
      WRITE (6, 29) "Please enter a decimal number "
      READ  (5, 30) ANUM

C     ECHO input
      WRITE (6, 48) ANUM

      CALL CLEARARRAY(IARRAY)
      
C     Convert base 10 decimal number to base 2
      DO 21 I = 1, 6
          TEMP = 2*ANUM
          IWHOLE = TEMP
C     Store each base 2 unit in an array
          IARRAY(I) = IWHOLE
          ANUM = TEMP - IWHOLE
          IF(ANUM.EQ.0.0) GOTO 10
  21  CONTINUE
  
C     Output newly convered base 2 number
  10  WRITE (6, 49) (IARRAY(I), I = 1,6)

      WRITE (6, 29) "Program ended successfully"

C     Format statements for entire program
  29  FORMAT (/1X, A35)
  30  FORMAT (F6.6)
  48  FORMAT(/1X, 'The base 10 value of the input is: ', 1X, F7.6)
  49  FORMAT(/1X, 'The base  2 value of the input is:  .', 6I1)
      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(MARRAY)
      DIMENSION MARRAY(6)
      DO 20 I = 1, 15
           MARRAY(I) = 0
  20  CONTINUE
      RETURN
      END
