Introduction. Array: An ordered collection of values with two distinguishing characters : Ordered and fixed length Homogeneous. Every value in the array must be of the same type.
Array. The individual values in an array are called elements . The number of elements is called the length of the array Each element is identified by its position number in the array, which is called index . In C/C++/Java, the index numbers begin with 0..
Array declaration. An array is characterized by Element type Length type Array_Name [ Length ] Ex: int student [30 ] An array named student of length 30.
Arrays. n element array c : c[ 0 ] , c[ 1 ] … c[ n - 1 ] Array elements are like normal variables c[ 0 ] = 3; cout << c[ 0 ]; Performing operations in subscript. If x = 3 , c[ 5 – 2 ] == c[ 3 ] == c[ x ].
Arrays. c[6]. -45. 6. 0. 72. 1543. -89. 0. 62. -3.
Selecting elements. Identifying an element array[ index ] Index can be an expression Cycling through array elements for ( int i = 0; i < array.length ; i ++).
Any component of the array can be inspected or updated by using its index. This is an efficient operation O(1) = constant time.
Examples Using Arrays. Initializers int n[ 5 ] =; If not enough initializers , rightmost elements become 0 If too many initializers , a syntax error is generated int n[ 5 ] = Sets all the elements to 0 If size omitted, the initializers determine it int n[] =; 5 initializers , therefore n is a 5 element array.
Basic Operations. Following are the basic operations supported by an array . Traverse − print all the array elements one by one. Insertion − Adds an element at the given index. Deletion − Deletes an element at the given index. Search − Searches an element using the given index or by the value. Update − Updates an element at the given index..
Algorithm For Insertion In Array. value insertion array.
Deletion in Array. If an element to be deleted ith location then all elements from the (i+1) th location we have to be shifted one step towards left . So (i+1) th element is copied to ith location and (i+2) th to (i+1) th location and so on.