C     PROGRAMMER: AUSTIN KEELEY
C     COURSE: CS 430 PROGRAMMING LANGUAGES
C     DUE DATE: SEPTEMBER 13, 2007
C     LANGUAGE: FORTRAN 77
C     COMPILER USED: FORCE 2.0.8
C     SOURCE FILENAME: PARTB.F
C     EXECUTABLE FILENAME (IF ANY): PARTB.EXE
C                                   (NOT INCLUDED WITH SUBMIT)
C     PROFESSOR: ELIZABETH ADAMS

C     PURPOSE: THIS PROGRAM FOR A FRACTION OF A REAL NUMBER (E.G. A
C              NUMBER GREATER THAN 0 BUT LESS THAN 1).  IT THEN
C              PERFORMS FLOATING POINT ARITHMATIC TO DETERMINE
C              THE INPUTTED NUMBER IN BASE 2 (BINARY).

C     INPUT: ONE REAL NUMBER THAT IS LESS THAN 1 BUT GREATER THAN 0
C            AN EXAMPLE WOULD BE .123 OR .91121

C     OUTPUT: THE INPUTTED NUMBER IN BASE 2 (BINARY).  THE OUTPUT
C             IS LIMITED TO 6 PLACES AFTER THE POINT.

C     GENERAL COMMENTS: NUMBERS ARE ASSUMED TO BE POSITIVE.  NO ERROR
C             CHECKING IS PERFORMED.  THIS PROGRAM USES THE FIXED
C             COLUMN FORMAT REQUIRED BY EARLY FORTRAN.  THIS WORK
C             COMPLIES WITH THE JMU HONOR CODE.

C     GENERAL COMMENTS ABOUT THE LANGUAGE: NONE

C     ------------------------------------------------------------------


C     THIS FUNCTION RETURNS THE FLOOR OF A REAL NUMBER.  E.G. IF THE
C     INPUT NUMBER IS 1.232 THE FUNCTION RETURNS 1.  IF THE NUMBER IS
C     0.345 THEN THE FUNCTION RETURNS 0.  THE NOTES STATED THAT FORTRAN
C     INCLUDES THIS AS AN INTRINSIC FUNCTION, HOWEVER, INTERNET SOURCES
C     STATED THAT THIS FUNCTION WASN'T INCLUDED UNTIL FORTRAN 95.
      FUNCTION IFLOOR (R)
          IFLOOR = 0
          IF (R .GT. 1)  THEN
            IFLOOR = 1
          ENDIF
      RETURN
      END

C     ------------------------------------------------------------------
      
C     WE NEED A PLACE TO STORE THE NUMBERS AFTER THE POINT.  SINCE THE
C     ONLY NUMBERS WE WILL BE DISPLAYING ARE 1 AND 0, WE CAN USE AN
C     ARRAY OF INTEGERS.
      DIMENSION IRBINARY(6)

C     PROMPT THE USER FOR INPUT.
      WRITE (6, 14) 'PLEASE ENTER A FRACTION OF A REAL NUMBER: '
  14  FORMAT (1X, A42);

C     READ IN THE INPUT.  THE USER IS REQUIRED TO ENTER IN THE POINT
C     THEMSELVES.  IF IT IS OMITTED, THEN THE PROGRAM ASSUMES THAT
C     THEY WANT A 6 DIGIT NUMBER WITH 6 DIGITS FOLLOWING THE POINT.
      READ (5, 15) USERINPUT
 15   FORMAT (F7.6)

C     ECHO THE USER INPUT BACK TO THE USER.
      WRITE (6, 19) 'YOU ENTERED: ', USERINPUT
 19   FORMAT (1X, A13, F7.6)

C     BEFORE WE CAN USE THE ARRAY, WE WANT TO INITIALIZE EACH ELEMENT
C     TO ZERO.  DEPENDING ON THE NUMBER INPUT, WE MAY NOT PLACE A VALUE
C     IN EACH OF THE ARRAY LOCATIONS, SO THIS WILL KEEP RANDOM NUMBERS
C     FROM APPEARING.
      DO 25 M = 1, 6
         IRBINARY(M) = 0
  25  CONTINUE


C     THIS IS WHERE THE FLOATING POINT ARITHMETIC TAKES PLACE.  WE LOOP
C     WHILE MULTIPLYING THE INPUTTED NUMBER BY 2 EACH TIME.  IF THE
C     NUMBER EQUALS EXACTLY ONE WE HAVE A BINARY FLOATING POINT NUMBER
C     THAT TERMINATES.  WE NEED TO EXIT THE LOOP IMMEDIATELY.  IF NOT,
C     THEN WE'LL CONTINUE TO GET THE FLOOR OF THE NUMBER UNTIL WE
C     REACH SIX PLACES.
      DO 17 M = 6, 1, -1
         USERINPUT = 2 * USERINPUT
         IF (USERINPUT .NE. 1) THEN
           IRBINARY(M) = IFLOOR(USERINPUT)
         END IF
         IF (USERINPUT .EQ. 1) THEN
           IRBINARY(M) = 1
C          BREAK OUT OF THE LOOP BECAUSE THE NUMBER TERMINATES!
           GOTO 23
         END IF
  17  CONTINUE

C     WRITE THE POINT AND THEN WRITE THE CONTENTS OF THE ARRAY IN
C     REVERSE ORDER.  WE COULD HAVE READ THEM IN FORWARD ORDER AND
C     PRINTED THEM BACK IN FORWARD ORDER, BUT THE CODE IN PART A DOES
C     IT IN REVERSE FOR CLAIRITY SO WE WILL DO IT HERE TOO.
  23  WRITE (6, 18) 'IN BINARY THAT EQUALS .',(IRBINARY(I), I=6, 1, -1)
  18  FORMAT (//1X, A23, 6I1)
   
C     TELL THE USER THAT THE PROGRAM IS OVER.
      WRITE (6, 26) 'END OF PROGRAM'
  26  FORMAT (1X, A14)
   
C     THE PROGRAM HAS ENDED.
      STOP
      END
