Introduction of Arrays. Arrays Array form an important part of almost all programming language. It...

42
Introduction of Arrays

Transcript of Introduction of Arrays. Arrays Array form an important part of almost all programming language. It...

Introduction of

Arrays

Arrays

Array form an important part of almost all programming language.

It provides a powerful feature and can be used as such or can be used to form complex data structures like stack and queue.

An array can be defined as an infinite collection of homogeneous (similar data type) elements.

This means that an array can store either all integer, all floating point numbers, all characters or any other complex data type but all of same type.

Arrays

there are some important points to be pointed out about arrays. Arrays are always stored in consecutive

memory locations. An array can store multiple value which can be

referenced by a single name unlike a simple variable which store one value at a time. Followed by an index or subscript, specified inside a square bracket.

Arrays

Array name is actually a pointer to the first location of the memory block allocation to the name of the array.

An array either be a integer, character or floating data type can be initialized only during declaring time and not afterwards.

There is no bound checking concept for arrays in C. it means you can attempt to enter any no of values irrespective of the integer index specified inside square brackets, during declaration of arrays. It is up to programmer to check upper bound of array.

Types of Arrays

There are three types of arrays: One-Dimensional Arrays Two-Dimensional Arrays Multi-Dimensional Arrays

One-Dimensional Array

A one-dimensional array is one in which only one subscript specification is needed to specify a particular element of the array.

One dimensional array can be declare as follows:

data_type array_name [size]; Data type is the type of elements to be stored in

the array. Variable name specifies the name of array, it may

be given any name like other simple variable.

One-Dimensional Array

Size the specifies the no of values to be stored in the array, it must be integer value.

The index of the array start from o to onwards. Declaration of one-dimensional array:

int num[5];

One-Dimensional Array It array will store five integer values, its name is num.

it can be visualized as show. index data

num[0] 10num[1] 20num[2] 30num[3] 40num[4] 50

size of array=(upper bound – lower bound) +1 =4+0+1

=5

One-Dimensional Array

Initialization of one dimensional arrays:

int arr[5]={10,20,30}; Here, initialization must be constant value at

compile time. Here, arr[0] to arr[2] has assign a value but other

element assign a zero by default.

One-Dimensional Array :Example

Void main()

{

int arr[5]={10,20,30,40,50};

int i;

for (i=0;i<5;i++)

{

printf(“\n[%d] value is:%d”,arr[i]);

}

}

Two-Dimensional Array

Two-dimensional arrays are as a grid. Two dimensional array can be declare as follows:

data_type array_name [row size] [column size]; the first number in brackets is the number of rows,

and second number in brackets is the numbers of column.

So the upper left corner of any grid would be element [0][0].

Two-Dimensional Array

Declaration of two-dimensional array: int arr[3][3];

Here, this example graphical or pictorial representation of 2-D array.

Arr[0][0] Arr[0][1] Arr[0][2]

Arr[1][0] Arr[1][1] Arr[1][2]

Arr[2][0] Arr[2][1] Arr[2][2]

0

1

2

0

1

2

0

1

20 1

Two-Dimensional Array Initialization of Two dimensional arrays:

int arr[2][3]={ {10,20,30}, {40,50,60}

}; Bellow show the data in 2-D grid.

10 20 30

40 50 60

0

1

0

1

1 20

Two-Dimensional Array : ExampleVoid main(){int arr[2][3]={ {10,20,30}, {40,50,60} };int i,j; for (i=0;i<2;i++) { printf(“\n”); for(j=0;j<3;j++) { printf(“%d ”, arr[i][j]); } }}

Implementation of Two-Dimensional Array in memory

A two-dimensional array can be implemented in a programming language in two ways: Row-major implementation Column-major implementation

Two-Dimensional Arrays:Row-major Implementation

Row-major implementation is a linearization technique in which elements of array are reader from the keyboard row-wise that means the complete first row is stored then the complete second row is stored and so on.

For example an array [3][3] is stored in the memory as show bellow:

A00 A01 A02 A10 A11 A12 A20 A21 A22

Row 1 Row 2 Row 3

Two-Dimensional Arrays Row-major Implementation

The storage can be clearly understood by arranging array as matrix as show bellow:

A00 A01 A02

A10 A11 A12

A20 A21 A22

Row 1

Row 2

Row 3

a=

Two-Dimensional Arrays: Row-major Implementation

Address of elements in row major implementation: the computer does not keep the track of all elements of

the array, rather it keeps a base address and calculates the address of required element when needed.

It calculates by the following relation:

address of element a[i][j]=B+W(n(i-l1)+(j-l2))

Two-Dimensional Arrays: Row-major Implementation

Here, B=Base address W=size of each element array element n=the number of column (u2-l2+1)

l1 the lower bound of row

l2 is lower bound of column

u1 upper bound of row

u2 upper bound of column

Two-Dimensional Arrays: Row-major Implementation

Example 1: a two-dimensional array defined as a[4:7,-1:3] requires 2 bytes of storage space for each element. If the array is stored in row-major form , then calculate the address of element at location a[6,2] given base address is 100.

Sol: B=100, l1=4, l2=-1, u1=7, u2=3,W=2(int size) i=6,j=2 and n(no. of col)= u2- l2+1=3-(-1)+1=5 Address of a[i][j]= B+W(n(i-l1)+(j-l2)) Address of a[6][2]=100+2(5(6-4) + (2-(-1)) =100+(2(5*2+3) =100+26 =126

Two-Dimensional Arrays: Column-major Implementation

In column major implementation memory allocation is done column by column that means first the elements of the complete first column is stored then elements of complete second column is stored and so on.

For example an array [3][3] is stored in the memory as show bellow:

A00 A10 A20 A01 A11 A21 A02 A12 A22

Col 1 Col 2 Col 3

Two-Dimensional Arrays Column-major Implementation

The storage can be clearly understood by arranging array as matrix as show bellow:

A00 A01 A02

A10 A11 A12

A20 A21 A22

Col 1 Col 2 Col 3

a=

Two-Dimensional Arrays: Column-major Implementation

Address of elements in Column major implementation:

It calculates by the following relation:

address of element a[i][j]=B+W(m(j-l2)+(i-l1))

Two-Dimensional Arrays: Column-major Implementation

Here, B=Base address W=size of each element array element m=the number of row (u1-l1+1)

l1 the lower bound of row

l2 is lower bound of column

u1 upper bound of row

u2 upper bound of column

Two-Dimensional Arrays: Column-major Implementation

Example 1: a two-dimensional array defined as a[-20:20,10:35] requires one bytes of storage space for each element. If the array is stored in column-major form , then calculate the address of element at location a[0,30] given base address is 500.

Two-Dimensional Arrays: Column-major Implementation

Sol: B=500, l1=-20, l2=10, u1=20, u2=35 W=1 byte i=0,j=30 and m (no. of col)= u1- l1+1 =20-(-20)+1=41 Address of a[i][j]= B+W(m(j-l2)+(i-l1)) Address of a[0][30]=500+1(41(30-10)+(0-(-20)) =500+1(41*20+20) =500+1(820+20) =500+840

=1340

Multi-Dimensional Array

C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The general form of a multi-dimensional array is:

type array_name[s1][s2][s3]…..[sm]; Suppose 3-D array can be group of an array of

arrays. For example : int a[3][3][2]; Here 3-D array which is collection of three 2-D

arrays each contain 3 rows and 2 column.

Multi-Two-Dimensional Array For example:

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

{ {1,2},{3,4},{5,6}},

{ {1,2},{3,4},{5,6}}

}; Memory representation of above 3-D array is bellow:

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6

0th 2-D array 1st 2-D array 2nd 2-D array

Passing Arrays To Functions:One-Dimensional Arrays:Examplevoid show (int a[],int s){ int i;

for (i=0;i<s;i++) { printf(“\n[%d] value is:%d”,a[i]); }}void main(){ int arr[5]={10,20,30,40,50}; show(arr,5);}

Passing Arrays To Functions:Two-Dimensional Arrays:Examplevoid show (int a[][3],int r,int c){ int i,j; for (i=0;i<r;i++) {

printf(“\n”); for(j=0;j<c;j++) { printf(“%d ”, arr[i][j]); } }}

Passing Arrays To Functions:Two-Dimensional Arrays:Example

void main(){ int arr[2][3]={ {10,20,30}, {40,50,60} }; show(arr,2,3);}

Pointer & Arrays

An array is a collection of homogeneous type of element stored in adjacent memory location.

Therefore it is used in the situation to store more than one value at a time in a single variable.

Array & pointer have a close link in fact array in itself acts like a pointer, as one element in a array is stored adjacent to the previous.

Therefore which displaying the elements of array the next adjacent address is automatically called when its previous element is displayed.

Pointer & Arrays

But in case of using pointers with arrays, the element can be stored in arrays at different location and can be called from that location when needed.

Two point must be remember: Array elements are always stored in contiguous

memory location. The increment or decrement of pointers leads to

increment or decrement of address based on the type of pointer define.

Pointer & One-Dimensional Array

Pointer along with single dimensional array can be used either to access a single element or it can also be used to access the whole array.

Given pointer only points to one particular type, not to all possible type.

Pointer & One-Dimensional Arrayvoid main(){ int *p,i; int arr[5]={10,20,30,40,50}; p=&arr[0]; for(i=0;i<5;i++) { printf(“\n element =%d”,*p); p++; } getch();}

Pointer & Two-Dimensional Array

Pointer is used in two-dimensional arrays in the same way as in single dimension.

The address of the first element of the matrix can be passed on the pointer and the rest of elements can be displayed.

For example : int a[3][2]={{10,20}, {30,40},{50,60}};

The above initialized arrays will be stored in memory in following arrangement:

Pointer & Two-Dimensional Array The above initialized arrays will be stored in memory in

following arrangement:

The program using pointer to handle 2-D array show in word file.

Elements 10 20 30 40 50 60

Matrix Location

A[0][0] A[0][1] A[1][0] A[1][1] A[2][0] A[2][1]

Address 1001 1003 1005 1007 1009 1011

Array of Pointer

Arrays of pointers refer to homogeneous collection of pointers that is collection of pointers the same data type.

The pointer are declared in array from same as other data types.

For example an integer pointer array of size 50 can be declared as:

int *ptr[50]; A pointer may also be used with more than one

array declaration of same data type for an array pointer of size 50 can be used for first array of size 20 and the third of 10.

Array of Pointer

Here, the pointer will be declared as: int a[20],b[20],c[10]; int *ptr[5]; In above give expression, we can assign the

address element of each array as:ptr[0]=&a[0];ptr[1]=&b[0];ptr[2]=&c[0];

The program using array of pointers shown in word file.

Array of Structure

A structure is simple to define if there are only one or two elements, but in case there are too many objects needed for a structure, for example a structure designed to show the data of each student of the class, then it that case the array will be introduced a structure declaration using arrays to define objects is given in next slide.

Array of Structurestruct student

{

char name[15];

int rollno;

char result[10];

};

Struct student data[60]; The above declaration sets the memory space for

60 objects. To see the detail example in word file.

Array within the Structure

We have used arrays outside the structures with the object declaration.

Arrays can also be used within the structures along with the member declaration.

In other words, a member of a structure can be an array type also.