User Tools

Site Tools


using_20array_20pointers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
using_20array_20pointers [2018/03/31 13:19] – external edit 127.0.0.1using_20array_20pointers [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 //by Richard Russell, December 2009//\\ \\  In //BBC BASIC for Windows// you can perform operations on **entire arrays**, such as arithmetic operations and the [[http://en.wikipedia.org/wiki/Dot_product|dot product]]. For example:\\ \\  //by Richard Russell, December 2009//\\ \\  In //BBC BASIC for Windows// you can perform operations on **entire arrays**, such as arithmetic operations and the [[http://en.wikipedia.org/wiki/Dot_product|dot product]]. For example:\\ \\ 
 +<code bb4w>
         a() = a() + b() + c()         a() = a() + b() + c()
         d() = e() * f()         d() = e() * f()
         g() = h() . i()         g() = h() . i()
         j() = 1, 2, 3, 4, 5         j() = 1, 2, 3, 4, 5
 +</code>
 This is fine so long as the arrays you want to operate on are known in advance, but suppose you have a large number of arrays and you want to determine **at run time** which is acted upon.\\ \\  Ideally what you might like to have is an **array of arrays**, then you could access the required array by means of an index, but //BBC BASIC for Windows// does not provide that facility. At first sight it would seem that a workaround would be to use an **array of structures**, each of which itself contains an array, but that won't work either because the **entire array** operations such as those listed above //cannot be used with structure members//.\\ \\  However, all is not lost. Although you cannot use an **array of arrays** or an **array of structures** what you //can// use is an **array of array-pointers**. This allows you to achieve effectively what is required; performing an **entire array** operation on an array which is determined at run time by means of an index.\\ \\  The main complication of this approach is that all operations on the individual arrays must be hived off into PROCedures, but this need not necessarily be too troublesome.\\ \\  The code below illustrates such an approach. Firstly the array of array-pointers is declared to have the necessary number of elements:\\ \\  This is fine so long as the arrays you want to operate on are known in advance, but suppose you have a large number of arrays and you want to determine **at run time** which is acted upon.\\ \\  Ideally what you might like to have is an **array of arrays**, then you could access the required array by means of an index, but //BBC BASIC for Windows// does not provide that facility. At first sight it would seem that a workaround would be to use an **array of structures**, each of which itself contains an array, but that won't work either because the **entire array** operations such as those listed above //cannot be used with structure members//.\\ \\  However, all is not lost. Although you cannot use an **array of arrays** or an **array of structures** what you //can// use is an **array of array-pointers**. This allows you to achieve effectively what is required; performing an **entire array** operation on an array which is determined at run time by means of an index.\\ \\  The main complication of this approach is that all operations on the individual arrays must be hived off into PROCedures, but this need not necessarily be too troublesome.\\ \\  The code below illustrates such an approach. Firstly the array of array-pointers is declared to have the necessary number of elements:\\ \\ 
 +<code bb4w>
         number_of_arrays = 10         number_of_arrays = 10
         DIM array_pointer(number_of_arrays-1)         DIM array_pointer(number_of_arrays-1)
 +</code>
 Next the individual arrays are DIMensioned; this must be done within a procedure (it is assumed here that all the arrays will be dimensioned identically, which will usually be the case):\\ \\  Next the individual arrays are DIMensioned; this must be done within a procedure (it is assumed here that all the arrays will be dimensioned identically, which will usually be the case):\\ \\ 
 +<code bb4w>
         FOR index% = 0 TO number_of_arrays-1         FOR index% = 0 TO number_of_arrays-1
           PROCdimarray(array_pointer(index%))           PROCdimarray(array_pointer(index%))
Line 15: Line 20:
  
         DEF PROCdimarray(RETURN array()) : DIM array(1,2) : ENDPROC         DEF PROCdimarray(RETURN array()) : DIM array(1,2) : ENDPROC
 +</code>
 Here the procedure is shown 'inline' for convenience; in practice it would more likely be placed out of harm's way at the end of the program. Of course the dimensions **(1,2)** are simply illustrative; the arrays can be dimensioned however is required.\\ \\  Having declared the arrays, one can perform operations on them. Suppose one wants to initialise each array with the same set of values:\\ \\  Here the procedure is shown 'inline' for convenience; in practice it would more likely be placed out of harm's way at the end of the program. Of course the dimensions **(1,2)** are simply illustrative; the arrays can be dimensioned however is required.\\ \\  Having declared the arrays, one can perform operations on them. Suppose one wants to initialise each array with the same set of values:\\ \\ 
 +<code bb4w>
         DIM initial(1,2)         DIM initial(1,2)
         initial() = 1, 2, 3, 4, 5, 6         initial() = 1, 2, 3, 4, 5, 6
Line 23: Line 30:
  
         DEF PROCinitarray(RETURN a(), i()) : a() = i() : ENDPROC         DEF PROCinitarray(RETURN a(), i()) : a() = i() : ENDPROC
 +</code>
 As before, the procedure is shown inline purely for convenience.\\ \\  If one wished to initialise the arrays differently then that can be achieved in a similar way:\\ \\  As before, the procedure is shown inline purely for convenience.\\ \\  If one wished to initialise the arrays differently then that can be achieved in a similar way:\\ \\ 
 +<code bb4w>
         DIM initial(1,2)         DIM initial(1,2)
         FOR index% = 0 TO number_of_arrays-1         FOR index% = 0 TO number_of_arrays-1
Line 29: Line 38:
           PROCinitarray(array_pointer(index%), initial())           PROCinitarray(array_pointer(index%), initial())
         NEXT         NEXT
 +</code>
 Of course there's no fundamental reason why the initial values need to be passed in another array **initial()**, they could (if there are relatively few elements) be passed as individual parameters to the procedure.\\ \\  Now suppose one wants to do something a little more complicated, like adding two of the arrays together:\\ \\  Of course there's no fundamental reason why the initial values need to be passed in another array **initial()**, they could (if there are relatively few elements) be passed as individual parameters to the procedure.\\ \\  Now suppose one wants to do something a little more complicated, like adding two of the arrays together:\\ \\ 
 +<code bb4w>
         dst% = 3         dst% = 3
         src% = 6         src% = 6
Line 35: Line 46:
  
         DEF PROCaddarray(RETURN dst(), RETURN src()) : dst() += src() : ENDPROC         DEF PROCaddarray(RETURN dst(), RETURN src()) : dst() += src() : ENDPROC
 +</code>
 This adds the array with index **src%** to the array with index **dst%**.\\ \\  If you want to perform a calculation on an array which returns a scalar value, a convenient way is to use a function rather than a procedure:\\ \\  This adds the array with index **src%** to the array with index **dst%**.\\ \\  If you want to perform a calculation on an array which returns a scalar value, a convenient way is to use a function rather than a procedure:\\ \\ 
 +<code bb4w>
         DIM result(number_of_arrays-1)         DIM result(number_of_arrays-1)
         FOR index% = 0 TO number_of_arrays-1         FOR index% = 0 TO number_of_arrays-1
Line 42: Line 55:
  
         DEF FNmod(RETURN array()) = MOD(array())         DEF FNmod(RETURN array()) = MOD(array())
 +</code>
 Hopefully you can now see a pattern emerging. Whenever you want to perform an operation on one or more of the arrays, you must put that operation in a procedure (or function, if appropriate) and call that procedure or function with one or more parameters which are //pointers// to the array(s). The procedure or function receives the array(s) as **formal** parameters and performs the necessary operations. Hopefully you can now see a pattern emerging. Whenever you want to perform an operation on one or more of the arrays, you must put that operation in a procedure (or function, if appropriate) and call that procedure or function with one or more parameters which are //pointers// to the array(s). The procedure or function receives the array(s) as **formal** parameters and performs the necessary operations.
using_20array_20pointers.1522502388.txt.gz · Last modified: 2024/01/05 00:16 (external edit)