C      Programmer: Shawn Bush
C      Course: CS 430
C      Date Due: 9/6/07
C     Language: FORTRAN
C     Compiler used: Force 2.0
C     Source Filename: PA2.f
C     Executable Filename: PA2.exe
C     Professor: Adams
C
C
C     PURPOSE:
C     To read in one base 10 number and echo it as a base 2 number,
C     then read a base 10 fraction and echo it as a base 2 number.
C
C     INPUT:
C     One base 10 integer, and one base 10 fraction in decimal format.
C
C     OUTPUT:
C     The integer displayed in base 2, followed by the fraction in
C     base 2.
C
C     DEBUGGING MODIFICATIONS:
C     I realized that dimensions had to be declared before any other
C     exectuable code. The errors that helped me find that out were
C     NOT helpful though.
C
C     GENERAL PROGRAM COMMENTS:
C     A do loop is required to convert each number to binary.
C     Several GOTO statements were required so the code looks a lot
C     like spaghetti code.
C
C     LANGUAGE COMMENTS:
C     This was easier to do in FORTRAN than I thought it would be,
C     but it was slightly aggrivating to be restricted to declaring
C     dimensions at the very top of the main program block.

C     Dimensions (arrays) declared at the very top of the program
C     because FORTRAN did not let me declare them anywhere else.
      DIMENSION INT2(10)
      DIMENSION IDEC2(6)
      
C     Output asking for a base 10 integer less than or equal to 1023.
C     Then, an input statement that takes the integer in as INT10.
      WRITE (6,1)
      READ (5,99) INT10
      WRITE (6,3) INT10
      
C     If the number is greater than 1023, the program ends.
      IF (INT10 .GT. 1023) GOTO 201

C     Declare and initialize a counter at 2 to the 9th power.
      ICOUNTER = (2**9)
      DO 100 N = 1,10,1
         IF (INT10 .GE. ICOUNTER) GOTO 101
            GOTO 102
101      INT2(N) = 1
         INT10 = INT10 - ICOUNTER
         GOTO 103
102      INT2(N) = 0

C        Each time a place in the output number is evaluated, the
C        counter must be dropped to the next power of 2 for the next
C        right place.
103      ICOUNTER = ICOUNTER/2
100   CONTINUE

      WRITE (6,4) INT2
      
C     After the first number is output in binary, ask for a fraction
C     in decimal format, then read one in to DEC10.
      WRITE (6,5)
      READ (5,98) DEC10
      
C     If the number is not a fraction, skip to the end of the program.
      IF (DEC10 .GE. 1) GOTO 201
      WRITE (6,6) DEC10
      
C     For each iteration of the do loop, the number is multiplied by
C     two, and the number in the "1s" position is placed into the
C     array representing the binary representation. If the number is
C     exactly one or greater than one, the program exits, or 1 is
C     subtracted from the number, respectively.
      DO 200 N = 1,6,1
         DEC10 = DEC10 * 2
         IF (DEC10 .EQ. 1) GOTO 301
         IF (DEC10 .GT. 1) GOTO 302
         IDEC2(N) = 0
         GOTO 303
301      IDEC2(N) = 1
         DEC10 = 0
         GOTO 303
302      IDEC2(N) = 1
         DEC10 = DEC10 - 1
303      IDEC2(N) = IDEC2(N)
200   CONTINUE

      WRITE (6,7) IDEC2
      
C     This GOTO statement skips to the end of the program, so that
C     the error message is not output incorrectly.
      GOTO 202
201   WRITE (6,2)

C     Various format statements.
1     FORMAT ("Please input one integer less than or equal to 1023.")
2     FORMAT (/"Number is outside allowed range. Exiting.")
3     FORMAT (/"You entered: ", I4)
4     FORMAT (/"That number in binary is: ", 10(I1)/)
5     FORMAT (/"Please enter a fraction in decimal notation.")
6     FORMAT (/"You entered: ", F7.6)
7     FORMAT (/"That number in binary is: .", 6(I1)/)
99    FORMAT (I4)
98    FORMAT (F6.6)

202   STOP
      END
