C     Programmer: Zachary Rezin
C     Course: CS-430
C     Date Due: Wednesday, September 12th
C     Language: Fortran
C     Compiler used: salford
C     Source Filename: secondfortran.f
C     Executable Filename: secondfortran.exe
C     Professor: Elizabeth Adams

C     Description of the program's PURPOSE:
C     To convert a base 10 integer and decimal value read in from the user, echoed and the base 2 equivalent displayed

C     Description of program's required INPUT:
C	  The input consists of an integer value between 0 and 32768 and a decimal value between 0 and 1 with 6 places

C     Description of program's expected OUTPUT:
C     The original data values read in from the user are echoed
C	  The coverted data from the original base 10 value to base 2

C     Language Information:
C	  C in column 1 is used for a comment
C	  Labels are placed in lines 1-5
C	  Program statements are placed in lines 7-72
C	  Comments can exceed the program line limit
C	  Variables must have a declared type if name does not follow the following conventions
C	  All the variables (except otherwise declared) in this program named with a beginning letter in the range of A-H, or O-Z are real numbers
C	  All the variables (except otherwise declared) in this program named with a beginning letter in the range of I-N are integers
C	  The variables don't need to be declared if they follow the above conventions because they are by default real or integer depending on their name
C	  DIMENSION indicates that the variable name(s) which follow are arrays
C	  The number in parentheses indicates the size of the array
C	  FORTRAN arrays start at 1, not 0
C	  = is the assignment operator
C	  WRITE is the output statement
C	  6 in WRITE (6,_) is the default output device, the numbers after 6 are the labels for the related FORMAT statements
C	  READ is the input statement
C	  The 5 in the READ statement indicates the default input device
C	  FORMAT statements are not executable and may be placed anywhere in the program
C	  The F format indicates a real number is to be output
C	  The I format indicates an integer is to be output
C	  The format descriptor for a string variable is A
C	  The / in the format descriptor means new line
C	  The 1X in the format descriptor means 1 blank space
C	  The 1st number after the F in the FORMAT statement indicates the width of the variable
C	  The 2nd number after the F in the FORMAT statement indicates numer of places after the decimal point
C	  The format statement places the decimal points even though no delimiters are given
C	  .LT. is Fortrans less than statement, similar syntax used for other relational operators (.LE., .GT., .GE., .EQ., ...)
C	  DO LOOP syntax, DO label variableName = startvalue, endvalue, modificationValue
C	  leaving modificationValue blank means it increments by 1 by default
C	  Continue generally used to mark the end of a DO loop
C	  CALL is used to enter into a subroutine
C	  Return statement is used for function and subroutine
C	  The STOP statement ends the program execution
C	  The END statement marks the end of a compilation unit

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 performed to determine if the numbers entered are outside of the expected range
C	  "Try Again" is printed if incorrect input is entered
C     This program uses the fixed column format required by early FORTRAN
C	  The format descriptor in format statement 5 says print 15 numbers, one integer after another
C	  It is necessary to pass the dimension of the array inside the subroutines
C	  The program reads in values and stores them in an integer or real respectively
C	  Base 2 versions of numbers are still bit by bit in one dimensional arrays
C	  ARRAYSIZE variable is declared as an integer because it does not follow standard naming conventions
C	  ARRAYSIZE, MAX and MIN are changed to the second array size and range for the decimal conversion to base 2
C	  MAX and MIN are used to store the range of values to be expected from the user
C	  

	  DIMENSION IBASE2ARRAY(15)
      DIMENSION IBASE2DECARRAY(6)
	  INTEGER ARRAYSIZE

	  ARRAYSIZE = 15
	  MAX = 32767
      MIN = 1

C	  Integer conversion to base 2      

  60  WRITE (6, 1) "Enter a base 10 integer < 32768 and > 0: "
	  READ (5, 2) INT
      
  1   FORMAT (/1X, A41)
  2	  FORMAT (I5)

C	  Error checking to make sure the input is in the correct range according to the max and min
	  IF ((INT .LE. MAX) .AND. (INT .GE. MIN)) GO TO 50
      WRITE (6, 10) "Try again"
  10  FORMAT (1X, A9)    
	  GO TO 60

  50  WRITE (6, 3) INT
  3   FORMAT (/1X, "The base 10 value of the input is: ", I5)

	  CALL BASE2CONVERSION (INT, IBASE2ARRAY, ARRAYSIZE)

	  WRITE (6, 4) "The base 2 value of the input is: ",
  4   FORMAT (/1X, A34)
  
	  WRITE (6,5) (IBASE2ARRAY(I), I = 15,1,-1)       
  5   FORMAT (1X, (15(I1)))

C	  Decimal conversion to base 2

	  ARRAYSIZE = 6
	  DMAX = 1.
      DMIN = 0.000001

  80  WRITE (6, 6) "Enter base 10 decimal up to 6 places < 1 and > 0: "
	  READ (5, 7) DEC
      
  6   FORMAT (/1X, A50)
  7	  FORMAT (F7.6)

C	  Error checking to make sure the input is in the correct range according to the max and min
	  IF ((DEC .LT. DMAX) .AND. (DEC .GT. DMIN)) GO TO 70
      WRITE (6, 10) "Try again"
	  GO TO 80

  70  WRITE (6, 8) DEC
  8   FORMAT (/1X, "The base 10 value of the input is: ", F7.6)

	  CALL BASE2DECCONVERSION (DEC, IBASE2DECARRAY, ARRAYSIZE)

	  WRITE (6, 4) "The base 2 value of the input is: "
  
	  WRITE (6,9) (IBASE2DECARRAY(I), I = 1,6)       
  9   FORMAT (1X, (6(I1)))
  
      STOP
      
      END

C	  Subroutine converts base 10 integer into base 2
C	  Method used takes 2 to the exponential power of the highest bit (15 - 1) and if that is smaller than the number,
C	  that bit is 1, and the value is subtracted from the remaining integer, otherwise that bit is 0 and no subtraction occurs
C	  This continues until all bits are filled
C	  The parameters are the base 10 integer, the array (to hold the base 2 conversion)
C	  and size of array and are passed by reference
C	  INTARRAY is changed, the INTEG and NUM are not changed
      SUBROUTINE BASE2CONVERSION (INTEG, INTARRAY, NUM)
      
      DIMENSION INTARRAY(NUM)

C	  Setting base 10 integer to a temp variable so as not to change the users input accidentally
	  ITEMP = INTEG

	  DO 40 M = NUM, 1, -1

C	  INTEGER EXPONENT VARIABLE
	  IEXP = 2 ** (M - 1)

      IF (ITEMP .LT. IEXP) GO TO 42
      ITEMP = ITEMP - IEXP
      INTARRAY(M) = 1
	  GO TO 40
  
  42  INTARRAY(M) = 0    

  40  CONTINUE
  
      RETURN
      END

C	  Subroutine converts base 10 decimal into base 2
C	  Method used was to multiply the decimal by 2, if answer is greater than 1, than that bit is one, otherwise 0
C	  If bit is changed to 1, than 1 is subtracted from the answer and process continues the length of the array times
C	  The parameters are the base 10 decimal, the array (to hold the base 2 conversion)
C	  and size of array and are passed by reference
C	  DECARRAY is changed, the DECIM and NUM are not changed
      SUBROUTINE BASE2DECCONVERSION (DECIM, IDECARRAY, NUM)
      
      DIMENSION IDECARRAY(NUM)

C	  Setting base 10 decimal to a temp variable so as not to change the users input accidentally
	  TEMP = DECIM

	  DO 90 M = 1, NUM

	  VALUE = TEMP * 2

      IF (VALUE .GE. 1) GO TO 92
      TEMP = VALUE
      IDECARRAY(M) = 0
	  GO TO 90
  
  92  IDECARRAY(M) = 1
      TEMP = VALUE - 1    

  90  CONTINUE

      RETURN
      END