Static Data Structures

We have seen how simple data items must have a designated type such as integer or float. Data structures can hold many data items of the same or different types. Static data structures are defined inside a program and cannot change their size or dimension during execution.

Array

An array is like an egg box or any other container designed to hold items of the same type. Arrays have dimensions which define the direction of the data in the structure.

A single dimension array, sometimes called a vector, has just one direction and is like an egg box that is 1 egg wide and 6 eggs long.

A two dimension array has two directions, X and Y or rows and columns. An array of 2 columns and 3 rows (or 3 columns and 2 rows) is like a  conventional egg box space for 6 eggs (some egg boxes are 2x10, some are 2x12).

A three dimension array has three directions, X, Y and Z. We might use a three dimension array to hold information on a 3D version of noughts and crosses (tic-tac-toe) or to hold the positions of objects in a 3D game.

Sample declarations:

var simple_array:array[1..20] of integer;

var table:array[1..5, 1..10] of string;

var three_d_array:array[1..5, 1..5, 1..5] of single;

Arrays are often processed inside a for loop, for example:

for i := 1 to length(simple_array) do
 simple_array[i] := i * i;

 

Record

The record structure provides a way of grouping together one or more simple variables of different type. Records for the basis of databases and they are often used in custom file designs. For example:

type
Tperson=record
             name:string[20];
             address:string[20];
             dateofbirth:TDateTime;
end;

The fields within a record are accessed by a '.', for example person.name, person.address, person.dataeofbirth.

 

Set

A set is a collection of values with the same ordinal type but where the items have no inherent order. The balls inisde the lottery dispenser, for example, are a set: they are not structured or ordered like an array.

Sets use [ ] as delimiters and the keyword 'in' to determine whether a particular value is in a set. We might, for example, define a set of Fibonacci numbers or prime numbers:

type
TFibs= 1..250;
fib_set=set of TFibs;

var fibonacci_numbers:fib_set;

fibonacci_numbers:[1,2,3,5,8];
if 5 in fibonacci_numbersthen showmessage('That is a Fibonacci number');

Back