Arrays of Structures

  1. The base type of an array can be any built-in as well as user-defined type, including structures and classes.

  2. We consider an example where an array of struct may be used. Suppose you want to keep track of the wind condition at 10 different cities. The data of interest can be packaged in a structure called WindInfo:

    struct WindInfo
    {
    double velocity;     // in miles per hour
    char direction;         // 'N', 'S', 'E', or 'W'
    };

    An array of struct having 10 elements of the above type is declared by:

    WindInfo data_point[10];

    A loop can be used to fill the array:

    for (int n = 0; n < 10; n++)
    {
        cout << "Enter velocity for " << n << " numbered data point: ";
        cin >> data_point[n].velocity;
        cout << "Enter direction for that data point" << " (N, S, E. or W): ";
        cin >> data_point[n].direction;
    }