|
|
|
|
|
|
|
A one-dimensional array is a structured collection of components (often called elements) that can be accessed individually by specifying the position of a component with a single index value. (In Chapter 13, we introduce multi-dimensional arrays, arrays that have more than one index value.) |
|
|
|
 |
|
 |
|
|
One-Dimensional Array A structured collection of components, all of the same type, that is given a single name. Each component (array element) is accessed by an index that indicates the component's position within the collection. |
|
|
|
|
|
|
|
|
Here is a syntax template describing the simplest form of a one-dimensional array declaration: |
|
|
|
|
|
|
|
|
In the syntax template, DataType describes what is stored in each component of the array. Array components may be of almost any type, but for now we limit our discussion to atomic components. ConstIntExpression is an integer expression composed only of literal or named constants. This expression, which specifies the number of components in the array, must have a value greater than zero. If the value is n, the range of index values is 0 through n-1, not 1 through n. For example, the declarations |
|
|
|
|
|
|
|
|
float angle[4];
int testScore[10]; |
|
|
|
|
|
|
|
|
create the arrays shown in Figure 11-3. The angle array has four components, each capable of holding one float value. The testScore array has a total of 10 components, all of type int. |
|
|
|
|
|
|
|
|
Accessing Individual Components |
|
|
|
|
|
|
|
|
To access an individual array component, we write the array name, followed by an expression enclosed in square brackets. The expression specifies |
|
|
|
|
|