C PA2a Program Header
C Programmer:		Doug Roper
C Course:		CS 430 Programming Languages 2pm
C Date Due:		9/5/07
C Language:		Fortran
C Compiler used:	Force 2.0
C Source Filename:	PA2a.f
C Executable Filename (if any) or None: (none)
C Professor:		Dr. Elizabeth Adams
C
C Description of the program's PURPOSE:
C Write a FORTRAN program which will read in an integer and print out
C   its base 2 representation.
C
C Description of program's required INPUT:
C Your program should be able to handle integers in the range 1 to
C    32767.
C
C Description of program's expected OUTPUT:
C Here is what the output should look like:
C The base 10 value of the input is:  32767
C The base  2 value of the input is: 1111111111
C
C Descriptions of any MODIFICATIONS made in debugging the program:
C Originally used an integer to store the resultant binary number, but
C    for large numbers, the integer type is insufficently small. Instead
C    of causing an overflow, the value wraps around as would be expected
C    of an integer in 2's complement notation. Therefore I had to change
C    the integer storing the "binary" to an array.
C    When I changed the output var to an array, my function broke since
C    functions cannot return arrays. I changed the function to a
C    subroutine instead. It's not as elegant, but Fortran was created
C    by mathematicians. It was never designed to be elegant.

C General COMMENTS about the PROGRAM:
C Input error-checking was not implemented.
C
C General COMMENTS explaining the syntax or features of the LANGUAGE
C   See inline comments.

      ! Declare variables
      INTEGER DECNUM
      INTEGER BINARRAY(15)

      ! Prompt for input.  Default formatting is fine.
      WRITE(*,*) 'Enter an Integer in base 10:'
      ! Get integer input.  Default formatting is fine.
      READ(*,*) DECNUM

      ! Call the subroutine that does all the work. Though not
      !   required, it modularizes the program nicely and improves
      !   style. Since functions cannot pass back arrays, we have to use
      !   a subroutine here.  Instead we pass the subroutine an array
      !   which will be populated with the result-- in this case, the
      !   binary array.
      CALL CONVERTTOBINARY(DECNUM, BINARRAY)

      ! Output in the required form
      ! Default formatting is acceptable here.
      WRITE(*,*) 'The base 10 value of the input is:', DECNUM
      ! Formatting is required for outputting the array.
      WRITE(*, 200) 'The base  2 value of the input is: ', BINARRAY
  200 FORMAT (1X, A35, 15I1) ! 1 Space, 35 Letters, 15 1-char Ints
      
      ! Dr. Adams mentioned in class that she liked output when the
      !    program terminated successfully.
      WRITE(*,*) 'Program has terminated successfully.'
      STOP ! Stops program execution
      END  ! Ends the program
      
      
      ! CONVERTTOBINARY(OLDBASENUM, NEWBASEARRAY)
      !    OLDBASENUM   - a number in base 10
      !    NEWBASEARRAY -  an empty array passed into the function to
      !       store the result of the function
      !
      ! Converts a decimal number to a number in base 2. Each digit of
      !    the result is stored in an array location in NEWBASEARRAY.
      SUBROUTINE CONVERTTOBINARY(OLDBASENUM, NEWBASEARRAY)
         ! Declare variables
         INTEGER NEWBASE, OLDBASENUM, REMAINDER, DIGITINDEX
         INTEGER NEWBASEARRAY(15)
         
         ! Specify desired base. Can be changed to any base <= 10.
         NEWBASE = 2

         ! Store input num in a new var so it is not overwritten.
         REMAINDER = OLDBASENUM
         
         ! Work backwards through the array
         DIGITINDEX = 15

         ! Convert the number to the new base.
         ! Though a while-loop was later added, it was not implemented in
         !   Fortran 77 so I will not use one. A labeled if-then with a
         !   goto at the end is functionally equivalent.
  100    IF (REMAINDER .GT. 0) THEN
            NEWBASEARRAY(DIGITINDEX) = MOD(REMAINDER, NEWBASE)
            DIGITINDEX = DIGITINDEX - 1
            REMAINDER = REMAINDER/NEWBASE
            GOTO 100
         ENDIF ! End of while-loop
         
         ! Zero out the remainder of the array with a 'do' for-loop.
         DO 101 DIGITINDEX = DIGITINDEX, 1, -1
            NEWBASEARRAY(DIGITINDEX) = 0
  101    CONTINUE ! End of loop
      RETURN ! Return control to the program at subroutine entry point
      END    ! End of subroutine
