faqts : Computers : Programming : Languages : Bbcbasic

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

BBCBASIC: Windows: Array: Maximum: How to determine the maximum value of an array?

Feb 2nd, 2006 17:30
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 02 February 2006 - 11:04 pm -------------------

BBCBASIC: Windows: Array: Maximum: How to determine the maximum value 
of an array?

===

Method: Set the maximum at first to a smallest value possible

---

Steps: Overview:

 1. -Let a variable contain the maximum

 2. -Set this variable equal to some minimum value
     (e.g. the minimum 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 greater than the current maximum
             set the maximum equal to this value

     2. -Endfor

 4. The resulting value will be the maximum

===

Steps: Worked out:

 1. -Create the following program

--- cut here: begin --------------------------------------------------

 maxR = 0
 minI% = 1
 maxI% = 5
 I% = 0
 :
 DIM xR( maxI% )
 :
 FOR I% = minI% TO maxI%
   READ xR( I% )
 NEXT I%
 :
 REM determine the maximum
 maxR = -1E9 : REM initialize maximum to a smallest minimum value
 :
 FOR I% = minI% TO maxI%
   IF ( xR( I% ) > maxR ) THEN maxR = xR( I% )
 NEXT I%
 :
 PRINT "maximum = "; maxR
 :
 END
 :
 :
 :
 DATA 43
 DATA 2
 DATA 789
 DATA 9999
 DATA 6
 :

--- cut here: end ----------------------------------------------------

 2. -If you run this program it will show

      maximum = 9999

===

Method: Set the maximum 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 --------------------------------------------------

 maxR = 0
 minI% = 1
 maxI% = 5
 I% = 0
 :
 DIM xR( maxI% )
 :
 FOR I% = minI% TO maxI%
   READ xR( I% )
 NEXT I%
 :
 REM determine the maximum
 maxR = xR( minI% ) : REM set maximum at first equal to first element
 :
 FOR I% = minI% TO maxI%
   IF ( xR( I% ) > maxR ) THEN maxR = xR( I% )
 NEXT I%
 :
 PRINT "maximum = "; maxR
 :
 END
 :
 :
 :
 DATA 43
 DATA 2
 DATA 789
 DATA 9999
 DATA 6
 :

--- cut here: end ----------------------------------------------------

 2. -If you run this program it will show

      maximum = 9999

===

In the following example you pass your array to a function
(using string concatenation)

--- cut here: begin --------------------------------------------------

 minI% = 1
 maxI% = 5
 I% = 0
 :
 DIM xR( maxI% )
 :
 FOR I% = minI% TO maxI%
   READ xR( I% )
 NEXT I%
 :
 PRINT "maximum = "; FNArrayGetMaximumR( minI%, maxI%, "xR(", ")" )
 :
 END
 :
 :
 :
 REM library: determine the maximum
 DEF FNArrayGetMaximumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
 LOCAL maxR
 LOCAL xR
 maxR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set 
maximum at first equal to first element
 :
 FOR I% = minI% TO maxI%
   xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
   IF xR > maxR THEN maxR = xR
 NEXT I%
 = maxR
 :
 DATA 43
 DATA 2
 DATA 789
 DATA 9999
 DATA 6
 :

--- cut here: end ----------------------------------------------------

 2. -If you run this program it will show

      maximum = 9999

===

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 "maximum = "; FNArrayGetMaximumR( minI%, maxI%, "xR(", ")" )
 :
 END
 :
 :
 :
 REM library: determine the maximum
 DEF FNArrayGetMaximumR( minI%, maxI%, arrayBegin$, arrayEnd$ )
 LOCAL maxR
 LOCAL xR
 maxR = EVAL( arrayBegin$ + STR$( minI% ) + arrayEnd$ ) : REM set 
maximum at first equal to first element
 :
 FOR I% = minI% TO maxI%
   xR = EVAL( arrayBegin$ + STR$( I% ) + arrayEnd$ )
   IF xR > maxR THEN maxR = xR
 NEXT I%
 = maxR
 :
 DATA 43
 DATA 2
 DATA 789
 DATA 9999
 DATA 6
 :

--- cut here: end ----------------------------------------------------

 2. -If you run this program it will show

      maximum = 9999



---
---

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

----------------------------------------------------------------------