Question

In: Computer Science

Write a Fortran program which simulates placing 100 molecules into the boxes of a 20 by...

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.

Solutions

Expert Solution

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:


Related Solutions

Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
Write a FORTRAN program that computes the average of a collection of numbers and then outputs...
Write a FORTRAN program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Write a Fortran program that reads in two NxN matrices A & B, and prints their...
Write a Fortran program that reads in two NxN matrices A & B, and prints their element-wise sum (A+B), element-wise difference (A-B), element-wise division (A/B), element-wise product (A*B), and their matrix product (matmul(A,B)), on the standard output.
Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT