Structured Data Type: Arrays

Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name.

An Array declaration is very similar to a variable declaration. For eg:

float annual_temp[10];

This will cause the compiler to allocate space for 10 consecutive float variables in memory. The number of elements in an array must be fixed at compile time. The elements of an array are ordered by the index. Array index numbering starts from zero.

annual_temp[10]

0 1 2 3 4 5 6 7 8 9

Base address is 0 Base address is n-1,

where n is the array size.

Need for Arrays

Arrays help to make a program simple, easy to code and understand. We can store five values of type int without having to declare five different variables. Instead we can use an array with a unique identifier, we can store five different values of the same type int. The index number should always be an integer. Each blank panel represents an element of the array.

Types of Arrays

Single-dimensional Arrays

An array whose elements are specified by a single subscript is known as a one-dimensional array. An array in C++ can be declared just as any other variable. This consists of type, array name and size. The element numbers in [ ] are called subscripts or idices. The array is given a name and its elements are reffered to by their subscripts or indices. The subscripts or index of an element designates its posisiton in the ordering of arrays. The array list would be written down simply as:

int list [5];

The size of the array must be a positive integer constant. The general form of an array declaration is :

type array-name[size];

where : type is the base type of thearray; array-name is the name with which array will be referenced; Size is the number of elements an array can hold; the data type of array elelments is known as the base type of the array;

Array Initialization

An Array of local scope(within a function), will not be initialized if we do not specify it otherwise while declaring it. If we declare a global array(outside any function), its content will be initiated with all its elements filled with zeros.

int list [5];

every element of list will be set initialy to 0:

But additionally, when we declare an Array, we have the possibility to assign initial values to each one of its elements using curly brackets { }. For example:

int list [5] = { 10, 20, 30, 40, 50 };

this declaration would have created an array like the following one:

The number of elements in the array that we initialized within curly brackets { } must match the length in elements that we declared for the array enclosed within square brackets [ ]. For example, in the example of the list array we have declared that it had 5 elements and in the list of initial values within curly brackets { } we have set 5 different values, one for each element. Because this can be considered useless repetition, C++ includes the possibility of leaving the brackets empty [ ] and the size of the Array will be defined by the number of values included between curly brackets { }:

int list [ ] = { 10, 20, 30, 40, 50 };

Unsized Array Initialization( Single Dimensional Array)

C++ allows one to skip declaring the size of an array in an array initialization statement.

For eg:

char str[]="Introductory Computer Science";

int num[]={1,2,3,4,5};

float price[]={50.5, 56.3,456.3.56.1};

HOME LEARN C++ PREVIOUS NEXT