You are on page 1of 5

C++ program 1

write a c++ program to find the frequency of presence of an element in an array

#include<iostream.h>
#include<conio.h>

class freq
{
private:
int n,a[10],i,ele,c;
public:
void read( );
void logic( );
};

void freq:: read( )


{
cout<<" enter the size of the array \n";
cin>>n;
cout<<"\n enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n enter the search element \n";
cin>>ele;
}
void freq::logic( )
{
c=0;
for(i=0;i<n;i++)
if(a[i]==ele)
c++;
cout<<"\n frequency of "<<ele<<" is "<<c;
}

void main( )
{
freq f;
clrscr( );
f.read( );
f.logic( );
getch( );
}
C++ program 2

write a c++ program to insert an element into an array at a given position

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class insert
{
private:
int n,a[10],i,ele,pos;
public:
void read( );
void logic( );
void print( );
};
void insert:: read( )
{
cout<<" enter the size of the array \n";
cin>>n;
cout<<"\n enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n enter the element to be inserted \n";
cin>>ele;
cout<<"\n enter the position \n";
cin>>pos;
}
void insert::logic( )
{
if(pos>=n)
{
cout<<”\n invalid position \n”;
getch();
exit(0);
}
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=ele;
n++;
}
void insert :: print( )
{
cout<<"\n the array after insertion \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main( )
{
insert i;
clrscr( );
i.read( );
i.logic( );
i.print( );
getch( );
}
C++ program-3

write a c++ program to delete an element from an array from a given position

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class del
{
private:
int n,a[10],i,pos;
public:
void read( );
void logic( );
void print( );
};
void del:: read( )
{
cout<<" enter the size of the array \n";
cin>>n;
cout<<"\n enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n enter the element position to be deleted \n";
cin>>pos;
}
void del::logic( )
{
if(pos>=n)
{
cout<<"\n invalid position";
getch( );
exit(0);
}
cout<<"\n the deleted element is :"<<a[pos];
for(i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
}
void del :: print( )
{
cout<<"\n the array after deletion \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}

void main( )
{
del d;
clrscr( );
d.read( );
d.logic( );
d.print( );
getch( );
}
C++ program-4
write a c++ program to sort the elements of an array in ascending order using insertion
sort

#include<iostream.h>
#include<conio.h>
class sort
{
private:
int n,a[10],i,j,t;
public:
void read( );
void logic( );
void print( );
};
void sort:: read( )
{
cout<<" enter the size of the array \n";
cin>>n;
cout<<"\n enter the array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
}
void sort::logic( )
{

for(i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(a[j-1]>a[j])
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
j--;
}
}
}
void sort :: print( )
{
cout<<"\n the sorted array is \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void main( )
{
sort s;
clrscr( );
s.read( );
s.logic( );
s.print( );
getch( );
}
C++ 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 binary
{
private:
int n,a[10],i,ele,pos,low,high,mid;
public:
void read( );
void logic( );
void print( );
};
void binary:: read( )
{
cout<<" enter the size of the array \n";
cin>>n;
cout<<"\n enter the sorted array elements \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n enter the element to be searched\n";
cin>>ele;
}
void binary::logic( )
{
pos=-1;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(ele==a[mid])
{
pos=mid;
break;
}
if(a[mid]>ele)
high=mid-1;
else
low=mid+1;
}
}
void binary :: print( )
{
if(pos>=0)
cout<<ele<<" found at location = " <<pos;
else
cout<<ele<<" not found " ;
}
void main( )
{
binary s;
clrscr( );
s.read( );
s.logic( );
s.print( );
getch( );
}

You might also like