C      Programmer: Bryan Rogoff

C      Course: CS 430

C      Date Due: September 12, 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      Description of the program's PURPOSE: To translate a base 10 decimal
C      into base 2.

C      Description of program's required INPUT:
C      Any positive base 10 decimal

C      Description of program's expected OUTPUT:
C      "The base 10 value of the input is: .xxxxx"
C      "The base 2 value of the input is: .xxxxxx"

C      Descriptions of any MODIFICATIONS made in debugging the program:
C      At first, I had the function creating a String and passing that
C      back to the main program, but that did not work so I changed to
C      the function actually printing the integers.

C      General COMMENTS about the PROGRAM: Very simple program that
C      prints out a binary representation of a base 10 number.

C      General COMMENTS explaining the syntax or features of the LANGUAGE:
C      I had a lot of trouble figuring out how to get a second print statement
C      get printed on the same line.

  21    WRITE(6,11)
  11    FORMAT(1X, "Please enter a base 10 number")
        READ(5,12) I
        IF(I .LT. 1) GO TO 22
        IF(I .GT. 32767) GO TO 22
  12    FORMAT(I5)
        WRITE(6,13) I
  13    FORMAT(1X, "The base 10 value of the input is: ", I5)
        WRITE(6,14)
  14    FORMAT(1X, "The base 2 value of the input is: ")
        IERR = IBINARY(I)

        IF(IERR .NE. 0) WRITE(6,15)
  15    FORMAT(1X, "ERROR in program, wrong output")
        GO TO 20

  22    WRITE(6,19)
  19    FORMAT(1X, "Invalid Input.")
        GO TO 21

  20    WRITE(6,17)
  17    FORMAT(1X, "This program has ended.")
        STOP
        END


C       This function prints out the number in binary.  If there is a logic
C       error, it will return a nonzero.
        FUNCTION IBINARY(I)
        DIMENSION IARRAY(6)
        K = I
        M = 0
          DO 10 J = -1,-6,-1
            M = M + 1
            IARRAY(M) = 1
            IF(K .LT. 2**J) IARRAY(M) = 0
            IF(K .GE. 2**J) K = K - (2**J)
  10      CONTINUE
        WRITE (6,24) (IARRAY(L), L = 1,6)
  24    FORMAT(1X, ".", 6(6(I1)/1X))
        IBINARY = K
        RETURN
        END
