How to resolve the algorithm Loops/Break step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Break step by step in the Fortran programming language
Table of Contents
Problem Statement
Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive).
If a number is 10, stop the loop after printing it, and do not generate any further numbers.
Otherwise, generate and print a second random number before restarting the loop.
If the number 10 is never generated as the first number in a loop, loop forever.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Break step by step in the Fortran programming language
Source code in the fortran programming language
program Example
implicit none
real :: r
integer :: a, b
do
call random_number(r)
a = int(r * 20)
write(*,*) a
if (a == 10) exit
call random_number(r)
b = int(r * 20)
write(*,*) b
end do
end program Example
PROGRAM LOOPBREAK
INTEGER I, RNDINT
C It doesn't matter what number you put here.
CALL SDRAND(123)
C Because FORTRAN 77 semantically lacks many loop structures, we
C have to use GOTO statements to do the same thing.
10 CONTINUE
C Print a random number.
I = RNDINT(0, 19)
WRITE (*,*) I
C If the random number is ten, break (i.e. skip to after the end
C of the "loop").
IF (I .EQ. 10) GOTO 20
C Otherwise, print a second random number.
I = RNDINT(0, 19)
WRITE (*,*) I
C This is the end of our "loop," meaning we jump back to the
C beginning again.
GOTO 10
20 CONTINUE
STOP
END
C FORTRAN 77 does not come with a random number generator, but it
C is easy enough to type "fortran 77 random number generator" into your
C preferred search engine and to copy and paste what you find. The
C following code is a slightly-modified version of:
C
C http://www.tat.physik.uni-tuebingen.de/
C ~kley/lehre/ftn77/tutorial/subprograms.html
SUBROUTINE SDRAND (IRSEED)
COMMON /SEED/ UTSEED, IRFRST
UTSEED = IRSEED
IRFRST = 0
RETURN
END
INTEGER FUNCTION RNDINT (IFROM, ITO)
INTEGER IFROM, ITO
PARAMETER (MPLIER=16807, MODLUS=2147483647, &
& MOBYMP=127773, MOMDMP=2836)
COMMON /SEED/ UTSEED, IRFRST
INTEGER HVLUE, LVLUE, TESTV, NEXTN
SAVE NEXTN
IF (IRFRST .EQ. 0) THEN
NEXTN = UTSEED
IRFRST = 1
ENDIF
HVLUE = NEXTN / MOBYMP
LVLUE = MOD(NEXTN, MOBYMP)
TESTV = MPLIER*LVLUE - MOMDMP*HVLUE
IF (TESTV .GT. 0) THEN
NEXTN = TESTV
ELSE
NEXTN = TESTV + MODLUS
ENDIF
IF (NEXTN .GE. 0) THEN
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + IFROM
ELSE
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + ITO + 1
ENDIF
RETURN
END
SUBROUTINE RANDU(IX,IY,YFL)
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60.
CAUTION! This routine's 32-bit variant is reviled by Prof. Knuth and many others for good reason!
IY = IX*899
IF (IY) 5,6,6
5 IY = IY + 32767 + 1
6 YFL = IY
YFL = YFL/32767.
END
FUNCTION IR19(IX)
CALL RANDU(IX,IY,YFL)
IX = IY
I = YFL*20
IF (I - 20) 12,11,11
11 I = 19
12 IR19 = I
END
IX = 1
Commence the loop.
10 I = IR19(IX)
WRITE (6,11) I
11 FORMAT (I3)
IF (I - 10) 12,20,12
12 I = IR19(IX)
WRITE (6,11) I
GO TO 10
Cease.
20 CONTINUE
END
You may also check:How to resolve the algorithm One of n lines in a file step by step in the Rust programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Nim programming language
You may also check:How to resolve the algorithm Munching squares step by step in the Nim programming language