ARRAYS
- An array is basically a variable, a list of elements that can hold multiple values of the same data type.
- It is declared as: datatype anAray[size];
e.g. intanArray[10]; <- this array can memorize 10 integers
- Elements are accessed by index which starts from 0 and go to n-1 (where n is the size). Therefore if we want to access the third element we write:
int element = anArray[2];
- Arrays can be initialised in the declaration as:
int anArray[5] = {34, 10, 11, 2, 25};
- But also, the user can be asked to input the values for an array using a for loop in which he is asked to introduce each value until the array is full. This can be done like that:
int anArray[5];
for (i=0; i<5; i++)
{
cout<< “Enter the value “ ;
cin>>anArray[i]; //this memorize the value on the position i in the array
cout<<endl;
};