# Tutorial - Fixed Size Arrays

Syntax of an Array Definition

The general form of a fixed size array definition is :-

```	index_range	:= low To high
fixed_array_def	:= Type identifier = basetype [ index_range [[ , index_range ]] ] ;

```

Here are some examples :-

```	[= an array of 10 Numbers =]
Type NumArray = Number[1 To 10];

[= an array of 10 Numbers - zero based index =]
Type NumArrayZB = Number[0 To 9];

[= two dimensional array of text =]
Type Text2DArray = Text[1 To 10, 1 To 2];
```

The low and high bounds are constant expressions that must be evaluable at compile time. One other thing to note is that arrays, like all types, must be defined in the global section ( i.e. not within a routine ) of a source file.

```	[= an array of 10 Numbers =]
Const Number Low = 1;
Const Number Len = 10;
Type NumArray2 = Number[Low To Low + Len - 1];
```

Declaring an Array

Once you have defined an array type you will want to have an actual instance of such an array. This is declared like any other variable. These may be global to the file or local to a routine. As with other types a variable it can be initialised when declared. If a fixed array is not explicitly initialised all the elements will be set to null.

Try the following :-

```Type NumArray = Number[1 To 4];

Program()
NumArray uninit;
NumArray inited = [ 10, 20, 30, 40 ];
Begin
Output uninit;
Output inited;
End;
```

You will notice that a complete array can be specified as a list of numbers between square brackets. Each element can be a complex expression provided it yields a result of the correct type. This applies to any place where you could have an array - so in the previous example I could have done :-

```	NumArray inited = [ 1 * 10, 2 * 10, 3 * 10, 4 * 10 ];
```

For higher dimensional arrays the successive dimensions are built like arrays of arrays :-

```	Type NumArray2D = Number[1 To 4, 1 To 2];
[ = . . . =]
NumArray2D inited2d = [ [1, 1], [2, 4], [3, 9], [4, 16] ];
```

Accessing Array Elements

The individual elements of an array can be accessed with a syntax common to a lot of programming languages :-

```	[= get the third item =]
x := inited[3];
```

The same syntax can be used to set the element :-

```	[= set the third item =]
inited[3] := 45;
```

For higher dimensional arrays you need to specify as many indexes as there are dimensions :-

```	[= set the appropriate item =]
inited2d[3, 2] := 45;
```

As with pretty much everything else in the system, a value can be replaced with an expression :-

```	[= set the third item =]
inited[1+1+1] := 45;
```

If the expression yields a result that is outside the permitted range of indexes then a run-time error will be reported.

Example

Below is an example of using a For loop with an array :-

```Type MyArrayType = Number[1 To 4];

Program()
[= declare a local array =]
MyArrayType mydata = [ 10, 120, 76, 40 ];
Number entry;
Begin
For entry From 1 To 4 Do
DrawBar(mydata[entry]) => {BarWidth * entry, BaseLine};
EndFor;
End;
```
array_1.grs array_1.png