ARRAY

An array is a group of data items in which every data item has the same data type and is referenced using the same variable name. The data items in an array are stored consecutively in memory. To reference individual data items in an array, you use an index. Indexing starts at 0 and should ends with n - 1, where n is the number of items stored in an array.

DECLARING AN ARRAY

Syntax Data type Array-name[SIZE];

Example
  • Declare an array of integer named test that holds ten integer numbers.
    const int MAXITEMS = 10;
    int test[MAXITEMS];
  • Declare an array of double named salary that holds 30 employees' salaries.
    const int EMPLOYEES = 30;
    double salary[EMPLOYEES];

Note: Declare an array without specifying its array size will cause a syntax error.

Example: int test [ ]; //Illegal array declaration

INITIALIZING AN ARRAY

Syntax Data type Array-name[SIZE] = {value1, value2, value3, ........};

or Data type Array-name[ ] = {value1, value2, value3, .........};

Example Example
const int SIZE = 5;
int test[SIZE] ={ 10, 34, 78, 4, 8};
double salary[ ] = { 100.0, 2000.0, 35000.0, 40.0};
C++ creates 5 array elements:
test[0] contains 10
test[1] contains 34
test[2] contains 78
test[3] contains 4
test[4] contains 85
You can initialize an array without specifying the array's size. The size of the salary array is four because the array is initialized with four values.
salary[0] contains 100.0
salary[1] contains 2000.0
salary[2] contains 35000.0
salary[3] contains 40.0

INPUT/OUTPUT ARRAY

You use a loop to input values for all elements in an array or to display the values of the array's elements.

Example Output
const int SIZE = 4;
void main( )
{ int test[SIZE];
cout<<"Enter input:\n";
for ( int i=0 ; i < SIZE ; i++)
{cin << test[ i ];
}
cout<<"The values of the test array's elements\n";
for (i = 0; i < SIZE ; i++)
{ cout<<setw(4) <<test[i] ;}
Enter input:
4
89
23
56
The values of the test array's elements
4 89 23 56

ACCESSING ARRAY ELEMENTS

Example Comments
const int SIZE = 4;
void main( )
{ int test[SIZE];
for ( int i=0 ; i < SIZE ; i++)
{ test[i] = i +3;
}
}
In this example, you use a for loop to assign a value to each element of the array test.
test[0] contains 3.
test[1] contains 4.
test[2] contains 5.
test[3] contains 6.
Example
const int SIZE = 5;
void main( )
{int num[SIZE]={10,45,27,86,6};
int test[SIZE];
//Copy values from num array to test array
for ( int i=0; i < SIZE ; i++)
test[i] == num[i];
// Add 5 to the second element of the array num
num[1] = num[1] + 5 ; //num[1]= 45 + 5 = 50
// Compare num[2] and num[4]
if ( num[2] > num[4] )
cout << " num[2] is greater than num[4]"<<endl;
//Display test[2] and test[3]
cout<<test[2]<< ' '<<test[3]<<endl;


}

Example

int num[4]={1,5,3,8};
num[5] = 12; //Overrun an array element. The array num has only four elements. They are num[0], num[1], num[2], and num[3].

PASSING AN ARRAY TO A FUNCTION

Function declaration Return data type function-name( data type [ ] );

Function definition Return data type function-name( data type array-name[SIZE] )
{ Function body;
}

Function calling function-name(array-name)

Example
const int SIZE = 5;
void addFive(int [ ], int);
void main( )
{int num[SIZE] = { 10,20,30,40,50};
addFive(num, SIZE); // passing array name
for (int i =0; i < SIZE ; i++ )
cout << setw(4) << num[i];
}
void addFive( int x[ ], int items) // You can indicate SIZE value in the square brackets.
{ for (int j = 0; j < items ; j++)
x[ j ] = x[ j ] + 5;
}

Output

15 25 35 45 55

The next example, you pass an item of the array to a function.

Example
const int SIZE = 5;
void addFive(int);
void main( )
{int num[SIZE] = { 10,20,30,40,50};
addFive(num[2]); //pass a value of 30 to the function
addFive(num[4]); //pass a value of 50 to the function
cout<<"Display the items in the array num"<<endl;
for (int i =0; i < SIZE ; i++ )
cout << setw(4) << num[i];
}
void addFive( int value)
{
value = value + 5;
cout<< value <<endl;
}

Output

35
55
Display the items in the array num
10 20 30 40 50

COPYING ARRAY
To copy values from one array to another array, you must use a for loop.

Example
int source[5] = { 10,20,30,40,50};
int target[5];
target=source; // Illegal statement to copy array source to array target
//Use for loop to make an array copy
for (int i = 0; i < 5 ; i++)
target[i] = source[i];