Arrays

Top  Previous  Next

An array is a single variable which contains several elements of the same type. Each element has a value and an index number, and may be accessed using this number.

An example of a simple array would be:

 

Index:

0

1

2

3

4

Value:

15

32

4

100

-30

 

The code to produce such an array in Tibbo Basic would look like this:

 

 

dim x(5) as char

x(0) = 15

x(1) = 32

x(2) = 4

x(3) = 100

x(4) = -30

 

 

The first index in an array is 0. Thus, the array above contains 5 values, with indices from 0 to 4.

 

note_warning-wt

This is different from some BASIC implementations that understand x(5) as "array x with the maximum element number of 5" (that is, with 6 elements). In Tibbo Basic declaring x(5) means "array of 5 elements, with indices from 0 to 4".

Variable Types For Arrays

In the example above, the array was assigned the type char. This means that each element within this array will be stored in a variable of type char. Starting from Tibbo Basic V2.0 you can have arrays of variables of any type.

Accessing a Value Within an Array

To access a specific value within an array, include its index immediately after the name of the array, in parentheses. The index may also be expressed through a variable.

 

 

y = x(4) ' y would get a value of -30, according to the previous array

y = x(z) ' y would get the value of index z within array x.

 

 

As an example, you could iterate through an array with a loop (such as a For... Next Statement) and execute code on each element in the array, by using a variable to refer to the index of an element. Let's see how to calculate the sum of the first three elements in the array we previously defined:

 

 

dim x(5) as char

x(0) = 15

x(1) = 32

x(2) = 4

x(3) = 100

x(4) = -30

 

dim sum as integer

sum = 0

 

for i = 0 to 4 ' note that you do not necessarily have to iterate through all elements in the array.

       sum = sum + x(i)

next i

' now, at the end of this loop, sum contains the sum for the first three elements (51)        

 

 

The TIDE and Tibbo Basic V2.0 introduced correct handling of array indices. It is no longer possible for your program to point to an array element that does not exist. For example, if your array only has 5 elements and you try to access element number 5 the Virtual Machine will:

 

Generate an OOR (Out Of Range) exception and halt if your program is in the debug mode. If you attempt to continue the Virtual Machine will access x(4) -- the element with maximum available index.
When in the release mode, the OOR exception will not be generated but "index limiting" will still occur.

 

Example:

 

 

dim x(5) as char

dim f as byte

 

f=5

x(f)=3 'index limiting will happen here (preceded by the OOR exception if you are in the debug mode)

 

 

Compiler is smart enough to notice out-of-range situations even at compile time. For example, the following code will generate an error during compilation:

 

 

dim x(5) as char

 

x(5)=3 'compiler will determine that this operation will be eccessing an array element which does not exist!

 

Multi-Dimensional Arrays

The array in the example above is called a one-dimensional array. This is because every element in the array has just a single index number. However, we could also have an array which looks like this:

 

Index:

0

1

2

3

4

0

15

32

4

100

-30

1

78

15

-3

0

55

2

32

48

97

5

22

3

13

18

9

87

54

4

32

35

79

124

3

5

7

-9

48

8

99

 

This is called two-dimensional array. Each element in the array is now identified by an index consisting of two numbers (two coordinates). For example, the element 2, 0 contains the value 32. To create such an array, you would use the following code:

 

 

dim x(5,6) as char

x(0,0) = 15

x(0,1) = 32

x(0,2) = 4

......

x(5,2) = 48

x(5,3) = 8

x(5,4) = 99

       

 

Iterating through such an array would be done using a nested loop for each dimension in the array. The array above contains only two dimensions, so we would nest one loop within another. For an array containing six dimensions, we would have to use six such nested loops. See:

 

 

dim x(5,6) as char

x(0,0) = 15

x(0,1) = 32

x(0,2) = 4

......

x(5,2) = 48

x(5,3) = 8

x(5,4) = 99

 

 

dim i, j, sum as integer

sum = 0

for i = 0 to 5

       for j = 0 to 4

               sum = sum + x(i,j)

       next j

next i

' here, sum would be equal to the sum of the whole array. How much is that? Try and see.

       

 

In Tibbo Basic, you may define up to 8 dimensions in an array.

Alternative way of defining arrays

We have already explained that the following string means "an array x containing 10 elements of type byte":

 

 

dim x(10) as byte

 

 

In Tibbo Basic, the same can be expressed in a different way:

 

 

dim x as byte(10) 'you can think of it as '10 times of byte type' :-)

 

 

Both ways of defining arrays are completely identical and you can even mix them together, as we can see on the following examples of 2-dimensional arrays:

 

 

dim i(20,10) as byte 'two-dimensional array, 20x10 elements

dim i2(20) as byte(10) 'same! that is, 20 groups of byte x 10 -- exactly same meaning

dim i3 as byte(20,10) 'yet another way -- same result!

 

 

Now, Tibbo Basic strings can be defined with an optional parameter specifying maximum string length, for example:

 

 

dim s as byte(30) 'this string will have maximum length of 30 characters (bytes)

 

 

So, how do we declare an array of string variables? Here are some examples:

 

 

dim s(20,10) as string(30) 'two-dimensional array of 30-byte strings, 20x10 elements

dim s2(20) as string(30)(10) 'same!

dim s2 as string(30)(20,10) 'same!

 

Arrays introduce slight overhead

Each array occupies more space than the sum total of space needed by all elements of an array. This is because each array also includes housekeeping data that, for instance, defines how many elements are there in an array, array of what type that is, etc.