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];

• Data type - the data type of the array items (integer, float, double, char, long ....)
• Array-name - the name of the array. Use the rule naming a variable to name an array.
• SIZE - the number of items the array can hold. The SIZE must be defined.
 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<

ACCESSING ARRAY ELEMENTS

• You can use loop to assign values to all elements in an array.
 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.
• To access an individual array element, use an integer value, an interger variable, or an integer expression placed in a square brackets to indicate which element in the array should be accessed.
 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]"<
• C++ does not perform range checking. As a C++ programmer, you are responsible for the index values used to access array elements are within the legal range. If you overrun the bounds of an array, the value stores at that memory location is read or overwritten. This can cause the program to abort or produce an unexpected result.
 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 [ ] );

• In the function declaration, you do not specify the size of the array; rather than, you use an empty square bracket to indicate an array without the size specified.

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

• In the function definition, you do need to specify the array name, but the array size is optional.

Function calling function-name(array-name)

• Passing array name in calling a function. When you pass an array to a function, you actually pass the memory address of the first byte of the array. Any changes made to the array in the function will be reflected to the original array from the calling function.
In the example below, the program will pass an array of integer to a function. The function will add a value of 5 to each item in the array. After returning from calling a function, the program displays the value of each item in the array passing to the function.
 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"<

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];