Arrays
An array is an indexed collection of elements. The array will have an identifier and elements will be indexed by integers:
Stock 0. PollenFilter 1. OilFilter 2. AirFilter 3. FuelFilter 4. HydraulicFilter 5. WaterFilter
The element at Stock[4] is a string variable with the value of "HydraulicFilter". The index range for C++ invariably forces the use of 0 as the start point of an array.
| Type | Identifier | Description |
| Array of string | Stock[6] | An array holding stock names |
In C++ the declaration is:
AnsiString Stock[6];
The base type is declared followed by the identifier and the array size.
Arrays must have the following properties:
- A fixed number of elements
- Elements of the same base type
There are no operations allowed directly on whole arrays:
- No assignment MyArray = YourArray is illegal
- No assignment MyArray = 0 is illegal
- No comparison MyArray == YourArray is illegal
Comparing arrays for equality can only be done through the individual comparison of each pair of corresponding elements.
Tables
One method of collating records into larger structures is via an 'array of records'. All the constituent elements of the array must be of the same type. This same type includes structured (programmer defined). An array of records is a table.
| Index | Item | Unit | Cost |
| 0 | Oil | Litre | 1.58 |
| 1 | OilFilter | Each | 3.35 |
| 2 | AirFilter | Each | 5.32 |
| 3 | FuelFilter | Each | 7.07 |
| 4 | HydraulicFilter | Each | 12.05 |
There are three fields, each with identifiers with index numbers in the left hand column. In C++ it is declared as follows:
struct StockType
{
AnsiString Item;
AnsiString Unit;
float Cost;
};
StockType Stock[5];
Two-dimensional arrays
| Type | Identifier | Description |
| Array of character | Page[40][80] | A two-dimensional array of characters |
Here an array of arrays is created showing the equivalent of 40 lines of text on a page with 80 characters per line. In C++:
char Page[40][80];
Accessing is by:
Page[Row][Column];
Once declared the order of the indexes is fixed. The form for declaring a two-dimensional array is therefore:
BaseType ArrayName [FirstIndexSize][SecondIndexSize];
Identified by:
ArrayName[FirstIndex][SecondIndex];
Comments, suggestions, ideas to
Stuart Banner
