You are on page 1of 13

# ARRAYS

Limitation of fundamental datatypes  Variable of fundamental datatypes can store only one value at any given time.   . we need powerful derived datatype like arrays. To handle large amount of data. Inefficient to handle large amount of data.

 example : int a[5].Array An array is a fixed size sequence collection of elements of the same data type. char c[4].    . float f[10].

 .Some examples where concept of arrays can be used.  List of employees in an organization List of students in a class.   List of subjects in a semester List of marks of 5 subjects.

} /* /* /* /* array array array array of of of of 365 floats */ 12 chars */ 50 ints */ 10 ints */  Above “[]” brackets identifies “a” as an array We can access individual elements by writing its index number or subscript in square brackets along with the array name. int debts[10]... char b[12]. Declaration of array. int main(void) { float a[365].  . .How the Compiler recognizes an Array?   Just like variables and functions array have to be declared before their usage. int c[50].

5 8    debts[9] . . . .How to Access the Elements of Array Array Index The index of an Array starts at index 0 . Example int debts[10] debts[1] debts[2] . .continue till one less than debts[0] the size of the array. debts[8] Array Data 1 2 1 .

a[3]).Input and output array element  scanf(“%d”.&a[0]).  . printf(“%d”.

.64}.4.Array Initialization int arr[8] = {1.32.6.78}. float f[3]={2.56.34.6.8.2.4.16.

1}.'H'.   char name[ ]={'J'.'\0'}.1.'N'.Another way int counter[ ]={1. char name[ ]=”JOHN”.  .1.'O'.

char city[5]={'B'}.Partial intialization  int number[5]={10.  .20}.

  number[2]=x[5]+y[10].Array element manipulations  a=number[0]+10. value[6]=number[i]*3. number[4]=number[0]+number[2].   .or expressions that yield integers.integer variables like i. The subsscripts of an array can be integer constants.

size must be > 0 // no. float a3[sizeof(int) + 1]. Remember that indexing starts at 0 in C. float a6[2. typecast float to int constant . // yes float a4[-4].5]. size must be an integer // yes.5]. // no. so it’s the programmer responsibility. float a5[0]. How to avoid!    Use a symbolic constant for length of Array index.Array Bounds   C compiler wont check the boundary of your array. size must be > 0 // no. Reason! To make C faster. float a7[(int)2. // yes // yes float a1[5]. float a2[5*2 + 1].

columns=4   Total elements = 12 (3*4) . Rows=3.Two dimensional array  int a[3][4].