C      Programmer: James Danberg


C      Course: CS 430


C      Date Due: September, 2007


C      Language: FORTRAN


C      Compiler used: FORCE2.08


C      Source Filename: SOURCE1.f


C      Executable Filename: NONE


C      Professor: Elizabeth Adams





C   Purpose:  Program converts a base 10 whole number or base 10 decimal

C             into its binary equivelant


C   Input:    User will be prompted for a number


C   Output:   The binary equivelant to the user input number


C   Descriptions of any MODIFICATIONS made in debugging the program:


C   General COMMENTS about the PROGRAM


C   Numbers are assumed to be positive


C   Error checking is only done to see if user selected a valid option


C   Uses integers for the whole number, but uses reals for the decimal


C   Format statements are all grouped together


C   General COMMENTS explaining the syntax or features of the LANGUAGE










C     Array is used to hold binary values
      DIMENSION NUMBER(15)
C     IMAX holds the value for each binary position, divides by 2 later
      IMAX = 32768
C     ISTART decides which part of the array to start printing
      ISTART = 1
C     IFLAG labels whether ISTART has been decided or not
      IFLAG = 0
      

C     Prompts user for decimal to binary, or whole number to binary
      WRITE (6, 14) 'Would you like to do option 1 or 2?'
      WRITE (6, 15) '1. Decimal'
      WRITE (6, 16) '2. Whole Number'
      READ (5, 18) I
      
C     Checks if you entered a valid number
      IF(I .NE. 1 .AND. I .NE. 2) GO TO 99

C     Prints the choice you selected
      WRITE (6, 22) 'You picked:',I

C     Goes to part B if you chose 1 as the option
      IF(I .EQ. 1) GO TO 98

C     This is part A of the program, doing whole numbers

C     Requests a base 10 number and echoes
      WRITE (6, 11) 'Enter the number you would like to convert: '
      READ (5, 12) I
      WRITE (6, 13) I

C     Loop automatically assigns 0 inside each spot of the array
C     and divides IMAX in half so it changes position according to the
C     binary equivelant
      DO 20 M = 1, 15
           NUMBER(M)=0
           IMAX=IMAX/2
C          If statement checks if I is less than Imax, if so continues
C          to the next part of the loop, else the program changes the
C          value in the array to 1 and subtracts IMAX from I
           IF(I .LT. IMAX) GO TO 20
           NUMBER(M)=1
           I = I - IMAX
C          If statment checks to see if ISTART has already been assigned
C          if so continues to loop, if not changes ISTART to whatever
C          position in the loop M is.
           IF(ISTART .NE. 1 .OR. IFLAG .EQ. 1) GO TO 20
           ISTART=M
           IFLAG=1;
20    CONTINUE

C     Implied do loop prints out the 0's and 1's in the array using
C     ISTART to know when the first 1 is.
      WRITE (6,24) (NUMBER(I), I = ISTART,15)
      GO TO 100
      
C     This is where part B of the program, doing a decimal number
98    WRITE (6, 11) 'Enter the number you would like to convert: '
      WRITE (6, 21) 'Warning, you must always lead with a decimal'
C     Reals needed for the floating points
      READ (5, 17) A
      WRITE (6, 19) A
C     Similar to the IMAX above, used to convert from the decimal to
C     the binary equivalent.
      FMAX=1
      
C     Loop automatically assigns 0 inside each spot of the array
C     and divides IMAX in half so it changes position according to the
C     binary equivelant, only uses 6 of the 15 spots of the array.
C     Unlike the one above this loop goes backwards to assign it in
C     a visually logical manner with 6 being the first spot after the
C     decimal.
      DO 30 N = 6 , 1 , -1
         NUMBER(N) = 0
         FMAX=FMAX/2
C          If statement checks if A is less than FMAX, if so continues
C          to the next part of the loop, else the program changes the
C          value in the array to 1 and subtracts FMAX from A
         IF(A .LT. FMAX) GO TO 30
           NUMBER(N)=1
           A = A - FMAX
           ISTART=N
30    CONTINUE

C     This part of the program makes sure that if the number wasn't
C     turned into binary within 6 places but had 0's after a 1, it will
C     print the remaining 0's
      IF(A .EQ. 0)GO TO 31
      ISTART=1
C     Implied do loop prints out the 0's and 1's in the array using
C     ISTART to know if it can cut off the printing early. Stops at
C     1 either way.
31    WRITE (6,23) (NUMBER(I), I=6,ISTART,-1)
      GO TO 100
      
C     Error message if user did not enter a 1 or a 2
99    WRITE(6,11) 'Enter only 1 or 2...exiting'

C     Prints a string with a field width of 45
11    FORMAT (A45)
C     Prints an integer with a field lenght of 5
12    FORMAT (I5)
C     Prints the following string alone with an integer with field
C     length of 5
13    FORMAT ('The base 10 value of the input is: ', I5)
C     Prints a string with a field width of 34
14    FORMAT (A34)
C     Prints a string with a field width of 10
15    FORMAT (A10)
C     Prints a string with a field width of 15
16    FORMAT (A15)
C     Prints a floating point with width 6, 5 spots reserved after the
C     decimal point
17    FORMAT (F6.5)
C     Prints an integer with a field lenght of 1
18    FORMAT (I1)
C     Prints the following string and a floating point with width 6, 5
C     spots reserved after the decimal point
19    FORMAT ('The base 10 value of the input is: ', F6.5)
C     Prints a string with a field lenght of 44
21    FORMAT (A44)
C     Prints a string with a field lenght of 13 and an Integer with a
C     field lenght of 1
22    FORMAT (A13, I1)
C     Prints the following string and a decimal, then the following
C     binary representation of the decimal number with 7 numbers
C     expected and 7 per line
23    FORMAT('The base 2 value of the input is: .', 7(7(I1)))
C     Prints the following string, then prints the binary representation
C     of a base 10 number with 15 numbers expected, 15 per line.
24    FORMAT('The base 2 value of the input is: ', 15(15(I1)))

C     End of program
100   STOP
      END


