Lab01 Array
Lab01 Array
01
Introduction
Array is a container which can hold a fix number of items and these items should be of the
same type. Most of the data structures make use of arrays to implement their algorithms.
Following are the important terms to understand the concept of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a numerical index, which is used
to identify the element.
Array Representation
For example,
int x[6];
Here,
int - type of element to be stored
x - name of the array
6 - size of the arrayve illustration, following are the important points to be considered.
Basic Operations
Following are the basic operations supported by an array.
Traversal: Process each element exactly once in array list.
Search: Search location of element of an array list.
Insertion: Add new element in an array list.
Deletion: Remove element in an array list.
Sorting: Arrange elements in an array list.
Tasks:
1. Run the programs 1, 2 and 3 and print the outputs in the space provided.
Program 1 (Traversal):
#include <iostream>
using namespace std;
int main()
{
int array[] = {4,3,2};
int n = 0; // traverse through array
while (n<3)
{
cout << array[n];
n++;
}}
Output:
Program 2 (Insertion):
#include<iostream>
using namespace std;
int main()
{ int i,a[5],no,pos;
cout<<"Enter data in Array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in Array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter position to insert number: ";
cin>>pos;
if(pos>5)
{ cout<<"\n\nThis is out of range"; }
else
{ cout<<"\n\nEnter new number: ";
cin>>no;
--pos;
for(i=5;i>=pos;i--)
{ a[i+1]=a[i]; }
a[pos]=no;
cout<<"\n\nNew data in Array: ";
for(i=0;i<6;i++)
{ cout<<a[i]; } }
}
Output:
Program 3 (Deletion):
#include<iostream>
using namespace std;
int main()
{ int i,a[5],no,pos;
cout<<"Enter data in array: ";
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"\n\nStored Data in array: ";
for(i=0;i<5;i++)
{
cout<<a[i];
}
cout<<"\n\nEnter poss. of element to delete: ";
cin>>pos;
if(pos>5)
{
cout<<"\n\nThis value is out of range: ";
}
else
{
--pos;
for(i=pos;i<=4;i++)
{
a[i]=a[i+1];
}
cout<<"\n\nNew data in array: ";
for(i=0;i<4;i++)
{
cout<<a[i];
}}
}
Output:
Searching
1. Linear search
Linear search is a very simple search algorithm. In this type of search, a sequential search is made
over all items one by one. Every item is checked and if a match is found then that particular item is
returned, otherwise the search continues till the end of the data collection.
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 8: Exit
Pseudocode
procedure linear_search (list, value)
end if
end for
end procedure
Binary Search:
Binary Search is applied on the sorted array or list of large size. It's time complexity of O(log n)
makes it very fast as compared to other sorting algorithms. The only limitation is that the array or list
of elements must be sorted for the binary search algorithm to work on it.
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
if A[midPoint] < x
if A[midPoint] > x
if A[midPoint] = x
end while
end procedure .
EXERCISE
Develop a C++ Program using the provided Linear Search and Binary Search algorithms for
Searching an array and print the output.
Code:
Output: