How to resolve the algorithm Loops/Nested step by step in the XBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Nested step by step in the XBasic programming language
Table of Contents
Problem Statement
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over
[ 1 , … , 20 ]
{\displaystyle [1,\ldots ,20]}
. The loops iterate rows and columns of the array printing the elements until the value
20
{\displaystyle 20}
is met. Specifically, this task also shows how to break out of nested loops.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Nested step by step in the XBasic programming language
Source code in the xbasic programming language
PROGRAM "loopsnested"
IMPORT "xst" ' for XstGetSystemTime
DECLARE FUNCTION Entry()
' Pseudo-random number generator
' Based on the rand, srand functions from Kernighan & Ritchie's book
' 'The C Programming Language'
DECLARE FUNCTION Rand()
DECLARE FUNCTION SRand(seed%%)
FUNCTION Entry()
DIM array%[10, 10]
XstGetSystemTime (@msec)
SRand(INT(msec) MOD 32768)
FOR row% = 0 TO 10
FOR col% = 0 TO 10
array%[row%, col%] = INT(Rand() / 32768.0 * 20.0) + 1
NEXT col%
NEXT row%
isFound% = $$FALSE
FOR row% = 0 TO 10
PRINT "Row:"; row%
FOR col% = 0 TO 10
PRINT " Col:"; col%; ", value:"; array%[row%, col%]
IF array%[row%, col%] = 20 THEN
isFound% = $$TRUE
EXIT FOR
END IF
NEXT col%
IFT isFound% THEN
EXIT FOR
END IF
NEXT row%
END FUNCTION
' Return pseudo-random integer on 0..32767
FUNCTION Rand()
#next&& = #next&& * 1103515245 + 12345
END FUNCTION USHORT(#next&& / 65536) MOD 32768
' Set seed for Rand()
FUNCTION SRand(seed%%)
#next&& = seed%%
END FUNCTION
END PROGRAM
You may also check:How to resolve the algorithm Bioinformatics/Global alignment step by step in the Phix programming language
You may also check:How to resolve the algorithm Window creation step by step in the Phix programming language
You may also check:How to resolve the algorithm Stack step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the Peloton programming language