Return to Topic Menu | Computer Science Main Page | MathBits.com  | Terms of Use | Resource CD

 

Declaring Arrays

Remember to include the header file <apvector.h>

 Declaring apvector variables:

 apvector <type> name(expression);

type of each element

name of array

subscript (integer number or constant expression representing the number of elements.)

The subscript is the number of elements in the array, surrounded by parentheses.  For example,

apvector <int> num(10);

declares an array named num that contains 10 integers.  When the compiler encounters this declaration, it immediately sets aside enough memory to hold all 10 elements.

A program can access each of the array elements (the individual cells) by referring to the name of the array followed by the subscript which denotes the cell.  For example, the third cell is denoted num[2].

The subscripts of array elements begin with zero.

The apvector class has a subscript checking code that displays an error message when the program uses a subscript that is out of the range of the array - a really nice feature!!

 

Warning!
Array subscripts start with the
 number 0
 (not 1).

 

<apvector.h>

 

Be sure to notice when to use parentheses ( ) and when to use brackets [ ].