You are on page 1of 22

Array Operations

Dr. Beulah Christudas,


Karunya Institute of Technology and Sciences
Arrays
✔ Definition : Array is defined as a data structure that stores a collection of
data of the same data type under a common name.
✔ The elements of the array are stored in contiguous memory locations.
✔ It is a homogeneous data structure and it can store data of the same data
type.
✔ Array can be considered as a variable that stores multiple data. Arrays
should be declared before they are used.
✔ The data type and the maximum size of the array should be specified
during declaration.
✔ The array elements are accessed using the array name and the position of
the element known as the index, specified within the subscript operator
[ ].
✔ The array index starts with zero. The index of the last element in an array
of size n is n-1.
✔ Syntax of array declaration:

data_type var_name[max size];

✔ int a[10];
✔ float height[5];
✔ double price[10];
✔ char s[12];
Address of Array Elements
✔ Formula : address(k) = base(a) + w( k – lower_bound)
✔ k : index of the element for which the address is to be calculated.
✔ a : name of the array.
✔ base(a) : base address of array ‘a’.
✔ w : word size of a single element.
✔ lower_bound : index of the first element in the array [usually zero].
✔ Ex.

✔ Consider the array int a = {5,10,15,20,25,30,35,40,45,50}

✔ Given that the base address of the array is 1200.

✔ Calculate the address of the element ‘40’

✔ Solution
✔ Given base(a) = 1200.
✔ Since it is an integer array, w = 4 bytes.
✔ Lower_bound = 0
✔ k=index of ‘40’ = 7
✔ address(40) = 1200 + 4 * (7-0) = 1228
Array Initialization
✔ int a[5] = {1, 2, 3, 4, 5};
✔ int a[10] = {1, 2, 3, 4, 5};
✔ float b[] = {12.64, 53.53, 7.4566};
✔ char c[10] = {‘a’,’p’,’p’,’l’,’e’};
✔ char d[]={‘o’,’r’,’a’,’n’,’g’,’e’};
✔ char city[]=”Coimbatore”;
Characteristics of Arrays
✔ The declaration of an array creates an array that would hold a maximum
of elements. In other words, it allocates memory space required for the
maximum number of specified data contiguously.
✔ All the elements of an array should be of the same data type.
✔ All the elements of an array share the same name but they are identified
by the subscript or index value. It is also called as the element number or
position of the element.
✔ Any element of an array can be modified without disturbing the other
elements.
✔ Any element of an array can be assigned to a variable of the same type.
✔ e.g. x = a[2];

✔ The amount of storage required for an array is calculated as the sum of


the storage required for all the elements together
✔ Size of array = Number of array elements * sizeof (datatype)
✔ Ex. For an integer array that can hold a maximum of 5 elements, the storage space
required is 5 * 4 = 20 bytes.
Operations on Arrays
1. Traversal
void read(int a[10], int n) void print(int a[10], int n)
{ {
int i; int i;
cout<<"Enter the elements : "; cout<<"\nThe array elements are\n";
for (i=0;i<n;i++) for (i=0;i<n;i++)
cin>>a[i]; cout<<a[i]<<'\t';
} }
int main() {
int a[10],n;
cout<<"Enter the number of elements : ";
cin>>n;
read(a,n);
print(a,n);
}
2. Inserting an Element into an Array
void insert(int a[10], int n, int t, int pos) 0 1 2 3 4
{
10 20 30 40 50
int i;
for (i=n-1;i>=pos;i--) t = 30; pos = 2
Push 50 from pos 3 to 4
a[i+1]=a[i]; Push 40 from pos 2 to 3
a[pos]=t; Insert 30 in pos 2

}
int main() { cout<<"\nEnter the element to be inserted : ";

int a[10],n,t,pos; cin>>t;


cout<<"Enter the number of elements : "; cout<<"\nEnter the position to insert : ";
cin>>n; cin>>pos;
read(a,n); insert(a,n,t,pos);
print(a,n); n++;
print(a,n);
}
3. Deleting an Element from an Array
void del(int a[10], int n, int t)

{ 0 1 2 3 4
int i;

for (i=0;i<n;i++) 10 20 30 40 50
if (a[i]==t)

break;
t = 30
for (i;i<n;i++)
10 == 30 => false
a[i] = a[i+1]; 20 == 30 => false
} 30 == 30 => true
int main() { cout<<"\nEnter the element to be
deleted : ";
int a[10],n,t,pos;
cin>>t;
cout<<"Enter the number of elements : ";
del(a,n,t);
cin>>n;
n--;
read(a,n);
print(a,n);
print(a,n);
4. Searching an element in an array
int search(int a[10], int n, int t)
{
int i;
for (i=0;i<n;i++)
if (a[i]==t)
break;
if (i<n)
return 1;
else
return 0;
}
int main() { cout<<"\nEnter the element to be
searched : ";
int a[10],n,found,t;
cin>>t;
cout<<"Enter the number of elements : ";
found=search(a,n,t);
cin>>n;
if (found==1)
read(a,n);
cout<<t<<" is found in the array";
print(a,n);
else

cout<<t<<" is not found in the array"; }


5. Sorting an Array
void sort(int a[10],int n) int main() {
{ int i,j,temp; int a[10],n;
for (i=0;i<n-1;i++) cout<<"Enter the number of elements : ";
for(j=i+1;j<n;j++) cin>>n;
if (a[i]>a[j]) read(a,n);
{ temp=a[i]; print(a,n);
a[i]=a[j]; sort(a,n);
a[j]=temp; } print(a,n);
}
4, 3, 5, 1, 7, 6, 2 1 , 4 : 5, 3, 7, 6, 2
1, 3 : 5, 4, 7, 6, 2
4 : 3, 5, 1, 7, 6, 2
1, 2 : 5, 4, 7, 6, 3
3 : 4, 5, 1, 7, 6, 2 -------------------------
1 : 4, 5, 3, 7, 6, 2 1, 2, 5 : 4, 7, 6, 3
1, 2, 4 : 5, 7, 6, 3
-----------------------------
1, 2, 3 : 5, 7, 6, 4
------------------------------
1, 2, 3, 5 : 7, 6, 4 1, 2, 3, 4, 5 ,7 : 6
1, 2, 3, 4 : 7, 6, 5 1, 2, 3, 4, 5, 6, 7
----------------------
1, 2, 3, 4, 7 : 6, 5
1, 2, 3, 4, 6 : 7, 5
1, 2, 3, 4, 5 : 7, 6 0, 1, 2, 3, 4, …., n-2, n-1
----------------------
for (i=0; i<n-1; i++) 0 : 1 to n-1
for (j=i+1;j<n;j++)
1 : 2 to n-1
if (a[i]<a[j])
{ 2 : 3 to n-1
t=a[i];
a[i]=a[j];
a[j]=t;
} i : i+1 to n-1
Thank you!

You might also like