Back    

Array


Array is a collection of elements of the same type.

Array size is the number of elements it can contain, array size cannot be altered after declaration.

Each element inside the array has an unique index number which is used to access them.

Index number starts from 0

Array declaration

variable type variable_name [ array size ];
/*example array of double type with array size 10*/
double example[10];



Array declaration with element initialization

variable type variable_name [ array size ] = { element1, element2 ,... };
/*example array of int type with array size 5*/
int prime[5] = {2,3,5,7,11} ;


An illustrated sample of above array is shown below:

prime
Index01234
Value235711


   prime[ 0 ] = 2   


Practice

using namespace std ;

int fibonacci [10] = {1,1,2,3,5,8,13,21,34,55}

for( int counter = 0; counter <= 9; counter++)

{

   cout<<"fibonacci["<< counter << "] = " << fibonacci[counter] << endl;

}

Output


//When counter =
fibonacci[ 0 ] = 1

//Whole output
fibonacci[ 0 ] = 1
fibonacci[ 1 ] = 1
fibonacci[ 2 ] = 2
fibonacci[ 3 ] = 3
fibonacci[ 4 ] = 5
fibonacci[ 5 ] = 8
fibonacci[ 6 ] = 13
fibonacci[ 7 ] = 21
fibonacci[ 8 ] = 34
fibonacci[ 9 ] = 55