Sei sulla pagina 1di 13

Name:-

Kasundra Navneet
Enroll. No.:- 130170119013
Branch:- Mechanical
Division:- E
Mobile No.:- 8690239123
Email:- navneetkasundra@gmail.com
1

Arrays
Array

is a collection of variables of the same type.


Array is referred through a common variable
name.
Array occupies contiguous memory locations.

Single Dimension Arrays


General

Form : Type Var_name[size];


e.g. int a[3];
A specific element in an array is accessed by an
index.
NAME OF ARRAY
a
INDEX

10

20

30

ACTUAL VALUES

Single Dimension Arrays


The

first index number is zero.


The last index number is size-1.

2 in this example.

a
INDEX

10

20

30

NAME OF ARRAY

ACTUAL VALUES

Single Dimension Arrays


Size

of an array:
Considering 16-bit compiler
Total bytes= sizeof(base type)*size of array.
= Sizeof(int)*3
= 6 bytes (in this example).
C/C++ has no bounds checking on arrays.
If you exceed array boundary, you may write into
some other variables data or even into the
program code.

Declaration & Initialization of Array


int a[3] = { 10 , 20 , 30 };
or
int a[ ] = { 10 , 20 , 30 };
in 2nd example, mentioning the size of array is
optional. Compiler will automatically calculate the
size of the array.
char nm[ ] = VGEC;
or
char nm[5] = VGEC;
or
char nm[ ] = { V , G, E , C, \0 };
6

Various Operations on Arrays

Input values to an array


Can

be done in two ways:


Single element wise:
scanf(%d, &a[0]);
scanf(%d %d, &a[2] , &a[1]);
i = 2 ; scanf(%d, &a[i]);
Whole array wise:
for ( int i = 0 ; i < 3 ; i++)
scanf(%d, &a[i]);

Assign values to an array


Can

be done in two ways:


Single element wise:
a[0] = 10;
a[2] = a [1] = 20;
i = 2 ; a[i] = 30; or a[i] = ( i + 1 ) * 10;
Whole array wise:
for ( int i = 0 ; i < 3 ; i++)
a[i] = (i+1) * 10;

Print values of an array


Can

10

be done in two ways:


Single element wise:
printf(%d, a[0]);
printf(%d %d, a[2] , a[1]);
i = 2 ; printf(%d, a[i]);
Whole array wise:
for ( int i = 0 ; i < 3 ; i++)
printf(%d, a[i]);

Passing individual element of an array to a


function
void main( )
{
void display ( int );
int i , marks[5] = { 70, 80, 75, 60, 55 };
for ( i = 0 ; i < 5 ; i++ )
display ( marks [i] ) ;
}
11

Passing individual element of an array to a


function
display

function.
void display ( int m )
{
printf(%d\n, m);
}

12

Passing whole array to a function


void main ( void )
{
int x[10];
func1(x);
}

13

Can be done in 3 ways:


Unsized array:
void func1 ( int x[ ] ) {}
Sized array:
void func1 ( int x[10] ) {...}
Pointer:
void func1 ( int * x ) {}
Try to understand this slide after learning Pointer.

Potrebbero piacerti anche