C        Programmer: Brenton N. Kohler
C        Course: CS 430 12:30-1:45
C        Date Due: September 12th, 2007
C        Language: FORTRAN 77
C        Compiler used: G77
C        Source Filename: decimalbase10to2.f
C        Executable Filename: decimalbase10to2.exe
C        Professor: Dr. Adams
C
C        Description of the program's PURPOSE: This program requests an
C        decimal value in base ten. It takes the number, echoes it, and
C        then finds its binary equivalent number. This program will run
C        until a negative number is encountered.
C
C        Description of program's required INPUT: A base ten decimal
C        value, if a negative number is encountered, the program ends.
C
C        Description of program's expected OUTPUT: A prompt for a base
C        ten decimal value, the number echoed, the binary equivalent
C        labeled, and a statement saying the program ended normally.
C
C        Descriptions of MODIFICATIONS made in debugging the program: I
C        made the input get recieved right in the same line as the
C        prompt instead of on a newline. I think this looks better to
C        have it on the same line.
C
C        General COMMENTS about the PROGRAM: I had lots of trouble
C        getting the format right on this program. Things wouldn't print
C        on the same line when I wanted them too.
C
C        General COMMENTS explaining the syntax of the LANGUAGE: The
C        write statement automatically adds a newline to the end of
C        every statement. I wanted my array of the binary number to
C        print sequentially. You do that by adding a '$' to your format
C        statement.
C        ---------------------------------------------------------------
C        My array to hold the binary representation of the number
         DIMENSION IBASETWO(30)

C        1: echoes the value out
C        2: The format statement for my binary numbers
C           Each digit is one integer long. The dollar sign tells the
C           compiler to write on the same line so the numbers are
C           sequential.
C        3: the prompt for a new base 10 decimal value
C        4: the program completion prompt
C        5: Reads the right length integer in
C        6: Writes the label for integer in base 2
  1      FORMAT(1X, 'The decimal number in base 10 is: ', F8.6)
  2      FORMAT(I1, $)
  3      FORMAT(1X,'Please enter a base 10 decimal number: ',$)
  4      FORMAT(A33)
  5      FORMAT(F10.10)
  6      FORMAT(1X, 'The decimal number in base 2 is:  0.', $)
  
C        Prompt for a new base 10 decimal.
   55    WRITE (6,3)
         READ (5,5) BASETEN

C        Keeps the program running until a negative number is entered.
         IF(BASETEN.LT.0)THEN
          GOTO 80
         ENDIF

C        This clears the array for a new number to come in
         INDEX = 30
         DO I = 1,INDEX
             IBASETWO(INDEX) = 0
          ENDDO
         
C        ECHO the new base ten number out
         WRITE(6,1) BASETEN
         
         INDEX = 1
C        Shift radix point to right by 1.
  22     BASETEN = BASETEN * 2

C        Case 2: BASETEN is less than 1
         IF(BASETEN .LT. 1)THEN
               IBASETWO(INDEX) = 0
               INDEX = INDEX + 1
         ENDIF
C        Case 2: BASETEN is greater than or equal to 1
         IF (BASETEN .GE. 1) THEN
              IBASETWO(INDEX) = 1
C             keep only integer part
              BASETEN = BASETEN - 1;
C        get rid of value left of radix point
              INDEX = INDEX + 1
         ENDIF
C        If baseten number is still greater than zero, do it again
         IF(BASETEN.GT.0)THEN
               GOTO 22
         ENDIF
         
C        The label and the number in base 2
          WRITE (6,6)
          DO I = 1,INDEX
             WRITE(6,2) IBASETWO(INDEX)
          ENDDO

C        The first statement allows the second statement to go on a new
C         line. This is because to print the base two number, I wanted
C         them all on one line and therefore the write statement would
C         follow on one line. Putting a blank one in allows the
C         completion sentence to go on a new line. This is repeated at
C         line 80.
         WRITE (6,4)
  
C        The GOTO commnad to keep the program looping until a negative
C         number is input.
         GOTO 55
         
  80     WRITE (6,4)
         WRITE (6,4) 'The program has ended normally!!'

         STOP
         END
