C     Programmer: Justin Fiedler
C     Course: CS 430
C     Date Due: 9/12/2007
C     Language: FORTRAN
C     Compiler used: Force 2.0
C     Source Filename: Fiedler PA2.f
C     Executable Filename (if any) or None: None
C     Professor: Adams

C     Description of the program's PURPOSE:

C     To take an integer input and convert it to binary, then do the
C     same with a decimal number.

C     Description of program's required INPUT:

C     The program will prompt the user first for an integer in the range
C     1 to 32767 and then for a decimal value.

C     Description of program's expected OUTPUT:

C     Labeled echo of each input value, each followed by the value's
C     binary representation.

C     Descriptions of any MODIFICATIONS made in debugging the program:

C     Had some trouble confusing real and integer values.

C     General COMMENTS about the PROGRAM

C     Only 6 binary digits for the decimal value are calculated. The
C     binary digits are stored in and read from an integer array. The
C     values are calculated by repeatingly dividing and using modulus by
C     2 respectively.

C     General COMMENTS explaining the syntax or features of the LANGUAGE

C     Goto statements may be used to extend if statements and simulate
C          loop functionality when a do loop is not feasible.
C     The arithmetic if statement may be used essentially as a switch/
C         case around testing a value compared to 0.
C     FORTRAN has several built in functions, such as MOD.
C     With a few GOTO statements and a few arithmetic ifs, it is easy
C          to see how quickly labels and GOTOs can become confusing.

C     Declare arrays to hold the binary digits
      DIMENSION I(15)
      DIMENSION M(6)
      
C     Set the array pointer to point to the beginning of the array
      ICURRENT = 1;
      
C     Ask the user for an integer
      WRITE (6, 1) 'Please enter an integer between 1 and 32767:'
1     FORMAT (1X, A44)
      READ (5, 2) INT
2     FORMAT (I5)

C     Echo the input back to the user in base 10
      WRITE (6, 3) 'The base 10 value of the input is:'
3     FORMAT (/1X, A34)
      WRITE (6, 8) INT
8     FORMAT (2X, I5)
C     1X adds a space, /1X adds a new line

C     Convert the integer into a binary array
66    IF(INT) 99, 4, 5
C     If the decimal integer is 0, the binary value is 0
4     I(ICURRENT) = 0
      GOTO 99
C     Mod and divide the int by 2 to get the next value; repeat
5     I(ICURRENT) = MOD(INT, 2)
      INT = INT / 2
      ICURRENT = ICURRENT + 1;
      GOTO 66
C     MOD is a builtin function in FORTRAN

C     Output the base 2 value
99    WRITE (6, 9) 'The base 2 value of the input is:'
9     FORMAT (/1X, A33)
      WRITE (6, 10) (I(J), J = ICURRENT - 1, 1, -1)
10    FORMAT (2X, 10(10(I1)))

C     Ask the user for a decimal number
      WRITE (6, 11) 'Please enter a decimal number:'
11    FORMAT (/1X, A30)
      READ (5, 12) DEC
12    FORMAT (F10.10)

C     Echo the input back to the user in base 10
      WRITE (6, 13) 'The base 10 value of the input is:'
13    FORMAT (/1X, A34)
      WRITE (6, 14) DEC
14    FORMAT (2X, F11.10)

C     Reset the value of ICURRENT for use in the new array
      ICURRENT = 0

C     Do loop to populate the array with the appropriate values
      DO 23 K = 1, 6
C     Multiply the value by 2 to obtain the initial binary digit
      DEC = DEC * 2
      ICURRENT = ICURRENT + 1
C     If the value to the left of the decimal is a 0, store it as the
C     binary digit
      IF(DEC .LT. 1) GOTO 16
16    M(ICURRENT) = 0
C     If the value to the left of the decimal is a 1, store it as the
C     binary digit and decrement it by 1
      IF(DEC .GT. 1) GOTO 19
      GOTO 20
19    M(ICURRENT) = 1
      DEC = DEC - 1
C     If the value is equal to 1, the number has terminated
20    IF(DEC .EQ. 1) GOTO 21
      GOTO 23
21    M(ICURRENT) = 1
      GOTO 98
23    CONTINUE

C     Output the value of the number in base 2
98    WRITE (6, 17) 'The base 2 value of the input is:'
17    FORMAT (/1X, A33)
      WRITE (6, 18) '.', (M(L), L = 1, ICURRENT)
18    FORMAT (2X, A1, 6(6(I1)))

      STOP
      END
