C++ 12 Programs
C++ 12 Programs
#include<iostream.h>
#include<conio.h>
class frequency
{
private :
int n,m[100],ele,freq;
public:
void getdata();
void findfreq();
void display();
};
void main()
{
clrscr();
frequency F;
F.getdata();
F.findfreq();
F.display();
getch();
}
//Prg02.write a program to insert an element into an array at a given position.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<stdlib.h>
class insertion
{
private:
int n,m[100],ele,pos;
public:
void getdata();
void insert();
void display();
};
void insertion::getdata()
{
cout<<"Enter the size of the array :";
cin>>n;
m[pos]=ele;
n=n+1;
cout<<ele<<" is successfully inserted"<<endl;
}
class deletion
{
int m[100],n,ele,p;
public:
void getdata();
void remove();
void display();
};
void deletion::getdata()
{
cout<<"how many elements?";
cin>>n;
cout<<"enter the elements";
for(int i=0;i<n;i++)
cin>>m[i];
cout<<"enter the index position:";
cin>>p;
}
void deletion::remove()
{
if(p>n-1)
{
cout<<p<<"is an invalid position";
exit(0);
}
ele=m[p];
for(int i=p+1;i<n;i++)
m[i-1]=m[i];
n - -;
cout<<ele<<"is successfully removed"<<endl;
}
void deletion::display()
{
cout<<"the arry after deletion is";
for(int i=0;i<n;i++)
cout<<setw(4)<<m[i];
}
void main()
{
clrscr();
deletion D;
D.getdata();
D.remove();
D.display();
getch();
}
Prg04 Insertion Sort
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class sorting
{
int m[100],n;
public:
void getdata();
void sort();
void display();
};
void sorting::getdata()
{
cout<<"how many elements?";
cin>>n;
cout<<"enter the elements";
for(int i=0;i<n;i++)
cin>>m[i];
}
void sorting::display()
{
cout<<"the array after sorting is";
for(int i=0;i<n;i++)
cout<<setw(4)<<m[i];
}
void sorting::sort()
{
int temp,j;
for(int i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(m[j]<m[j-1])
{
temp=m[j];
m[j]=m[j-1];
m[j-1]=temp;
}
j--;
}
}
}
void main()
{
clrscr();
sorting s;
s.getdata();
s.sort();
s.display();
getch();
}
//Prg 05 write a program to search for a given element in an array using binary search method.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class binarysearch
{
int m[100],n,ele,loc;
public:
void getdata();
void search();
void display();
};
void binarysearch::getdata( )
{
cout<<"How many elements ? ";
cin>>n;
void binarysearch::search()
{
int beg,end,mid;
loc=-1;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(ele==m[mid])
{
loc=mid;
break;
}
if(ele<m[mid])
end=mid-1;
else
beg=mid+1;
}
}
void binarysearch::display() void main()
{ {
if(loc>=0) clrscr();
cout<<"Element located at index position : "<<loc;\ binarysearch B;
else B.getdata();
cout<<"Element not found, Search is unsuccessful"; B.search();
} B.display();
getch();
}
// Prg 06 Simple Interest
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class interest
{
double p,t,r,si;
public:
void getdata();
void calculate();
void display();
};
void interest::getdata()
{
cout<<"enter Principle amount, time and rate :"<<endl;
cin>>p>>t>>r;
}
void interest::calculate()
{
si=(p*t*r)/100;
}
void interest::display()
{
cout<<"the simple interest is "<<si;
}
void main()
{
clrscr();
interest s;
s.getdata();
s.calculate();
s.display();
getch();
}
//Prg07 Quadratic Equation
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
class quadratic
{
double a,b,c,r1,r2;
public:
void getdata();
void roots();
void putdata();
};
void quadratic::getdata()
{
cout<<"enter the co-efficients:";
cin>>a>>b>>c;
}
void quadratic::roots()
{
double d=b*b-4*a*c;
if(d==0)
{
cout<<"roots are equal"<<endl;
r1=-b/(2*a);
r2=r1;
}
else if(d>0)
{
cout<<"roots are positive and different:"<<endl;
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
}
else
{
cout<<"roots are imaginary";
exit(0);
}
}
void quadratic::putdata()
{
cout<<"first root="<<r1<<endl;
cout<<"second root="<<r2<<endl;
}
void main()
{
clrscr();
quadratic Q;
Q.getdata();
Q.roots();
Q.putdata();
getch();
}
//prg 08 Function Overloading
#include<iostream.h> #include<conio.h>
#include<math.h> #include<iomanip.h>
class overloading
{
double s;
public:
double area(double side)
{
return side*side;
}
void main()
{
clrscr();
overloading f;
double x,y,z;
int ch;
cout<<"enter how many inputs (1/2/3) : ";
cin>>ch;
if(ch==1)
{
cout<<"enter the side :";
cin>>x;
cout<<"Area of the square = "<<f.area(x);
}
else if(ch==2)
{
cout<<"enter two sides : ";
cin>>x>>y;
cout<<"Area of the rectangle = "<<f.area(x,y);
}
else if(ch==3)
{
cout<<"enter Three sides : ";
cin>>x>>y>>z;
cout<<setprecision(5);
cout<<"Area of a triangle ="<<f.area(x,y,z);
}
else
{ cout<<"invalid choice";
}
getch();
}
//Prg 09 Inline Function
#include<iostream.h>
#include<conio.h>
class assign
{
int n;
public:
assign(int value)
{
n=value;
}
int cube();
};
inline assign::cube()
{
return(n*n*n);
}
void main()
{
clrscr();
int num;
cout<<"enter the number ";
cin>>num;
assign obj=num;
cout<<"cube of "<<num<<" is "<<obj.cube();
getch();
}
//Prg10 Sum of Series
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class copy
{
int base,term,sum;
public:
double calculate();
copy (int b,int t)
{
base=b;
term=t;
}
};
double copy::calculate()
{
int p;
sum=1;
p=base;
for(int i=1;i<=term;i++)
{
sum=sum+p;
p=p*base;
}
return sum;
}
void main()
{
int b,pow;
clrscr();
cout<<"enter the base and the power :";
cin>>b>>pow;
copy obj(b,pow);
copy cpy=obj;
cout<<endl<<"sum of the series is"<<obj.calculate();
cout<<endl<<"sum of the series is"<<cpy.calculate();
getch();
}
//Prg11, Inheritance
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class student
{
int rollno;
char name[20];
public:
void read()
{
cout<<endl<<"enter the name : ";
cin.getline(name,20);
void display()
{
cout<<endl<<"rollno : "<<rollno;
cout<<endl<<"name : "<<name;
}
};
void display1()
{
cout<<endl<<"subject1 = "<<m1;
cout<<endl<<"subject2 = "<<m2;
cout<<endl<<"total marks = "<<total;
}
};
void main()
{
clrscr();
marks ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}
// Prg12, Pointer Implimentation
#include<iostream.h>
#include<conio.h>
class student
{
int regno;
char name[20];
float fees;
public:
void get();
void display();
};
void student::get()
{
cout<<"enter student name : ";
cin.getline(name,20);
void student::display()
{
cout<<endl<<"student register number : "<<regno;
cout<<endl<<"student name : "<<name;
cout<<endl<<"student fees : "<<fees;
}
void main()
{
clrscr();
student s;
student *sp=&s;
sp->get();
sp->display();
getch();
}