2. Introduction:
Array is a data structure consisting of a
collection of elements of related data types.
Defined in same manner as ordinary variable
expect size specification.
Syntax :
data_type array_name[ no. of elements];
Is also known as subscripted value.
No limits on dimension of arrays.
3. Types of array:
1)One Dimensional Array:
Only one subscript also called one subscript.
1D array variable are declared with data type
followed by variable name and a pair of square
brackets.
Syntax:
data_type array_name[ ];
Array size most be integer constant greater than
zero with valid data type.
4. Initialization of 1D array:
Should be initialized as other variable .
Examples:
int array [6]={1,3,5,7,9,8};
int array [5]={3,5};
int array [ ]={2,4,6,7};
Note: We can’t initialized an array using a variable
For example:
int x=5;
int array [x];
Above example is illegal.
5. 2) Two dimensional array:
Simplest form of multi dimensional array .
Requires two pair of square brackets.
Syntax:
To declare 2D array of size x , y.
data_type array_name[x][y];
First bracket is know as number of rows and
second numbers of columns.
6. Initialization of 2D array:
2D array is initialized by specifying bracket value
of each value .
Following is an array with 3 rows and 4 columns:
int a[3][4]={ {0,1,2,3},
{4,5,6,7},
{8,9,4,7} };
Also equivalent to
int a[3][4]={0,1,2,3,4,5,6,7,8,9,4,7};
7. 3) Multi-dimentional array:
Has more than one subscript.
General form of multidimensional array:
data_type array_name [size 1][size 2]…[size n];
For example:
int array [2][3][2]={ { {2,4},
{3,5},
{5,7}
},
{ {3,5},
{7,8},
{1,2}
} };
8. Accessing 2-D array elements:
An element in 2-D array is accessed by using subscripts, i.e. rows
and column index of the array.
For example:
9. Arrays as argument to function:
To pass 1D array as argument to function, we should declared
function formal parameter in three ways.
Way-1:
Formal parameter as pointer
void function (int *aru)
{…}
Way-2:
Formal parameter as sized array
void function(int aru[10])
{…}
Way-3:
Formal parameter as unsized array
void function(int aru [ ])
{…}
12. Array application :
Array is used for maintaining multiple variable
names using single name.
Array can be used for sorting element using
different sorting techniques.
Array can perform matrix operation.
Array can be used in recursive function:
-When the function calls another function
or the same function, the current values
can be stored in array and can be
retrieved back.
13. Limitation of array:
1.Array is static data structure.
2.Can hold data only belonging to same data type
3.Deletion is not easy because the element are
stored in contiguous memory location.
4.Bound checking :
process of checking limit of array.
5.Shortage of memory may occur if we don’t know
required memory in advanced.
6.Wastage of memory may occur if array of large
size is defined and less element is processed.
14. Points to remember:
An array is similar to an ordinary variable except it
can stored multiple element of same type.
And it is also know as subscripted variable.
Elements of an array are accessed by specifying the
index of desired element within square brackets .
Array subscripts must be a integer type.(int, long
int, char ,etc.)
However, big an array it elements are always stored
in contiguous memory location
Array indices starts at zero and goes to one less
than the size of array.