PROGRAM 1: Write a C++ program to find the frequency presence of an element in an array.
#include<iostream.h>
#include<conio.h>
class Frequency
{
private:
int a[10], n, ele, count; //Data member
public:
void readdata( );
void findfreq( ); //Member functions declaration
void display( );
};
void Frequency::readdata( ) //Member function definition
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter the array elements:"<<endl;
for(int i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the element to find the frequency"<<endl;
cin>>ele;
}
void Frequency::findfreq( ) //Member function definition
{
count=0;
for(int i=0; i<n; i++)
if(ele == a[i]) //Traversing Operation
count++;
}
void Frequency::display( ) //Member function definition
{
if(count > 0)
cout<<ele<<" Occurs"<<count<<" Time";
else
cout<<ele<<" Does Not Exists";
}
void main( ) //Main function
{
Frequency f;
clrscr( );
f.readdata( );
f.findfreq( );
f.display( );
getch( );
}
PROGRAM 2: Write a C++ program to insert an element into an array at a given position.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Insertion
{
private:
int a[10], n, pos, ele, i; //Data Members
public:
void readdata( );
void insert( ); // Member Function Declaration
void display( );
};
void Insertion::readdata( )
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the elements for the array"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the position of the element in the array"<<endl;
cin>>pos;
cout<<"Enter the element to be inserted"<<endl;
cin>>ele;
}
void Insertion::insert( )
{
if(pos>n)
{
cout<<"Out of array limits!!!";
getch( );
exit(0);
}
else
{
for(i=n; i>=pos; i--)
a[i+1] = a[i]; // Shift array elements
a[pos] = ele; // Insert the given element
n = n+1;}
}
void Insertion::display( )
{
cout<<"Array elements after insertion are:"<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<"\\t";
}
void main( )
{
Insertion i;
clrscr( );
i.readdata( );
i.insert( );
i.display( );
getch( );
}
PROGRAM 3: Write a C++ program to delete an element from an array from a given position.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Deletion
{
private:
int a[10], n, pos, i;
public:
void readdata( );
void delet( );
void display( );
};
void Deletion::readdata( )
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the elements for the array:"<<endl;
for (i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the position to an delete an element:\\n";
cin>>pos;
}
void Deletion::delet( )
{
if(pos>n)
{
cout<<"Out of array limits...!!!";
getch( );
exit(0);
}
else
{
for(i=pos; i<n; i++)
a[i] = a[i+1]; // Move higher position element
n = n-1; // Reduce size of the array by 1
}
}
void Deletion::display( )
{
cout<<"After deletion the array elements are"<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<"\\t";
}
void main( )
{
Deletion d;
clrscr();
d.readdata( );
d.delet( );
d.display( );
getch( );
}
PROGRAM 4: Write a C++ program to sort the element of an array in ascending order using insertion sort.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Sort
{
private:
int a[10], n, i;
public:
void readdata( );
void insertionsort( );
void display( );
};
void Sort::readdata( )
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter the elements for the array:"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
}
void Sort::insertionsort( )
{
int j, temp;
for(i=1; i<n; i++)
{
j = i;
while(j >= 1)
{
if(a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
j = j-1;
}
}
}
void Sort::display( )
{
for(i=0; i<n; i++)
cout<<a[i]<<"\\t";
cout<<endl;
}
void main( )
{
Sort s;
clrscr();
s.readdata( );
cout<<"Unsorted List......"<<endl;
cout<<"*******************"<<endl;
s.display( );
s.insertionsort( );
cout<<"Sorted List........"<<endl;
cout<<"*******************"<<endl;
s.display( );
getch( );
}
PROGRAM 5: Write a C++ program to search for a given element in an array using binary search method.
#include<iostream.h>
#include<conio.h>
class Search
{
private:
int a[10], n, ele, loc, beg, end, mid, i;
public:
void readdata( );
void bsearch( );
void display( );
};
void Search::readdata( )
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter the array elements in sorted order:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the element to search:"<<endl;
cin>>ele;
}
void Search::bsearch( )
{
loc = -1; // Assume that element does not exist
beg = 0; // First element of the array
end = n-1; // Second element of the array
while(beg <= end)
{
mid = (beg+end)/2;
if(ele == a[mid]) // Element found at mid
{
loc = mid;
break;
}
else
if(ele < a[mid])
end = mid-1;
else
beg = mid+1;
}
}
void Search::display( )
{
if(loc == -1)
cout<<ele<<" Element does not exist...!!!";
else
cout<<ele<<" Found at Location:"<<loc+1;
}
void main( )
{
Search s;
clrscr( );
s.readdata( );
s.bsearch( );
s.display( );
getch( );
}