Month: February 2015

Arrays in C++

An array is a collection of a set number of components all of the same data type. A one-dimensional array is an array where the components are arranged in list form.

The general form for declaring an array is to name the array’s data type, followed by the array name itself, with square brackets immediately placed after the name, no spaces, and inside these square brackets an integer that defines how many individual components the array should contain.

The integer that specifies the number of elements in an array is called an index. The index can be any non-negative number. The index is used to specify the size of the array when it is defined, and then it is used to specify which component in the array we want to access.

The square brackets we play the index number into are called the array subscripting operator.

#include <iostream>

int main(void){

    //declare an array of five int elements
    int list[5];
    //declare an array of ten char elements
    char listTwo[10];
    //declare an array of seven double elements
    double listThree[7];

    return 0;

}

Assigning values to an array is done the same way as assigning a value to regular variable. We just need to be sure to use the index to specify what element in the array is having the value assigned to it. A word of caution, however, arrays are zero indexed. What this means is that the first element of the array is at index 0, the second element is at index 1, etc. This is easy to forget, leading to off by one errors.

#include <iostream>

using namespace std;

int main(void){
    
    int list[3];
    
    //first element
    list[0] = 30;
    //second element
    list[1] = 60;
    //third element
    list[2] = 90;
    
    cout << list[0] << endl;
    cout << list[1] << endl;
    cout << list[2] << endl;
    
    //this code would not work
    //as [3] specifies the fourth element
    //which does not exist
    //cout << list[3] << endl
    
    return 0;
    
}

Loops are perfect for working with arrays. We can use loops to initialize an array, print an array, input values into an array, etc. In our next program, let’s set up an integer array, populate the array with five different values, and then output the array in opposite order.

#include <iostream>

using namespace std;

int main(void){
    
    int i = 0;
    int iArray[5];
    
    for(i; i < 5; i++){
        cout << "Please enter a value for element " << i + 1 << ": ";
        cin >> iArray[i];
    }
    
    for(i; i >= 0; i--){
        cout << iArray[i] << " ";
    }
    
    cout << endl;
    
    return 0;
    
}

Again, it must be noted that it is very easy to attempt to access an array element that does not exist. In C++ there is no guard against out-of-bound indices. The compiler does not check whether the index value. It is our responsibility to make sure that the indexed is within bounds.

Like a normal variable, an array can be initialized while it is being declared. When we wish to initialize an array, we place the values within a pair of curly braces, and separate each value from the other with a comma. If we initialize an array while declaring it, we do not have to specify the size of the array, we can simply place empty brackets and the compiler will infer the bounds of the array from the number of initialized values.

When we declare and initialize an array simultaneously, we do not need to initialize all of the elements in the array. We can simply put an index value greater than the number of values we are initializing, and the extra elements will all be initialized to zero.

using namespace std;

int main(void){
    
    int i = 0;
    //each of these arrays of type double
    //will hold five elements
    double dArray[] = {7.25, 8.02, 7.39, 9.18, .315};
    double dArrayTwo[5] = {0};
    double dArrayThree[5] = {11.38, 19.84};
    
    
    for(i; i < 5; i++){
        cout << dArray[i] << " ";
    }
    
    cout << endl;
    
    for(i = 0; i < 5; i++){
        cout << dArrayTwo[i] << " ";
    }
    
    cout << endl;
    
    for(i = 0; i < 5; i++){
        cout << dArrayThree[i] << " ";
    }
    
    cout << endl;
    
    return 0;
    
}

Arrays are passed as parameters to functions by reference only. The base address of the array is the memory location of the first array element. For example, if iArray is an array, then the memory address of iArray[0] is the base address of the array. It is the base address that is passed to the formal parameter when we pass an array as an argument to a function.

As arrays are passed by reference only, we do not use the symbol & when declaring an array as a formal parameter. When declaring a one-dimensional array as a formal parameter, the array’s size is usually omitted.

When a parameter is a reference parameter, whenever the parameter is altered within the called function, the change is made to the variable it is referring to. If we wish the function to keep from altering the values stored in an array, we can use the reserved word const in the declaration of the formal parameter.

#include <iostream>

using namespace std;

void printArray(const int iArray[], int bounds);
void fillArray(int iArray[], int bounds);

int main(void){
    
    const int bounds = 8;
    
    int iList[bounds] = {30, 45, 60, 90};
    
    printArray(iList, bounds);
    fillArray(iList, bounds);
    printArray(iList, bounds);
    
    
    return 0;
    
}

void fillArray(int iArray[], int bounds){
    for(int i = 0; i < bounds; i++){
        cout << "Enter value for element #" << i + 1 << ": ";
        cin >> iArray[i];
    }
}

void printArray(const int iArray[], int bounds){
    for(int i = 0; i < bounds; i++){
        cout << iArray[i] << " ";
    }
    cout << endl;
}

Note that when passing an array into a function, we often want to pass another integer parameter that specifies the bounds of the array.