Entry
BBCBASIC: Windows: Array: Minimum: How to determine the minimum value of an array?
Feb 2nd, 2006 17:55
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 02 February 2021 - 11:33 pm -------------------
BBCBASIC: Windows: Array: Minimum: How to determine the minimum value
of an array?
===
Method: Set the minimum at first to a largest value possible
---
Steps: Overview:
1. -Let a variable contain the minimum
2. -Set this variable equal to some maximum value
(e.g. the maximum value possible for this variable
type (e.g. integer))
3. -Search linearly through the array
(and so check against all values in the array)
1. -For first to last value in the array
1. -Read the value
2. -If this value is smaller than the current minimum
set the minimum equal to this value
2. -Endfor
4. The resulting value will be the minimum
===
Steps: Worked out:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minR = 0
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI% )
:
FOR I% = minI% TO maxI%
READ xR( I% )
NEXT I%
:
REM determine the minimum
minR = +1E9 : REM initialize minimum to a largest minimum value
:
FOR I% = minI% TO maxI%
IF ( xR( I% ) < minR ) THEN minR = xR( I% )
NEXT I%
:
PRINT "minimum = "; minR
:
END
:
:
:
DATA 43
DATA 2
DATA 789
DATA 9999
DATA 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
minimum = 2
===
Method: Set the minimum at first equal to the first element in the
array
This method has as an advantage that it is directly dependent
on the dataset.
===
Steps: Worked out:
1. -Create the following program
--- cut here: begin --------------------------------------------------
minR = 0
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI% )
:
FOR I% = minI% TO maxI%
READ xR( I% )
NEXT I%
:
REM determine the minimum
minR = xR( minI% ) : REM set minimum at first equal to first element
:
FOR I% = minI% TO maxI%
IF ( xR( I% ) < minR ) THEN minR = xR( I% )
NEXT I%
:
PRINT "minimum = "; minR
:
END
:
:
:
DATA 43
DATA 2
DATA 789
DATA 9999
DATA 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
minimum = 2
===
In the following example you pass your array to a function
(using direct passing)
--- cut here: begin --------------------------------------------------
minI% = 1
maxI% = 5
I% = 0
:
DIM xR( maxI% )
:
FOR I% = minI% TO maxI%
READ xR( I% )
NEXT I%
:
PRINT "minimum = "; FNArrayGetMinimumR( minI%, maxI%, "xR(", ")" )
:
END
:
:
:
REM library: determine the minimum
DEF FNArrayGetMinimumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
LOCAL minR
LOCAL xR
minR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set
minimum at first equal to first element
:
FOR I% = minI% TO maxI%
xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
IF xR < minR THEN minR = xR
NEXT I%
= minR
:
DATA 43
DATA 2
DATA 789
DATA 9999
DATA 6
:
--- cut here: end ----------------------------------------------------
2. -If you run this program it will show
minimum = 2
---
---
Internet: see also:
---
BBCBASIC: Windows: Array: Function: How to pass an array to a function?
http://www.faqts.com/knowledge_base/view.phtml/aid/39453/fid/768
----------------------------------------------------------------------