Arrays An array is a derived data type

  • Slides: 10
Download presentation
Arrays An array is a derived data type. It is used to store homogeneous

Arrays An array is a derived data type. It is used to store homogeneous data items in contiguous memory location.

Types of Array • 1 - Dimentional • N-Dimentional

Types of Array • 1 - Dimentional • N-Dimentional

One Dimentional Array Syntax data_type array_name[size of array / index]; Example : int a[5];

One Dimentional Array Syntax data_type array_name[size of array / index]; Example : int a[5]; Here ‘a’ is declared as an array of int data type that is capable of storing 5 integer values naming from a[0] to a[4]. a[0] a[1] 23 12 a[2] -10 a[3] 4 a[4] 37

Two-Dimentional Array Syntax data_type a_name[row size][column size]; Example : int m[2][3]; Here ‘m’ is

Two-Dimentional Array Syntax data_type a_name[row size][column size]; Example : int m[2][3]; Here ‘m’ is an 2 -D array of int type that is capable of storing 6 different integer values in 2*3 matrix form.

int m[2][3] m[0][0] 45 -12 m[1][0] m[0][1] -98 m[0][2] 23 0 m[1][1] 5 m[1][2]

int m[2][3] m[0][0] 45 -12 m[1][0] m[0][1] -98 m[0][2] 23 0 m[1][1] 5 m[1][2]

Array Initialization It can be performed in two ways a) Initialization During Declaration b)

Array Initialization It can be performed in two ways a) Initialization During Declaration b) Initialization After Declaration

Initialization During Declaration Example int a[5]= { 4, -7, 10, 8, 12 }; Here

Initialization During Declaration Example int a[5]= { 4, -7, 10, 8, 12 }; Here compiler will automatically assign a[0]=4, a[1]= -7, a[2]= 10, a[3]= 8, a[4]= 12

Initialization After Declaration It can be done in two ways a) Initializing values from

Initialization After Declaration It can be done in two ways a) Initializing values from user without loop b) Initializing values from user with loop

Initializing values without loop Example int a[5]; printf(“n Enter 5 integer values…”); scanf(“ %d

Initializing values without loop Example int a[5]; printf(“n Enter 5 integer values…”); scanf(“ %d %d %d”, &a[0] , &a[1] , &a[2] , &a[3] , &a[4]);

Initializing values from user with loop \ WAP to input 5 number from user

Initializing values from user with loop \ WAP to input 5 number from user and print void main() { int i=0, a[5]; printf(“n. Enter 5 integer values…”); for(i=0; i<5 ; i++) { scanf(“%d “, &a[i] ); } printf(“n The Entered values are…”); for(i=0; i<5 ; i++) { printf(“n%d “, a[i] ); } getch(); }