Wednesday, August 26, 2009

Array

A variable of the type int or double can contain one value at a time. It would often be useful of the able to handle an aggregate of elements of the same type. An array is a structured datatype that:
  • has a specified size
  • consists of data elements of the same type
  • the elements are identified by their position in the array
In C++ array variables are declared:

type array_name[ capacity ];

The result is an array type variable named array_name in which data elements of the type type can be stored. The number of elements the array can contain is determined by capacity. type can be any defined type (predefined or programmer-defined). capacity needs to be a constant or a literal. For example, an integer array with nine elements:

int numbers[ 9 ];

An array can be seen as a row of numbered boxes, whose contents can be checked and changed. Every box fits one data element of the type type. In C++ arrays are indexed from zero through size-1.

int numbers[ 9 ] looks like this:

numbers:









 0  1  2  3  4  5  6  7  8 


the index of the last element is one smaller than the size of the array.

An element at index i can be accessed using [ ]:

numbers[8] = 2*numbers[0];

would give the last element the value of the first element times two. Any expression of the type integer can be used as an operand for the brackets.

For example,

const int SIZE = 10;

int maxValue( )
{
    int array[ SIZE ];
    int i = 0;
    int max = 0;
    while( i <>
    {
        cin >> array[ i ];
        if( array[ i ] > max )
        {
            max = array[ i ];
        }
        ++i;
    }
    return max;
}

No checking is done ot ensure that the indices stay within the range determined by the declaration of the array:

int index = 1000;
numbers[ index ] = 10; // logical error!

A compiler compiles the code above just fine. It is still incorrect because it refers to a non-existent element.

Like all variables, an array needs to be initialized with some sensible values before it can be used. An array can be initialized at declaration:


int numbers[ 9 ] = {1, 2, 3, 4, 5, 6, 7};

results in an array:

numbers:
 1  2  3  4  5  6  7  0  0 
 0 1 2 3 4 5 6 7 8


If there are less elements at initialization than the size of the array can contain, the rest of the elements are initialized to zero.

It is not necessary to tell the size of the array when initialized. It is automatically determined from the number of elements.

int numbers[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

This is true only at initialization and cannot be used as a general method of handling array data. An array cannot be assigned another with =. Moreover, arrays cannot be compared with != and == operators. In array everything must be done one element at a time.

Arrays are static data structures. Their capacity cannot change during program execution. The capacity must always be fixed when the program is compiled:

int capacity = 0;
cin >> capacity;
int array[ capacity ]; // Error

results in a compilation error.


Reference: cplusplus, codesource, fredosaurus


Previous post

Next post




No comments:

Post a Comment