You are on page 1of 16

Arrays or one dimentional arrays

Definition: Array is a collection of similar elements of same data type such as int, float, char , etc. Explanation: To store more than one value at a time in a single variable. This

is called subscripted variable ([]) or array.


Before using an array its type and size must be declared The first element in array is numbered 0

Last element is 1 less than the size of array


Its elements are always stored in contiguous memory location An array can be initialised at the same place where it is declared

Syntax:

datatype subscripted_variable[arraysize];

Example :

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

int specifies the type of the variable

n is name of variable
[] tells compiler dealing with array 6 tells how many elements in array . This is called dimension of

array
Dimension of array is optional. This mentioned in 2nd example. There is no value in subscript.

Storage structure
n[5] n[4] n[3] n[2] n[1]

6 5 4

2010 2008 2006

3 2
1

2004
2002

n[0]

2000

Two dimentional array


Definition:
It is a collection of number of 1-D arrays placed one after another . It is also called matrix.

Syntax datatype subscripted_variable[size1][size2];

Example :
main() { int n[5][2] = {

int i , j ;

} ;

{ { { { {

1 2 3 4 5

, , , , ,

2 5 4 5 6

} } } } }

, , , ,

for(i=0;i<=4;i++) { for(j=0;j<=1;j++) { printrf( %d ,n[i][j]); } } `

0th 1-D

1st 1-D

2nd 1-D

3rd 1-D

4th 1-D

4000

4002

4004

4006

4008

4010

4012

4014

4016

4018

int n[5][2] contains 5 one dimentional array . Each one dimentional array contains 2

values
n[i][j] 1 st subscript is row no 2 nd subscript is column no

n[0][0] 1 n[1][0] 2 n[2][0] 3 n[3][0] 4 n[4][0] 5

n[0][1] 2 n[1][1] 3 n[2][1] 4 n[3][1] 5 n[4][1] 6

Three dimentional array

Definition:
It is a collection of number of 2-D arrays placed one after
another . It is also called as array of array.

Syntax datatype subscripted_variable[size1][size2][size3];

Example
main() { int n[2][2][2] = { { {1,2} , {2,3} } , { {3,4}. {4,5} } }; Int I,j,k; For(i=0;i<=1;i++) { for(j=0;j<=1;j++) { for(k=0;k<=1;k++)

printf( %d , n[i]i[j][k]) ;
} }

0th 2-D

1st 2-D

0th 1-D

1st 1-D

0th 1-D

1st 1-D

4000

4002

4004

4006

4008

4010

4012

4014

int n[2][2][2] contains two 2-D array . Each 2-D array contains 2 1-D array Each 1-D array contains two elements

n[i][j][k]
I 2-D array j 1-D array or row k element or column

n[0][0][0] n[0][0][1]

n[0][1][0] n[0][1][1] 2 3

n[1][0][0] n[1][0][1] 3 4 n[1][1][0] n[1][0][1] 3 5

Quiz

What is the output ?

1. main() { int k[3]={1,2,3}; for(i=0;i<=3;i++) Printf(\n %d,k[i]); }

2. main() { int array[26] , i ; for(i=0;i<=25;i++) { array[i]=A+i; Printf(\n %d %c , array[i],array[i]); } }

3. main() { int arr[]={2,3,4,1,6} Printf( %d ,sizeof(arr)); }

4. main() { int arr[]={12 ,15 ,15 ,23 , 45 } Printf(%u %u,arr+1,&arr+1); }

5.

main()
{ Int size; Int arr[size]; Scanf(%d,&size); for(i=0;i<=size;i++) { Scanf(%d,arr[i]); Printf(%d,arr[i]); } }

You might also like