CC Programmer: James Read

CC Course: CS 430

CC Date Due: 9/12/2007

CC Language: FORTRAN

CC Compiler used: Force 2.0

CC Source Filename: ProgLang2PART1.f

CC Executable Filename (if any) or None: ProgLang2PART1.exe

CC Professor: Elizabeth Adams, JMU Computer Science Dept.;



CC Description of the program's PURPOSE:

CC Reads in an integer and prints out its base 2 representation.



CC Description of program's required INPUT:

CC Any integer in the range of 1 to 32767



CC Description of program's expected OUTPUT:

CC The integer read in is echoed out with a label
CC The base 2 representation of the integer is printed out with a label.



CC Descriptions of any MODIFICATIONS made in debugging the program:

CC Simple math and many formatting errors.  INTEGER and REAL datatypes were too small to
CC hold the binary representation as a value.  Was forced to store the binary value in an array.



      DIMENSION B(15)

CC Initial prompt
CC Could not figure out how to get keypress echo on same line as prompt
      WRITE (6,10)
  10  FORMAT (1X, ' Please enter an integer between 1 and 32767 ')

      READ (5,20) J
  20  FORMAT (I6)

CC Echo of input with label
      WRITE (6,40) J
  40  FORMAT (1X, ' The base 10 value of the input is:  ', I6)

CC Convert base 10 integer input to base 2
CC Calls TOBINARY subroutine
CC    Passes input J and array B to hold the binary representation
      CALL TOBINARY(J, B)

CC Prints the value of the array formatted to appear as a binary number to screen with label.
CC Uses an implied do loop to print the values of array B
CC The INT() built-in function is used to type the values of array B as integers for properly formatted output.
      WRITE (6,60) (INT(B(I)), I=1,15)
  60  FORMAT (1X, ' The base 2 value of the input is: ', 15I1)


      STOP
      END

CC TOBINARY subroutine
CC Converts base 10 integers from 1 to 32767 into their base 2 representations
CC    Uses arrays to hold the binary value.
CC      INTEGER and REAL datatype max values were too low to hold the binary representation as a value.
CC
CC    Uses IF statement to create a do while loop.
      SUBROUTINE TOBINARY(J, A)
      DIMENSION A(15)
      K = J
      N = 0
      DO 110 I = 1,15,1
      A(I) = 0
 110  CONTINUE
 120  IF (MOD(K,2) .EQ. 0) GO TO 130
      A(15-N) = 1
 130  K = K/2
      N = N+1
      IF (K .NE. 0) GO TO 120
      RETURN
      END
      
