< previous page page_595 next page >

Page 595
for (number = 0; number < 1000; number++)
    cin >> value[number];
for (number = 999; number >= 0; number--)
    cout << value [number] << endl;
This code fragment is correct in C++ if we declare value to be a onedimensional array-that is, a collection of variables, all of the same type, where the first part of each variable name is the same, and the last part is an index value enclosed in square brackets. In our example, the value stored in number is called the index.
The declaration of a one-dimensional array is similar to the declaration of a simple variable (a variable of a simple data type), with one exception: you must also declare the size of the array. To do so, you indicate within brackets the number of components in the array:
int value[1000];
This declaration creates an array with 1000 components, all of type int. The first component has index value 0, the second component has index value 1, and the last component has index value 999.
Here is the complete ReverseList program, using array notation. This is certainly much shorter than our first version of the program.
//****************************
// ReverseList program
//****************************
#include <iostream.h>

int main()
{
    int value[1000];
    int number;

    for (number = 0; number < 1000; number++)
         cin >> value[number];
    for (number = 999; number >= 0; number--)
         cout << value[number] << endl;
    return 0;
}
Now that we have demonstrated how useful one-dimensional arrays can be, we define them formally and explain how individual components are accessed.

 
< previous page page_595 next page >