In: Computer Science
Write a Fortran program which simulates placing 100 molecules into the boxes of a 20 by 20 square grid. Each box can hold at most one molecule. Your program should count and report how many molecules in the final arrangement have no neighbors. Two molecules are considered neighbors if they are directly above or below or directly side by side (diagonals don't count). For instance, if molecules were placed at the locations labelled by letters in the following 5 by 5 grid:
* * * * b a * d c * * * * * * * f * * e * * * * g
we would say that d and c are neighbors and e and g are neighbors, but b and c are not. In this example, the three molecules a, b, and f have no neighbors. Your program would report that 3 of the 7 molecules are isolated. Your program should perform the experiment of placing 100 molecules into an empty lattice and reporting the results five times. The results should be neatly displayed in some readable format. Your final output should not include the full picture of the lattice (although that might be useful during debugging).
This problem has many natural opportunities to use subroutines and functions. In particular, I would like your program to use a subroutine MOLPUT to place the molecules into the grid. An easy way to represent the grid is an integer array where 0 represents empty and 1 represents a molecule at that location. MOLPUT should take a single argument, which is a 20 by 20 integer array, and return with one additional molecule added to the array. This subroutine, therefore, needs to generate random subscripts until it finds an empty position in the array and then mark that a molecule has been placed there. To perform an experiment, you can then just clear the array, call MOLPUT 100 times, and then check the results.
Solution:
Working code implemented in FOTRAN and appropriate comments provided for better understanding.
main.f Code:
REAl G(22,22)
INTEGER MOLCOUNT
X = RAND(TIME())
Y = RAND(TIME())
DO 2 J=1,5
COUNT=0
CALL INIT(G,22)
DO 1 I=1,100
CALL MOLPUT(G,X,Y)
1 CONTINUE
CALL SINGLE(G,22,COUNT)
CALL INIT(G,22)
2 CONTINUE
END
C This subroutine intiolise the 2D array with all zeros
SUBROUTINE INIT(A,N)
REAL A(N,N)
DO 3 I=1,N
DO 3 J=1,N
A(I,J)=0
3 CONTINUE
RETURN
END
C This subroutine puts a molecule in a random spot in the 2D
array
SUBROUTINE MOLPUT(A,X,Y)
REAL A(22,22)
INTEGER XCORD
INTEGER YCORD
4 X = RAND(0)
Y = RAND(0)
XCORD = 19*X+3.0
YCORD = 19*Y+3.0
IF(A(XCORD,YCORD) == 1) GOTO 4
A(XCORD,YCORD) = 1.0
CONTINUE
RETURN
END
C Thsi Subroutine finds all of the isolated molecules and displays
the number
SUBROUTINE SINGLE(A,N,COUNT)
REAL A(N,N)
INTEGER COUNT = 0
DO 5 I=2, N-1
DO 5 J=2, N-1
IF( (A(I,J) == 0)) GOTO 5
IF (A(I+1,J) == 0 .AND. A(I-1, J) == 0 .AND. A(I,J+1) == 0
& .AND. A(I,J-1) == 0) COUNT = COUNT + 1
A(I,J) = 2.0
5 CONTINUE
PRINT *, "Isolated molecules: " , COUNT
RETURN
END
C OutPut: Isolated molecules: 25.0000000
C Isolated molecules: 24.0000000
C Isolated molecules: 38.0000000
C Isolated molecules: 24.0000000
C Isolated molecules: 33.0000000
Code Screenshots: