RSS | Roulette Scripter Studio | Lesson 9 | Array structures
     
This tutorial was made by Silver
http://www.money-maker-machine.com/forum/profile/Silver/
 
Lesson 9. Array structures.
 
0. Arrays
An Array is a powerful data structure that stores variable data. It is just like a small fixed number of boxes linked together one after the other storing things that are related to each other. An array is said to be a static data structure because, once declared, its original size that is specified by the programmer will remain the same throughout the whole program and cannot be changed.

Up until now, we have used single variables only as a tool to store data. Now we will be using the array data structure.

I. Declaration

The array is declared like a simple variable, in VAR section.
The size of the array and the data types that it will use for storing data - will be defined at first assing of values.
Ex.9.1: The following example stores in the array 10 numbers. This size depends on your script requirements.
var myArray1;
begin
  myArray1:=[1,2,3,4,5,6,7,8,9,10];
end;


II. Assign and read data.

Arrays are used just like ordinary variables. They are used to store typed data just like the ordinary variables.

To assign values to a particular integer of an array, we do it like this:
myArray1[5]:=22;
<arrayName>[index] := <value>;

To read the value from array and to assign it to a variable (or to other array), use:
myVar:=myArray1[3];
<variableName> := <arrayName>[index];

Arrays in script are 0-based index, so the first element in array have index 0.
This mean that in array from Ex.9.1 we have next:
myArray1[0]=1;
myArray1[1]=2;
...
myArray1[8]=9;
myArray1[9]=10;
myArray1[10]=nothig - because the maximum index is 9.

III. Multi-dimensional arrays .
Multi-dimensional are arrays which store variables in a second or nth dimension having n*m storage locations. Mutli dimensional arrays including the 2 dimensional array, are defined by array in array.

Ex.9.2: The following example stores 3 sets by 12 numbers.
This array dimension is 3x12 or 3 values by array of 12 (remember that first element in array have index 0).
var myArray2;
begin
  myArray2:=[
                [1,2,3,4,5,6,7,8,9,10,11,12],
                [13,14,15,16,17,18,19,20,21,22,23,24],
                [25,26,27,28,29,30,31,32,33,34,35,36]
                ];
end;


How to access it? Next I will show on what position are numbers situated :
  number 1 is on position [0,0] - so print(myArray2[0,0]) - will return 1
  number 6 is on position [0,5] - so print(myArray2[0,5]) - will return 6
  number 12 is on position [0,11] - so print(myArray2[0,11]) - will return 12
  number 13 is on position [1,0]
  number 14 is on position [1,1]
  number 24 is on position [1,11]
  number 25 is on position [2,0]
  number 35 is on position [2,10]
  number 36 is on position [2,11]

The limit of this array are indexes 2 and 11.
trying to read/assign myArray2[ 3 ,5], myArray2[2, 12 ] (x any value) - RSS will generate error.
Because we have no index 3 and 12.

The next code will replace the value of position [2,6] (was number 31) to 777;
myArray2[2,6]:=777;
And the array will be
 myArray2:=[
                [1,2,3,4,5,6,7,8,9,10,11,12],
                [13,14,15,16,17,18,19,20,21,22,23,24],
                [25,26,27,28,29,30,777,32,33,34,35,36]
                ];


Yes, it is pretty much the same. Well, array helps you make spreadsheets, just like Lotus 1-2-3 or Excel.

How about the three-dimensional table or more ? It also the same. Look at the definition:
Ex.9.3: The following example stores 3 sets of 4 sets by 3 numbers.
This array dimension is 3x12 or 3 values by array of 12 (remember that first element in array have index 0).
myArray3:=[
                [[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
                [[13,14,15],[16,17,18],[19,20,21],[22,23,24]],
                [[25,26,27],[28,29,30],[31,32,33],[34,35,36,37]]
                ];


  number 1 is on position [0,0,0] - so print(myArray3[0,0,0]) - will return 1
  number 6 is on position [0,1,2] - so print(myArray3[0,1,2]) - will return 6
  number 12 is on position [0,3,2] - so print(myArray3[0,3,2]) - will return 12
  number 20 is on position [1,2,1]
  number 34 is on position [2,3,0]

As you may see I have added the 37 after the 36.
myArray3[2,3,2] will return 36,
myArray3[2,3,3] will return 37,
myArray3[2,3,4] will generate the error,
myArray3[1,3,2] will return 24,
myArray3[1,3,3] will generate the error.

The next code will replace the value of position [1,2,2] (was number 21) to 777;
myArray3[1,2,2] :=777;

------------------------------------
We may assign values of an array to other array:
myArray3[1,1] - it is the next array - [16,17,18].
the next code myArray3[1,1]:=myArray3[1,2]; will copy the position [1,2] to position [1,1] and the myArray3 will be next:
myArray3:=[
                [[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
                [[13,14,15],[19,20,21],[19,20,21],[22,23,24]],
                [[25,26,27],[28,29,30],[31,32,33],[34,35,36,37]]
                ];


IV. function VarArrayHighBound().
Function VarArrayHighBound() will return the index of last element in the array.

print(IntToStr(VarArrayHighBound(my_array,1))); Result will be 11.
print(IntToStr(VarArrayHighBound(my_array2,1))); Result will be 2.
print(IntToStr(VarArrayHighBound(my_array3,1))); Result will be 2.

Because the myArray2 and myArray3 are multidimensional arrays, this mean that inside them we also have arrays. We may use the function VarArrayHighBound() for accesing the limit of an array that is inside other array.
myArray3[1,1] - it is the next array - [16,17,18].
print(IntToStr(VarArrayHighBound(my_array3[1,1],1))); Result will be 2.

myArray3[2,3] - it is the next array - [34,35,36,37].
print(IntToStr(VarArrayHighBound(my_array3[2,3],1))); Result will be 3.

V. Example with array.
var my_array, k;
begin    
  my_array:=[0,1,1,2,3,4,6,9,14,21,31,47,70,105,158];
  print('Example with array');
  print('');
  print('Without VarArrayHighBound()');
  for k:=1 to 14 do print('Step '+IntToStr(k)+' - Bet'+IntToStr(my_array[k])+' Chips.');
  print('');
  print('With VarArrayHighBound()');
  print(' Array contain '+IntToStr(1+VarArrayHighBound(my_array,1))+' elements;');
  for k:=0 to VarArrayHighBound(my_array,1) do print(' Element '+IntToStr(k)+' - have value '+IntToStr(my_array[k])+'.');
end;
 
Copyright © 2007-2011, All rights reserved to Money Maker Machine