This program simulates rolling a six sided dice, each side has 1 to 6 dots.
A D V E R T I S E M E N T
C
The number of times the die is tossed is given by the integer
C variable: roll.
C
C
The result of an individual toss (a 1 or
C 2,..or 6 dots) is given by the integer variable: dice.
C
C
The real variable count is an ARRAY with six entries.
C
Count keeps track of the number of times a 1 (2,etc) was rolled.
C
For example if array(1)=5 and array(2)=7 then a one was
C
rolled 5 times and a two was filled 7 times.
C
C
FORTRAN is based on lines and columns, usually one instruction per line.
C
In FORTRAN computer instructions start in column 7.
C
In FORTRAN a C in column 1 flags this line as a comment.
C234567
C
In FORTRAN a character in column 6 flags this as a continuation line.
C
to assume that it is a comment line and ignore it.
C
lines in RED are the actual Fortran code.
implicit none
character key
real count(6), RAN
integer i, dice, roll, seed
C
C
Special precaution in integer division: dividing one integer
C
by another integer gives another integer, e.g. i = 3, j = 2,
C
then c = i/j = 1.0. To get the correct answer, define i and
C
j as real numbers.
C
C
RAN is a computer function that gives us a random number in
C
the range [0,1). Initialize the random number generator RAN
C
with a large odd integer seed.
C
seed = 432211111
10 continue
C
"10" is the statement number of the continue command.
C
Statement numbers should start at column 2 or later and end at column 5.
C
All statement numbers must be a unique integer.
C
C
Initialization of variables C
Here we have a do loop, which repeats itself 6 times
do dice = 1, 6
count(dice) = 0
end do
C
Write to unit 6 (screen) and read from unit 5 (keyboard).
write(6,*)'How many rolls of dice :'
read(5,*)roll
do i = 1, roll
dice = INT(1 + 6*RAN(seed))
count(dice) = count(dice) + 1
end do
do i = 1, 6
write(6,*)i, count(i)
end do
C
The ">" character at 6th character location indicates
C
continuation of a program line.
C
write(6,*) �Hit <return> to continue or type q or Q',
> ' and <return> to quit the program.'
read(5,20)key
C
Define key as a one character variable ("a1"). If you use
C
* instead of 20, you do not explicitly specify the format.
20 format(a1)
if (key.ne.'q'.and.key.ne.'Q') go to 10
end
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
fortran arrays,fortran data statements,fortran common blocks,fortran file i/o,fortran debugging,fortran format statements,fortran loops,fortran sub programs,fortran example programs