Declaration and Referencing Arrays

An array is a built-in data type for a collection of data all of which having the same type. For example an array can represent a list of test scores (of type int) or a list of temperatures (of type float).

  1. The syntax for declaring an array is:

    Data_Type Array_Name [A_Fixed_Number];

    This declares an array named Array_name capable of storing up to A_Fixed_Number of data values all of type Data_Type.

  2. The type of data is referred to as the base type of the array. Any built-in as well as user defined data types are acceptable.

  3. A_Fixed_Number specifies the maximum size of the array. It must be either an integer literal or an integer constant. It cannot be a variable.

  4. Sometimes the maximum number of elements of an array is not known until the program is actually run. For example a certain number of exam scores are to be entered from the keyboard and stored in an array. In that case we need to declare an array with a sufficiently large size to accommodate practically all reasonable number of data points. For the exam score of an undergraduate class, we may use a size of 700. When storing data in an array always make sure that the array is not already full before adding more data.

  5. The following are valid ways to declare arrays:

    const int size = 128;
    int test_scores[size];
    float city_temperature[5];
    char letter_grades[700];

  6. The individual variables that together make up the array are referred to as indexed variables, or subscripted variables, or elements of the array.

  7. Individual elements of an array are referred to by the array name and an integer index enclosed in a pair of square brackets [ ]. The index starts from zero and ends with one less than the maximum size of the array.

  8. Suppose we want to use the above declared array city_temperature to store today's lowest temperatures for 5 cities: Chicago, New York City, Miami, Los Angeles, and Anchorage. We can store the data as follows:

    city_temperature[0] = -4;       // The first element of the array
    city_temperature[1] = 8;        // The second element of the array
    city_temperature[2] = 26;      // The third element of the array
    city_temperature[3] = 19;      // The fourth element of the array
    city_temperature[4] = -15;     // The fifth and the last element of the array

  9. The integer that appears inside the square bracket when an array is declared represents the array's maximum size. After the array has been declared, the integer inside the square bracket represents an index (a subscript) and so the array name followed by an index placed inside a pair of square brackets actually refers to a particular element of the array.

  10. Elements of an array can be used any place that an ordinary variable of that type can be used.

    For example:

    float city_temperature[5];
    float average_temperature;
    float sum = 0;
    cout << "Today's lowest temperature in New York City is: "
             << city_temperature[1] << endl;
    if (city_temperature[3] > city_temperatue[2])
         cout << "It is warmer tonight in Los Angeles than in Miami.\n";
    for (int i = 0; i < 5; i++)
         sum += city_temperatue[i];
    average_temperature = float(sum) / 5.0;

  11. //Another example in declaring and using arrays.
    //Read in 500 scores from the keyboard and show how much each
    //score differs from the highest score.
    #include <iostream>
    using namespace std;

    int main( )
    {
        int i, score[500], max;

        cout << "Enter 500 scores:\n";
        cin >> score[0];
        max = score[0];
        for (i = 1; i < 500; i++)
        {
                cin >> score[i];
                if (score[i] > max)
                    max = score[i];
                //max is the largest of the values score[0],..., score[i].
        }

        cout << "The highest score is " << max << endl
                << "The scores and their\n"
                << "differences from the highest are:\n";
        for (i = 0; i < 500; i++)
                cout << score[i] << " off by " << (max - score[i]) << endl;

        return 0;
    }