< previous page page_594 next page >

Page 594
One-Dimensional Arrays
If we wanted to read a list of 1000 values and print them in reverse order, we could write a program of this form:
//****************************
// ReverseList program
//****************************
#include <iostream.h>

int main()
{
    int value0;
    int value1;
    int value2;
      .
      .
      .
    int value999;

    cin >> value0;
    cin >> value1;
    cin >> value2;
      .
      .
      .
    cin >> value999;

    cout << value999 << endl;
    cout << value998 << endl;
    cout << value997 << endl;
      .
      .
      .
    cout << value0 << endl;
    return 0;
}
This program is over 3000 lines long, and we have to use 1000 separate variables. Note that all the variables have the same name except for an appended number that distinguishes them. Wouldn't it be convenient if we could put the number into a counter variable and use For loops to go from 0 through 999, and then from 999 back down to 0? For example, if the counter variable were number, we could replace the 2000 original input/output statements with the following four lines of code (we enclose number in brackets to set it apart from value):

 
< previous page page_594 next page >