! Programmer: Tomasz Pawel Kurzawa
! Course: CS530 Programing Languages
! Date Due: 12th Sept 2007
! Language: Fortran
! Compiler used: Force 2.0
! Source Filename: Assignment2.f
! Executable Filename (if any) or None:
! Professor: Elizabeth S. Adams

! Description of the program's PURPOSE:
! This program demonstrates how to read input provided by user as well as how to format it and print the output of program.
! Program also presents how to convert integer number and fraction represented in decimal form to binary form

! Description of program's required INPUT:
! First part of program requires as an input integer number between 1 to 32767.
! Second part of program requires as an input a decimal fraction between 0 and 1

! Description of program's expected OUTPUT:
! Labeled information about inputed integer and fraction in decimal form.
! Labeled result of conversion integer number and fraction to binary.

! Descriptions of any MODIFICATIONS made in debugging the program:

! General COMMENTS about the PROGRAM

! General COMMENTS explaining the syntax or features of the LANGUAGE
! MOD(x,y) is a build in function , returns remainder x - INT(x/y)*y
! Arithmetic IF statement is   IF (e) s1, s2, s3   where e is an integer, real, or double precision expression
! s1, s2, and s3 are each the statement label of an executable statement that appears in the same program unit as the arithmetic IF statement
! Variables of type REAL do not have to be defined if their names begin with letters A-H, or O-Z because they are by default real
! If name of variable starts with letter I-N and its going to be REAL variable it should be defined
! DIMENSION indicates  that the variable name which follow are arrays
! The number in parenthesis  after the name of array indicates the size of the array
! = is the assignment operator
! READ is the input statement
! 5 in READ(5,...) is the default input device
! The numbers after 5, in READ statement are the labels for the related FORMAT statement
! WRITE is the output statement
! 6 in WRITE(6,...) is the default output device
! The numbers after 6, in WRITE statement are the labels for the related FORMAT statement
! All write statements should have 1X as the first statement in the format list eg. FORMAT (1X,...) which means 1 blank space.
! Real number to be input or output is indicated by the F format.
! Integer number to be input or output is indicated by the I format.
! Number before F indicates  how many numbers is expected.
! In the FORMAT statement first number after F indicates the 10 width of variable, 2nd indicates number of places after the decimal point
! The '/' in FORMAT statements indicates in case of READ that the rest of line should not be read, while in case of WRITE indicates empty line.
! Loop statement has form DO label varName = startValue, endValue, modificationValue
! Conditional statement IF( Boolean condition) THEN ... ELSE ... ENDIF
! The RETURN statement returns from function or subroutine, is executable statement
! The STOP statement ends the execution of the program
! The END statement marks the end of a compilation unit


! ******************************************************************** !
!       MAIN PROGRAM
! ******************************************************************** !

! ******************************************************************** !
!       FIRST PART OF PROGRAM
! ******************************************************************** !

! ******************************************************************** !
!       Variables definition
! ******************************************************************** !
! Definition of integer array BINARY, that will store binary (integer 0
! or 1) representation of number
! Variable COUNTER used as temporary variable to specify location in
! BINARY array
        INTEGER BINARY(15),COUNTER
  ! ******************************************************************** !
!       Variables definition
! ******************************************************************** !
! Definition of integer array FRACTION_BINARY that will store binary
! representation of fraction
! Variable COUNTER used as temporary variable to specify location in
! DECIMAIL_BINARY array
        INTEGER FRACTION_BINARY(6),FRACTION_COUNTER
        REAL NUMBER, NUM
! ******************************************************************** !
!       Introductory information for user
! ******************************************************************** !
        WRITE(6,20)
  20    FORMAT(1X,'CS530 - Programing Languages'/' Programming Assignmen
     6t#2 - FORTRAN'//' Work performed under JMU Honor Code'/' Author: T
     6omasz Pawel Kurzawa'///' Please enter a positive integer number be
     6tween 1 to 32767'/)

! ******************************************************************** !
!       Definition of format for reading user input
! ******************************************************************** !
! Format of reading user five digit integer number input
  12    FORMAT(I5)

! ******************************************************************** !
!       Operation of reading user input
! ******************************************************************** !
! Reading user input number, with specfied format, and assign read value
! to variable J of integer type
        READ (5,12) J

! ******************************************************************** !
!       Definition of format for labeling and writing user input
! ******************************************************************** !
! Format for labeling and writing five-digit integer number
  15    FORMAT(1X, /'The base 10 value of the input is:',I5)

! ******************************************************************** !
!       Definition of format for labeling and writing binar output
! ******************************************************************** !
! Format for labeling and writing up to 15 one-digit integer numbers,
! binary representation of input number
  16    FORMAT (1X, //'The base  2 value of the input is:', 15(I1))

! ******************************************************************** !
!       Operation of checking if input is valid
! ******************************************************************** !
! If value inserted by user is less than or equal to 0 than go to label
! 100 otherwise go to label 120
        IF (J) 100,100,120
! In case value inserted is less than or equal to 0 , inform user about
! it
  100   WRITE(6,13)
  13    FORMAT(1X, 'Integer number should be greater than 0')
! Finish execution of the program
        GO TO 200
! If number inserted by user is less than 32767 execute conversion
  120   IF(J.LT.32767)THEN

! ******************************************************************** !
!       Conversion of decimal integer number to binary representation
! ******************************************************************** !
! Initialize counter
        COUNTER=16
! Assign orginal decimal value to variable K; Valu of J will change
! during conversion
        K=J
! Do unitl J is not equal to 0
        DO WHILE (J .NE. 0)
! Decrement counter; start location of an array is 15
             COUNTER=COUNTER-1
! Assign value of MOD operation to BINARY array cell pointed by COUNTER
             BINARY(COUNTER)=MOD(J,2)
! Divide current J value by 2
             J = J/2
        ENDDO

! ******************************************************************** !
!       Wrting output of the program
! ******************************************************************** !
        WRITE(6,40)
   40   FORMAT(1X,'*****************************************************
     6************************* '/' **************************** RESULTS
     6 FOR PART A  *****************************'/)
! Write decimal value of integer number provided to program as an input
        WRITE (6,15)   K
! Write binary representation of integer number
        WRITE (6,16) (BINARY(I), I = COUNTER,15)

 ! If number inserted by user is not less than 32767, inform user about
 ! it, and finish execution of program
        ELSE
        WRITE(6,17)
  17    FORMAT(1X, 'Integer number should be LESS than 32767')
        ENDIF

! ******************************************************************** !
!       SECOND PART OF PROGRAM
! ******************************************************************** !

! ******************************************************************** !
!       Ask for input from user
! ******************************************************************** !
        WRITE(6,21)
  21    FORMAT(1X,///'Please enter a positive fraction between .0 to .99
     69999.. : ')

! ******************************************************************** !
!       Definition of format for reading user input
! ******************************************************************** !
! Format of reading fraction provided by user
! User should insert fraction starting with decimal point eg. ".5"
  22    FORMAT(F11.10)

! ******************************************************************** !
!       Operation of reading user input
! ******************************************************************** !
! Reading user input number, with specfied format, and assign read value
! to variable NUMBER of real type
        READ (5,22) NUMBER

! ******************************************************************** !
!       Definition of format for labeling and writing user input
! ******************************************************************** !
! Format for labeling and writing fraction provided by user as an input
! to the program
  25    FORMAT(1X, /'The base 10 value of the input is:', F6.5)

! ******************************************************************** !
!       Definition of format for labeling and writing binar output
! ******************************************************************** !
! Format labeling and writting the 2 base representation of fraction,
! represented by 6 one digit integer numbers
  26    FORMAT (1X,//'The base  2 value of the input is: 0.', 6(I1))

! ******************************************************************** !
!       Conversion of fraction to binary representation
! ******************************************************************** !

! Initialization of variable counter
        FRACTION_COUNTER=0
! Assignment operation, not to loose value of number inserted by user
! during conversion operations
        NUM=NUMBER

! DO loop until NUM is not equal to 0, otherwise finish conversion
        DO WHILE (NUM .NE. 0)
! Increment counter by one each time in loop
             FRACTION_COUNTER=FRACTION_COUNTER+1
! Multiply NUM by 2
             NUM=NUM*2
! Check if the result of multiplication is grater than 1
             IF(NUM.GE.1)THEN
! If result of multiplication is grater than 1
! Insert 1 to FRACTION_BINARY array
                FRACTION_BINARY(FRACTION_COUNTER)=1
! Subtract 1 from result of multiplication
                NUM=NUM-1
             ELSE
! If result of multiplication is less than 1
! Insert 0 to FRACTION_BINARY
                FRACTION_BINARY(FRACTION_COUNTER)=0
             ENDIF

! If binary fraction is represented by 6 digits terminate
             IF(FRACTION_COUNTER.EQ.6)THEN
! Assign NUM zero causing end of DO loop
                 NUM=0
             ENDIF
        ENDDO

! ******************************************************************** !
!       Wrting output of the program
! ******************************************************************** !
        WRITE(6,41)
   41   FORMAT(1X,/' ***************************************************
     6*************************** '/' **************************** RESUL
     6TS FOR PART B  *****************************'/)
! Write fraction inserted by user in 10 base
        WRITE (6,25)   NUMBER
! Write fraction in 2 base representation
        WRITE (6,26) (FRACTION_BINARY(I), I = 1,FRACTION_COUNTER)

! ******************************************************************** !
!       End of program
! ******************************************************************** !
 200    STOP
        END
