C PA2b 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:	PA2b.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 a decimal number and print
C    out its base 2 representation.
C
C Description of program's required INPUT:
C Decimals that are greater than 0 and less than 1
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: .5
C The base 2 value of the input is :  .1
C
C Descriptions of any MODIFICATIONS made in debugging the program:
C Can't figure out why it's having problems with FLOOR. Is it a
C   variable type issue?
C
C General COMMENTS about the PROGRAM:
C Input error-checking was not implemented. Didn't have time to fully
C   comment the program. I'll add the remaining comments in the printed
C   version.
C
C General COMMENTS explaining the syntax or features of the LANGUAGE
C   See inline comments.
      REAL DECNUM, BINNUM
      ! Prompt for input.  Default formatting is fine.
      WRITE(*,*) 'Enter a decimal number between (1,0):'
      ! Get real input.  Default formatting is fine.
      READ(*,*) DECNUM

      ! Call the function and store the result in BINNUM
      BINNUM = CONVERTTOBINARY(DECNUM)

      ! Output in the required form
      ! Default formatting is acceptable here.
      WRITE(*,*) 'The base 10 value of the input is:', DECNUM
      WRITE(*, *) 'The base  2 value of the input is: ', BINNUM
      
      ! 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(DECNUM)
      !    DECNUM - a number in base 10 between 0 and 1
      !
      ! Converts a decimal number between 0 and 1 to a number in base 2.
      FUNCTION CONVERTTOBINARY(DECNUM)
         REAL REMAINDER, NEWBASENUM
         INTEGER DIGITINDEX, NEWBASE
         
         NEWBASE = 2
         NEWBASENUM = 0

         ! Store input num in a new var so it is not overwritten.
         ! Make sure we're just converting the fractional part.
         REMAINDER = DECNUM - FLOOR(DECNUM)

         DIGITINDEX = -1
  100    IF (REMAINDER .GT. 0) THEN
            REMAINDER = REMAINDER * NEWBASE
            NEWBASENUM = NEWBASENUM + FLOOR(REMAINDER) *
     6         (10 ** DIGITINDEX)
            REMAINDER = REMAINDER - FLOOR(REMAINDER)
            DIGITINDEX = DIGITINDEX - 1
            GOTO 100
         ENDIF
         CONVERTTOBINARY = NEWBASENUM
      RETURN
      END
