Tuesday, December 7, 2010

ARRAYS

ARRAYS
It is data structures which contains or hold multiple values of same data type.



Array is a collection of similar data elements.


Before using an array its type and dimension must be declared.


Arrays elements can be stored in contiguous memory locations.


In C, there is no boundary checking for arrays.


Size indicates the maximum number of elements that can be stored inside the array.


Array elements starts with ZERO.


To enter data into array elements using with LOOPS.


Syntax of single dimension array:


Datatype arrayname[size of the array]={list of values};


Example:


int n[10];


size of the array is 10.


Type of the array is int.


Declaration of array:


int n[10]={1,2,3,4,5,6,7,8,9,10};


Read the array variables using FOR LOOP:


int i,n[10];


for(i=0;i<9;i++)


scanf(“%d”,&n[i]);


It reads 10 array elements start with 0 to 9 and stored in memory.


MULTIDIMENTIONAL ARRAYS: 



It allows one or more dimensional arrays also.


To store and manipulate two dimensional data structures such as matrices and tables.


Declaration of two dimensional arrays:


Datatype arrayname[rowsize][columnsize];


Example:


int n[2][3];


In above example 2 is the row size and 3 is column size.


Total memory length is, row*column*sizeof the datatype = 2*3*2 = 12 bytes.


Initialization of the two dimensional array:


int n[2][3]={ {1,1,1},{2,2,2}};


Declaration of multidimensional array:


Datatype arrayname[r1][r2].....[r n];


Example of single dimensional array:


To read single dimensional array and add the array elements.


main()


{


int a[10],i,sum; //variable declaration


for(i=0;i<9;i++) //reading of array elements


{


printf(“\n Enter a[%d]:”,i);


scanf(“%d”,&a[i]);


}


sum=0; 


for(i=0;i<9;i++) 


sum+=a[i]; //adding of array elements


printf(“\nSum of array elements is:%d”,sum);


}


Output:


Enter a[0]=1


Enter a[1]=2


Enter a[2]=3


Enter a[3]=4


Enter a[4]=5


Enter a[5]=6


Enter a[6]=7


Enter a[7]=8


Enter a[8]=9


Enter a[9]=10


Sum of array elements is:55


Example of double dimensional array:


To read and write two dimensional array.


main()


{


int a[5][5],i,j; //variable declaration


for(i=0;i<5;i++) //for row elements


for(j=0;j<5;j++)//for column


{


printf(“\n Enter a[%d][%d]:”,i,j);


scanf(“%d”,&a[i][j]);


}


printf(“Display of array elements:\n”);


for(i=0;i<5;i++) //for row elements


{


for(j=0;j<5;j++)


printf(“ %d\t”,i,j,a[i][j]);


printf(“\n”);


}


}


Output:


Enter a[0][0]:1


Enter a[0][1]:2


Enter a[0][2]:3


Enter a[0][3]:4


Enter a[0][4]:5


Enter a[1][0]:1


Enter a[1][1]:2


Enter a[1][2]:3


Enter a[1][3]:4


Enter a[1][4]:5


Enter a[2][0]:1


Enter a[2][1]:2


Enter a[2][2]:3


Enter a[2][3]:4


Enter a[2][4]:5


Enter a[3][0]:1


Enter a[3][1]:2


Enter a[3][2]:3


Enter a[3][3]:4


Enter a[3][4]:5


Enter a[4][0]:1


Enter a[4][1]:2


Enter a[4][2]:3


Enter a[4][3]:4


Enter a[4][4]:5


Display array of elements:


1        2        3        4        5

1        2        3        4       5

1        2        3        4       5

1        2        3        4       5

1        2        3        4       5