MT 262 - Block 2 - Unit 1 - Arrays
fellstrider.com - the logo!
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
 
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
Data table entry for an array

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:

There are no operations allowed directly on whole arrays:

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.

Stock
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
Data table entry for a two-dimensional array

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];
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
Move on to For Loops

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner