Sunday, April 6, 2008

Arrays in C

Arrays is a type of Data Structure
  • An array is a collection of elements of the same data type
  • All the elements have the same name
  • They are differentiated from one another by their positions (index) within the array .
  • Data is stored in contiguous (one next to the other ) memory locations.Hence array processing is faster.

e.g. int arr[ 5 ] = { 1,2,5,7,9 } ;

Here arr is an array of 5 integers referred as arr[0] , arr[1] , arr[2] , arr[3] , arr[4]

Memory mapping:

arr[0] arr[1] arr[2] arr[3] arr[4]

1 2 5 7 9

Index or subscript of an array :- The numbers 0 , 1, 2 , 3, …. are used as array index to refer to each element of the array. The first element of array is indexed as 0 and not 1. Hence arr[4] is the 5th element. Arrays are known as indexed or subscripted variables

Arrays and Loops :

Generally a loop is used to process all the elements of the array

Programs:

void main( )

{

int i , a[5] = { 1, 2, 5, 7, 9 } ; /* An array can be assigned */

/* while declaring */

clrscr( ) ;

for( i = 0 ; i < style=""> i++ )

printf(“ %d ”, a[ i ] ) ; /* Output */

} /* 1 2 5 7 9 */

void main( )

{ /* F O C U S */

int i;

char a[5] = { ‘F’, ‘O’, ‘C’, ‘U’, ‘S’ };

clrscr();

for( i = 0 ; i< style=""> i++)

putchar (a [ i ] ) ;

}


Two Dimensional Array

  • A Two dimensional array is a data structure
  • It means an array involving two dimensions with rows and columns .
  • We interpret 1st dimension as row and 2nd as column (matrix).

eg. int a [ 2 ] [ 3 ] = { 1 , 5 , 7 , 9 , 11 , 25 } ;

a[2][3] has 2 rows with each row having 3 columns

Human interpretation

column# 0 1 2

row#0 0 1 5

row#1 9 11 25

MEMORY MAPPING :The elements of a 2d array are stored in contiguous locations just like a single dimensional array

1 5 7 9 11 25

0th array 1st array

A two dimensional array is a collection of single dimensional arrays.

MEMORY ALLOCATION =No.of elements X Sizeof Datatype = 6 X 2 = 12 bytes

Two Dimensional Arrays and Nested Loops :

Generally nested loops are used to process all the elements of a 2d array. The outer loop gen. refers to rows and the inner loop to columns.

eg:

main( )

{

int i , j ;

int a[2][3] = { 10, 20 , 30 , 4 , 5 , 6 } ;

clrscr( ) ;

for ( i = 0; i <>

for ( j = 0 ; j <>

printf ( “%5d“ , a [ i ] [ j ] ) ;

}

A 2D array can be used to store a collection of words , lines (strings)

In the following example note the use of a single loop to process the names.

main( )

{

char names[5][10]; /* 5 names each with max 10 characters */

int j ;

for ( j = 0 ; j <>

{

printf(“ Enter name : “);

gets ( names [ j ] );

}

printf(“ You have entered : \n\n “);

for( j = 0 ; j <>

puts( names [ j ] ) ;

getch();

return 0 ;

}



A Data Structure is a way of managing data i.e., storing and retrieval .

· C provides basic data types char , int , float , double and pointers

· It is possible to derive several data structures like arrays , linked list, queue from these basic data types to suit our applications

No comments: