CONCEPT OF ARRAY IN DATA STRUCTURES CONCEPT OF ARRAY IN DATA STRUCTURES
1.
CONCEPT OF ARRAYIN DATA
STRUCTURES
Ms.C.NANDHINI
Assistant Professor
Department of Information Technology
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
1
INTRODUCTION
• Arrays arecollections of related types of data
items that are stored in adjacent memory
locations.
• It is one of the most basic data structures in
which each data element can be accessed at
random by using its index number.
1001 1002 1003 1004 1005
56 43 65 98 12
0 1 2 3 4
Address
Element
Index
4.
TERMINOLOGY
• TYPE- Thetype of array represents the kind of
data type it is meant for. For example an array
of integers, an array of characters, strings etc.
Int i=10;
Int m=20;
Int j=50;
Int u=45;
Multiple variables
to stores same type
of data element
10
int arr[4]={10,20,50,45}
20 50 45
Single variable to store multiple
values of same type
Variable arr of size 4 follows
integer type
5.
TERMINOLOGY
• SIZE- Thenumber of elements in an array is
called size of array. The size is also called
length or dimension.
6.
TERMINOLOGY
• BASE- Thebase of an array is the address of
the memory location where the first element
of the array is located.
453
454
455
.
.
.
7.
TERMINOLOGY
• INDEX- Allthe elements of an array can be
referenced by subscript like Ai or A[i] this
subscript is known as index.
• An index is always an integer value.
8.
Memory Allocation ofan array
• In the above image, we have shown the memory allocation of an
array arr of size 5.
• The array follows a 0-based indexing approach. The base address
of the array is 100 bytes.
• It is the address of arr[0]. Here, the size of the data type used is 4
bytes; therefore, each element will take 4 bytes in the memory.
9.
How to accessan element from the
array?
• We required the information given below to access any random
element from the array -
Base Address of the array.
Size of an element in bytes.
Type of indexing, array follows.
• The formula to calculate the address to access an array element -
• Here, size represents the memory taken by the primitive data types.
As an instance, int takes 2 or 4 bytes, float takes 4 bytes of
memory space.
Byte address of element A[i] = base address + size * ( i - first index)
10.
• We canunderstand it with the help of an
example -
• Suppose an array, A[0…..9] having Base
address (BA) = 104 and size of an element(s) =
2 bytes, find the location of A[3].
• Address(k) = BA+s(k-LB)
• A[3] = 104+ 2 x [3 - 0]
= 104+6
=110
Basic operations
• Traversal- This operation is used to print the
elements of the array.
• Insertion- is used to add an element at a particular
index.
• Sorting- It is used to sort an element in a specific
order(ascending/ descending)
• Deletion- is used to delete an element from a
particular index.
• Search - It is used to search an element using the
given index or by the value.
16.
Traversal operation
• Thisoperation is performed to traverse through the array
elements. It prints all array elements one after another. We
can understand it with the below program –
#include <stdio.h>
void main() {
int Arr[5] = {18, 30, 15, 70, 12};
int i;
printf("Elements of the array are:n");
for(i = 0; i<5; i++) {
printf("Arr[%d] = %d, ", i, Arr[i]);
}
}