Arrays in C++ programming



Introduction to Arrays in C++

An array allows you to store and work with multiple values of the same data type. An array works like a variable that can store a group of values, all of the same type. The values are stored together in consecutive memory locations.

Here is a definition of an array of integers:


    int days[6];

The name of this array is days. The number inside the brackets is the array’s size declarator. It indicates the number of elements, or values, the array can hold. The days array can store six elements, each one an integer.

Arrays in C++
Arrays in C programming

Arrays of any data type can be defined. The following are all valid array definitions:


    float temperature[100];     // Array of 100 floats
    char letter[26];     // Array of 26 characters
    long unit[50];       // Array of 50 long integers
    double size[1200];   // Array of 1200 doubles
    string name[10];     // Array of 10 string objects
   

Accessing array elements

Even though an entire array has only one name, the elements may be accessed and used as individual variables. This is possible because each element is assigned a number known as a subscript.

A subscript is used as an index to pinpoint a specific element within an array. The first element is assigned the subscript 0, the second element is assigned 1, and so forth.

The following statement stores the integer 30 in hours[3]. Note that this is the fourth array element.


    hours[3] = 30;

Array elements may receive values with assignment statements just like other variables. However, entire arrays may not receive values for all their elements at once. Assume the following two arrays have been defined.


    int doctorA[5]; // Holds the number of patients seen by Dr. A
    // on each of 5 days.
    int doctorB[5]; // Holds the number of patients seen by Dr. B
    // on each of 5 days.

Displaying array content

Array elements may also have information read into them using the cin object and have their values displayed with the cout object, just like regular variables, as long as it is done one element at a time.

The program below shows the hours array, being used to store and display values entered by the user.

This program stores employee work hours in an int array.


    #include <iostream>
    int main()
    {
    const int NUM_EMPLOYEES = 6;
    int hours[NUM_EMPLOYEES]; // Holds hours worked for 6 employees
    // Input hours worked by each employee
    cout << "Enter the hours worked by " << NUM_EMPLOYEES
    << " employees: ";
    cin >> hours[0];
    cin >> hours[1];
    cin >> hours[2];
    cin >> hours[3];
    cin >> hours[4];
    cin >> hours[5];
    // Display the contents of the array
     cout << "The hours you entered are:";
     cout << " " << hours[0];
     cout << " " << hours[1];
     cout << " " << hours[2];
     cout << " " << hours[3];
     cout << " " << hours[4];
     cout << " " << hours[5] << endl;
    return 0;
    }

    Program Output with Example Input Shown in Bold:
    Enter the hours worked by 6 employees: 20 12 40 30 30 15
    The hours you entered are: 20 12 40 30 30 15

Even though most C++ compilers require the size declarator of an array definition to be a constant or a literal, subscript numbers can be stored in variables.

This makes it possible to use a loop to “cycle through” an entire array, performing the same operation on each element.


PROMOTIONS