10 Arrays
10 Arrays
Index
Introduction to Arrays.
Declaration of a Array. Variables, Creating Arrays.
The Length of Arrays.
Initializing Arrays.
Multidimensional Arrays.
Passing array to function
Accessing array through pointer
Introduction to ARRAYS
Array is a data structure that represents
a collection of the same types of data.
Data Types
Data items
Name of array
a –array of 10 integers
Square braces represents number of elements
Array index starts from a[0] as shown below:
28 15 86 54 78 21 34 7 91 67
Array1.c
Take the case where a is an array int a [10]
What will be the output of the following statement?
printf(“%d”, sizeof(a[0]));
printf(“%d”, sizeof(a));
2-D Arrays
Marks[0][0]=35.5
Marks[1][1]=55.5
Marks[2][1]=70.0 T
Marks[3][1]=85.0
Storage of 2-D ARRAY
200 204 208 212 216 220 224 228 232 236 240 244
Marks[0][0]
Marks[1][0] Marks[2][0] Marks[3][0]
Marks[0][1]
Initialization:
Float marks[4][3]={ {35.5,40.5,45.5},
{50.5,55.5,60.5},
{65.0,70.0,75.0},
{80.0,85.0,90.0} };
T
3-D ARRAY
Array3d.c
Passing ARRAY to a Function
/*Pass by Value*/
main( )
{
int i ;
int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i <=6 ; i++ ) {
display ( marks[i] ) ;}
}
display ( int m )
{
printf ( "%d ", m ) ;
}
arrpv.c
Passing ARRAY to a Function
/* Call by reference */
main( )
{
int i ;
int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i <= 6 ; i++ ) {
disp ( &marks[i] ) ; }
}
disp ( int *n )
{
printf ( "%d ", *n ) ;
} arrpcallr.c
POINTERS and ARRAYS
Facts to be considered:
Array elements are always stored in contiguous
memory locations.
Pointer when incremented always points to
•Array: Array is collection of similar data type, you can insert and
deleted element form array without follow any order.
•Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last
entered element removed first.
•Queue: Queue work on the basis of First-In-First-Out (FIFO). First
entered element removed first.
•Linked List: Linked list is the collection of node, Here you can insert
and delete data in any order.
•Tree: Stores data in a non linear form with one root node and sub
nodes.