Array

 Introduction :-

                         Till now we are familier with scalar or non-subscripted variable ie. the variable which can store only one value & there memory location are not-contiguous . In programming , many time we need to use more number of variable like 40 , 50 or more .for this we have two options .
® First , to use non-surscripted or Scalar variables & declare them as a1 , a2 , a4 , b4 , ....... z  & remember there name at every place . Which is difficult and time-waste .
® Second , we can declare only ONE Subscripted variable which is able to store 50 value in it ie. Array . Which is easy to use , easy to remember the name of variable and store the value in contiguous memory location .

Definition :-                       An Array is a collection of Homogeneous data-elements of a specific Data-type ( ie. integer , float or character ) with a single name and store the value in contiguous memory location .
eg.  int  name[50] ;
In this example
int       ----> data-type 
name ----> Name given to Array ( Rule same as variable name )
50      ----> Number of variables


Types of Array :-

                           Array can be divided into two types .

 


One Dimensional Array :- 
                                         It is One subscript or One Dimensional Array . It contain only one Subscript & the subscript value tell the number of value which array can store .
eg. int one_dim[ 5 ] ;
                                            it tell that it is an array of integer data type with name one_dim & number of value it can store is 5 .

Memory location map for One Dimensional Array :-

 In programming , we start from 0 & the last number is one less from total number . So position number start from 0 , 1 , 2 , 3 & 4 is last position .
The address of Memory Location are supposed the actual address can different . As we know integer data type  take Two bytes therefore there is a difference of two between two memory addresses .

® Declaring One Dimensional Array :-  
                                           The step involve in declaring One Dimensional Array are much similar  to simple variable . We must provide the following information 
Data type of array ie. int char etc.
Name of Array ( Same as scalar variable )
Only one subscript because it is one dimensional array
Index value ie. number of variable

                                           As we Know the number of subscript tell the Dimension of array . In one dimensional array there is only one subscript and the value of subscript ie. Index value tells the total number of value it can store .
eg.
int one_dim[ 5 ] ;
                             /* It is an array of integer data type which can take 5 values*/
 ® Initialising One Dimensional Array :- 
                                                                   As simple variable , One dimensional array can also be initialise at the Declaration place  ie.
int name[ 5 ] = { 8 , 9 , 5 , 4 ,1 }  ;
In above example we have declared an array of integer data-type which can store 5 value & at the same time we have initialise the array with
name[ 0 ] =   8
name[ 1 ] =   9
name[ 2 ] =   5
name[ 3 ] =   4
name[ 4 ] =   1
these value are initial value given to array which  can be change in the program .

eg.   int name[ 5 ] = { 5 , 1 , 4 , 8 , 9 , 8 , 11 , 10 } ;
In above example we have declare Index value = 5 , BUT we are initialising array with more from Index value  . This type of mistake cause an error signal at the time of compilation .
If we do this type of mistakes in the program ie.
int name[5 ] , i ;
for( i = 0 ; i <= 10 ; i++ )
{
   scanf(" %d ",&name[ i ] ) ;
}
this type of mistake not give error , it give us chance to enter 10 value but it only store first 5 values .


eg.        int name[ 9 ] =  { 8 , 9 , 5 , 4 ,1 }  ;
In above example we have declare Index value = 8 and we are initialising only five value . It not give any type of error But if we print or use this array in program it take first five value as given and take garbage value for left of the variables ie. name[ i ] = { 8 , 9 , 5 , 4 ,1 }    ( i= 0 , 1 , 2 , 3 , 4 )
and for   name[ i ] = Garbage value  ( i = 5 , 6 , 7 , 8 )


Multi Dimensional Array :-
                                         It is Multi dimensional array .It contain more from one subscript ie. it there can be two or tree or more subscript in declaration .
syntax :- 
                   int mul_dim[ sub 1 ][ sub 2 ][ ____ ] ...  ;
  in this 
int                  ----> Data type
mul_dim       ---->  Name given to Array
the number of subscript tell the dimension of array
&the multiplication of index value teel the total number of value it can store . 
eg.  int name[ 4][ 5 ] ;          /* Two Dimensional Array */
it can store 4*5 =20 values .                 

Memory location map for Two dimensional Array .

In above , the total number of variable are 4 * 5 = 20 so the are 20 locations .
 the address are written in cells . there is difference of two bytes between two addresses because integer variable take two bytes . so address are 2000 , 2002 , 2004 , 2008 , ......... , 2038 .

®Declaring Multi Dimensional Array :-  
                                           The step involve in declaring Array are much similar  to simple variable . We must provide the following information 
Data type of array ie. int char etc.
Name of Array ( Same as scalar variable )
Number of subscript
Index value ie. number of variable

                                    As name says it is an array with more from one subscript . The multiplication of Index value tell the total number of values it can store .
eg.
int two_dim[ 4 ][ 5 ] ;
                                          /* It is two dimensional Array of integer data type
                                                                    which can store 4 * 5 = 20 values */
int mul_3dim[ 2 ] [ 3 ] [ 2 ] ;
                                         /* It is a three dimensional array of integer data type
                                                               which can store 2 * 3 * 2 = 12 values */

®Initialising Multi Dimensional Array :-
                                                                    Initialising Multi Dimensional array is slightly different from One Dimensional array .

------->Two Dimensional Array :- 
                                               Two Dimensional Array can also be initialise at the declaration time .
int name[ 4 ][ 5 ] = {
                                       {    0      ,      1      ,      2      ,      3      ,      4  } ,
                                       {    5      ,      6      ,      7      ,      8      ,      9  } ,
                                       {  10    ,      11      ,   12       ,    13     ,    14  } ,
                                       {  15    ,      16      ,   17       ,    18     ,    19  }
                               }
 in above example , we have declare a two dimensional array with first subscript = 4 ( ie. 4 rows ) & second subscript = 5 ( ie. 5 columns ) .

------->Three Dimensional Array :-
                                             Three dimensional array can also be declare & initialise at the same time .
int name[ 2 ][ 3 ][ 2 ] = {
                                            {     
                                                   { 0 , 1 } ,
                                                   { 2 , 3 } ,
                                                   { 4 , 5 } ,
                                             } , 
                                             {
                                                    { 6 , 7 } ,
                                                    { 8 , 9 } ,
                                                    {10,11} ,
                                             }
                                      }
as we know total value it can store are 2 * 3 * 2 = 12 .

Let's see how to use Array .


Declaring an Array :-  
                                           The step involve in declaring Array are much similar  to simple variable . We must provide the following information 
Data type of array ie. int char etc.
Name of Array ( Same as scalar variable )
Number of subscript
Index value ie. number of variable

®One Dimensional Array :-
                                           As we Know the number of subscript tell the Dimension of array . In one dimensional array there is only one subscript and the value of subscript ie. Index value tells the total number of value it can store .
eg.
int one_dim[ 5 ] ;
                             /* It is an array of integer data type which can take 5 values*/
 
®Multi Dimensional Array :-
                                            As name says it is an array with more from one subscript . The multiplication of Index value tell the total number of values it can store .
eg.
int two_dim[ 4 ][ 5 ] ;
                                          /* It is two dimensional Array of integer data type
                                                                    which can store 4 * 5 = 20 values */
int mul_3dim[ 2 ] [ 3 ] [ 2 ] ;
                                         /* It is a three dimensional array of integer data type
                                                               which can store 2 * 3 * 2 = 12 values */


Initialising Array :- 
                                        As simple variable , Array can also be Declare & initialise at the same time .
® One dimensional array :-
                                                One dimensional array can also be initialise at the Declaration place  ie.
int name[ 5 ] = { 8 , 9 , 5 , 4 ,1 }  ;
In above example we have declared an array of integer data-type which can store 5 value & at the same time we have initialise the array with
name[ 0 ] =   8
name[ 1 ] =   9
name[ 2 ] =   5
name[ 3 ] =   4
name[ 4 ] =   1
these value are initial value given to array which  can be change in the program .

eg.   int name[ 5 ] = { 5 , 1 , 4 , 8 , 9 , 8 , 11 , 10 } ;
In above example we have declare Index value = 5 , BUT we are initialising array with more from Index value  . This type of mistake cause an error signal at the time of compilation .
If we do this type of mistakes in the program ie.
int name[5 ] , i ;
for( i = 0 ; i <= 10 ; i++ )
{
   scanf(" %d ",&name[ i ] ) ;
}
this type of mistake not give error , it give us chance to enter 10 value but it only store first 5 values .

eg.        int name[ 9 ] =  { 8 , 9 , 5 , 4 ,1 }  ;
In above example we have declare Index value = 8 and we are initialising only five value . It not give any type of error But if we print or use this array in program it take first five value as given and take garbage value for left of the variables ie. name[ i ] = { 8 , 9 , 5 , 4 ,1 }    ( i= 0 , 1 , 2 , 3 , 4 )
and for   name[ i ] = Garbage value  ( i = 5 , 6 , 7 , 8 )

®Multi Dimensional Array :-
                                                 Initialising Multi Dimensional array is slightly different from One Dimensional array .

------->Two Dimensional Array :- 
                                               Two Dimensional Array can also be initialise at the declaration time .
int name[ 4 ][ 5 ] = {
                                       {    0      ,      1      ,      2      ,      3      ,      4  } ,
                                       {    5      ,      6      ,      7      ,      8      ,      9  } ,
                                       {  10    ,      11      ,   12       ,    13     ,    14  } ,
                                       {  15    ,      16      ,   17       ,    18     ,    19  }
                               }
 in above example , we have declare a two dimensional array with first subscript = 4 ( ie. 4 rows ) & second subscript = 5 ( ie. 5 columns ) .

------->Three Dimensional Array :-
                                             Three dimensional array can also be declare & initialise at the same time .
int name[ 2 ][ 3 ][ 2 ] = {
                                            {     
                                                   { 0 , 1 } ,
                                                   { 2 , 3 } ,
                                                   { 4 , 5 } ,
                                             } , 
                                             {
                                                    { 6 , 7 } ,
                                                    { 8 , 9 } ,
                                                    {10,11} ,
                                             }
                                      }
as we know total value it can store are 2 * 3 * 2 = 12 .
 

Who's online

We have 13 guests and no members online

About Us

Harsimran Singh

Hello

This website is a special gift for Programming Lovers , which want to learn basic Programming Languages and want to develop there skills Read More....

Our work

Find Us

This email address is being protected from spambots. You need JavaScript enabled to view it.

Harsimran Singh(Facebook)

Harsimran Singh(about.me)

Harsimran Singh(G+)