Array 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Arrays

Contents

Definition
Declaration
Types
Accessing An Array elements
Paasing ( initialization ) values to an array
Operation

Definition :
It is derived data type which is a group
(colloection) of similar type of data elements stored at
contigous memory locations.

Or
It is an ordered set of homogenous data elements.
Declaration

one can define an array by using following syntax

datatype array-name [ size 1] [size2] [size3].....[size n] ;

where,

datatype :It is a simple ( Basic ) datatype


array-name : A valid array-name

[ size 1] [size2] [size3].....[size n] : An aray’s dimension

Example :
1) int abc[5] ;
abc is an array of 5 data elements of type integer
( 1 dimension )

2) float xyz [2[4];


xyz is an array( 2-D) of 2*4=8 data elements of type float
(2 dimension )
An array can be classified into :

I) One dimensional ( 1-D) array

II) Mutli- dimensional ( MD) array


( 2 D array )

I) One dimensional ( 1-D) array ( one subscripted variable)


i) Definition

when an array is used to store data values( elements) in


either row-wise or column-wise then it is called as 1-D array.

ii) Declaration

one can use following syntax to declare 1-d array

datatype array-name [size] ;


where ,

data type : It is basic data type.

array-name : A valid name .

[size] : It is positive integer constant


iii)Example
i) int abc[ 5+5]; valid
ii) char xyx [5*2]; valid
iii) float st [ 6/4]; invalid
iv) int w [-5]; invalid

II) Accessing an array elements ( Array index )

Like variable , an array has to declare before it is used.

One can access an array elements by indexing i.e.

index of an array start from 0 to size-1.

consider example ,

float abc [ 10/2]; float abc [ 5];

abc[0 ] abc[ 1 ] abc[2] abc[ 3 ] abc[ 4]

accessing the first element of an array we must access index 0


i.e. abc[0]

accessing the second element of an array : abc[1]


accessing the third element of an array : abc[2] & so on.
float abc [ 5] ; // declaration

Here , abc is an array of float type with 5 data elements .

i.e. an array abc has definition as well as declaration.

defining means , compiler will allocate a contiguous block of


memory of size = 5*sizeof(float) =5 * 4bytes = 20 bytes of
memory.

abc[0] abc[1] abc[2] abc[3] abc[4]


100 - 103 104-107 108 - 111 112-115 116 - 119
III) Passing values to an array

i) After declaring an array one must intialize( pass values to) an


array elements otherwise it may hold garbagevalue ( any
random value) by using one the following method :

I) Static ( compile-time) initialization


II) Dynamic (Run-time) initialization

I) Static ( compile-time) initialization

i) Just like normal variable initializtion , an array also can be


initialize at time it’s declaration .

ii) syntax

data – type array-name [size] = {list of values };

iii) Example

i) int abc [ 5] = { 2 , 3 , 4 , 5 , 6};

Here an array abc with 5 integer data elements with values


shown in following memory mapping diagram

2 3 4 5 6
abc[0] abc[1] abc[2] abc[3] abc[4]
II) run time
i) Here, an array can initiailzed by using scanf() which interms
use for loop at run-time.

ii) This is usefull for initializing larger arrays values .

Iii) Example

// 1-D array run time initialization

// Title : Program for accepting 15 integer values & display


them

#include <stdio.h>

int main()
{
int abc [15]; // an array abc with 15 integer data elements
int a // loop counter variable;

printf("Enter the values for abc ");

for(a=0; a<15;a++ ) // input stmt (array index )


{
scanf("%d",&abc[a]) ;
}

for(a=0;a<15;a++)

{
printf("\t%d\n",abc[a]); // Process & O/p
}
}

You might also like