C     Programmer: Cameron Kolleda
C     Course: CS430
C     Date Due: Sept. 11 2007
C     Language: FORTRAN
C     Compiler used: Force 2.0
C     Source Filename: PA2.f
C     Executable Filename (if any) or None: PA2.exe
C     Professor: Adams

C     Description of the program's PURPOSE:
C        This program reads in a base 10 integer between 1 and 32767, echoes
C        it, then converts it to its binary equivalent and outputs the result
C        Then it reads in a base 10 decimal, echoes the input,
C        then also converts it to its binary equivalent and outputs the result

C     Description of program's required INPUT:
C        a base 10 integer, then a base 10 decimal

C     Description of program's expected OUTPUT:
C     Labled information:
C        Echoed base 10 values
C        Equivalances in base 2

C     Descriptions of any MODIFICATIONS made in debugging the program:
C        Had to use a count when converting to binary to only output the proper
C            array values and to know when to stop processing
C        Could not use a DO WHILE loop so I had to figure out how to use IF and GO TO
C            statements properly to circumvent this
C        Getting the implied DO loops to properly output data on the same
C            line as the lable was difficult to get working properly

C     General COMMENTS about the PROGRAM
C             Error checking is not used with input
C             Uses functions/subroutines to simplify
C             Uses IF/GO TO statements to mimic a DO WHILE loop

C     General COMMENTS explaining the syntax or features of the LANGUAGE
C             MOD(a, b) returns the remainder after dividing 'b' into 'a'
C             INT(a) truncates the real value 'a' and casts it as an int
C             .EQ. is the equality comparison
C             .NE. is the not equal to comparison
C             .GE. is the greater than or equal to comparison
C             Those are the new features and syntax I used.

C     Declares the arrays, lengths, and count variables
      DIMENSION NUM_BIN(15), NFRACT_BIN(6)
      NSIZE = 15
      MSIZE = 6
      NCOUNT = 1
      MCOUNT = 1

C     Requests and reads an integer value
      WRITE (6,10) 'Please enter a number in base 10 (1-32767): '
      READ (5,11) INT

C     Formatting statements for requesting and reading integer value
 10   FORMAT(1X, A44)
 11   FORMAT(I5)

C     Echoes the entered integer value with a label
      WRITE (6,20) INT
 20   FORMAT (/1X, 'The base 10 representation is: ', I5)

C     Continuously calls the subroutine to calculate the binary until zero would repeat
 99   CALL NUM_TO_BIN(NUM_BIN, NSIZE, INT, NCOUNT)
      IF (INT .NE. 0) GO TO 99
      
C     Outputs the base 2 equivalent using an implied DO loop
      WRITE (6,40) (NUM_BIN(I), I = (NCOUNT-1),1,-1)
 40   FORMAT(1X, 'The base 2 representation is: ', 15I1)

C     Requests and reads a base 10 decimal value
      WRITE (6,50) 'Please enter a number between 0 and 1: '
      READ (5, 22) FRACT

C     Formatting for the request and read
 50   FORMAT(/1X, A39)
 22   FORMAT(F6.6)

C     Echoes the entered value
      WRITE (6, 60) FRACT
 60   FORMAT (/1X, 'The base 10 representation is: ', F7.6)

C     Repeatedly calls the subroutine until the conversion is done or 6 digits have been calculated
 88   CALL FRACT_TO_BIN(NFRACT_BIN, MSIZE, FRACT, MCOUNT)
      IF (MCOUNT .EQ. 7) GO TO 80
      IF (FRACT .NE. 0) GO TO 88

C     Outputs the binary converstion using an implied DO loop
 80   WRITE (6,70) (NFRACT_BIN(I), I = 1, (MCOUNT-1))
 70   FORMAT(1X, 'The base 2 representation is: .', 6I1)

C     Display for successful completion
      WRITE (6, 90) 'Program ended successfully!'
 90   FORMAT(/1X, A50)

C     Ends the program
      STOP
      END
      
      
C     This subroutine is repeatedly called to calculate each binary digit using MOD
      SUBROUTINE NUM_TO_BIN (NUM_BIN, NSIZE, INT, NCOUNT)
C     Redeclare the array
      DIMENSION NUM_BIN(NSIZE)
C     MOD to get the 1 or 0 value, place it into the array, do integer division
      NREMAIN = MOD(INT, 2)
      INT = INT / 2
      NUM_BIN(NCOUNT) = NREMAIN
C     Increment the count
      NCOUNT = NCOUNT + 1
      RETURN
      END
      
C     This subroutine is repeatedly call and calculates the binary equivalent of the decimal
      SUBROUTINE FRACT_TO_BIN (NFRACT_BIN, MSIZE, FRACT, MCOUNT)
C     Redeclare the array
      DIMENSION NFRACT_BIN(MSIZE)
C     Multiply by two to get the 1 or 0 digit from the result, then places it into the array
      FRACT = FRACT * 2
      NFRACT_TEMP = INT(FRACT)
      NFRACT_BIN(MCOUNT) = NFRACT_TEMP
C     If the value is greater than one, subtracts the extra digit from in front of the decimal
      IF (FRACT .GE. 1) FRACT = FRACT - 1
C     Increments the count
      MCOUNT = MCOUNT + 1
      RETURN
      END
 

