PU2 LAB PROGRAMS:
TURBO C++:
1)Write a program to find the frequency of presence an element in an array
#include <iostream.h>
#include <conio.h>
class Frequency
{
private:
int a[100], n, ele,freq; //Data member
public:
void getdata( );
void findfreq( ); //Member functions declaration
void display( );
};
void Frequency:: getdata( ) //Member function definition
{
cout<<"Enter the size of the array:";
cin>>n;
cout<<"Enter the"<<n<<" array elements:" ;
for(int i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the search element";
cin>>ele;
}
void Frequency::findfreq( ) //Member function definition
{
freq=0;
for(int i=0; i<n; i++)
if(ele == a[i]) //Traversing Operation
freq++;
}
void Frequency::display( ) //Member function definition
{
if(freq> 0)
cout<<"Frequency of "<<ele<<" is "<<freq ;
else
cout<<ele<<" Does Not Exists";
}
void main() //Main function
{
Frequency f; //object creation
clrscr() ;
[Link]( );
[Link]( );
[Link]( );
getch( );
OUTPUT:
Enter the size of the array: 5
Enter the 3 array elements: 10 23 12 23 50
Enter the search element23
Frequency of 23 is 2
Enter the size of the array: 5
Enter the 3 array elements: 10 23 12 23 50
Enter the search element 78
78 does not exist
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<<"How many elements? "<<endl;
cin>>n;
cout<<"Enter the elements for the array"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the position (0 to "<<n<<"):"<<endl;
cin>>pos;
cout<<"Enter the element to be inserted"<<endl;
cin>>ele;
}
void Insertion::insert( )
{
if(pos>n)
{
cout<<p<<" is an invalid position!!!";
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++; // Size of the array is incremented by 1
cout<<ele<<" is succesfully inserted"<<endl;
}
}
void Insertion::display( )
{
cout<<"Array elements after insertion are:"<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
}
int main( )
{
Insertion i;
[Link]( );
[Link]( );
[Link]( );
getch( );
return 0;
}
OUTPUT:
How many elements?
5
Enter the elements for the array
20 30 40 50 60
Enter the position (0 to 5):
0
Enter the element to be inserted
10
10 is succesfully inserted
Array elements after insertion are:
10 20 30 40 50 60
How many elements?
5
Enter the elements for the array
20 30 40 50 60
Enter the position (0 to 5):
3
Enter the element to be inserted
70
70 is succesfully inserted
Array elements after insertion are:
20 30 40 70 50 60
How many elements?
3
Enter the elements for the array
12 34 56
Enter the position (0 to 3):
8
Enter the element to be inserted
1
8 is an invalid position!!!
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>
#include<iomanip.h>
class Deletion
{
private:
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 position (0 to "<<n-1<<"): ";
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 array after deletion is ";
for(int i=0; i<n; i++)
cout<<setw(4)<<m[i];
}
void main()
{
Deletion D;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
OUTPUT:
How many elements?
5
Enter the elements for the array
20 30 40 50 60
Enter the position (0 to 5):
3
50 is succesfully removed
The array after deletion is 20 30 40 60
4)Write a C++ program to sort the elements of an array in ascending order using
insertion sort.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class Sorting
{
private:
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++) //1= the pass number
{
j = i ; // j is the comparison position
while( j >= 1 )
{
if(m[j]<m[j-1]) //m[j]=comparison element
{
temp = m[j];
m[j] = m[j-1];
m[j-1] = temp;
}
j--;
}
}
void main()
{
Sorting S;
clrscr();
[Link]();
[Link]();
[Link]();
getch();
}
5}Write a program to search for a given element in an array using Binary
search method
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class Search
{
private:
int m[100], n, ele, loc, beg,end,mid;
public:
void getdata( );
void search( );
void display( );
};
void Search::getdata( )
{
cout<<"Enter the size of the array:"<<endl;
cin>>n;
cout<<"Enter the array elements in sorted order:"<<endl;
for(int i=0;i<n;i++)
cin>>m[i];
cout<<"Enter the element to search:"<<endl;
cin>>ele;
}
void Search::search( )
{
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 == m[mid]) // Element found at mid
{
loc = mid;
break;
}
else
if(ele < m[mid])
end = mid-1;
else
beg = mid+1;
}
}
void Search::display( )
{
if(loc>=0 )
cout<<ele<<" Found at Location:"<<loc;
else
cout<<ele<<" does not exist!!!";
}
void main( )
{
Search s;
clrscr( );
[Link]( );
[Link]( );
[Link]( );
getch( );
}
Output:1
Enter the size of array: 5
Enter the array elements in sorted order: 10 20 30 40 50
Enter the element to search: 40
40 found at location:3
Output:2
Enter the size of array: 5
Enter the array elements in sorted order: 10 20 30 40 50
Enter the element to search: 60
60 does not exist!!!
6}Write a C++ program to create a class with data members principal, time and
[Link] a member function to accept data values, to compute simple interest and
to display the result.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<stdlib.h>
class interest
{
private:
double p, t, r, si;
public:
void getdata();
void compute();
void putdata();
};
void interest::getdata()
{
cout<<"enter principle amount, time, and rate"<<endl;
cin>>p>>t>>r;
}
void interest::putdata()
{
cout<<"principle: "<<p<<endl;
cout<<"time: "<<t<<endl;
cout<<"rate: "<<r<<endl;
cout<<"simple interest: "<<si<<endl;
}
void interest::compute()
{
si=(p*t*r)/100;
}
void main()
{
interest I;
[Link]();
[Link]();
[Link]();
}
OUTPUT:
Enter principle amount, time and rate: 5000 5.5 8.75
Principle: 5000
Time: 5.5
Rate:8.75
simple interest: 2406.25
7}Write a C++ program to create a class with data members a, b, c and member
functions to input data, compute the discriminates based on the following conditions
and print the roots.
If discriminates = 0, print the roots are equal and their value.
If discriminates > 0, print the real roots and their values.
If discriminates < 0, print the roots are imaginary and exit the program.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class quadratic
{
private:
double a, b, c, r1, r2;
public:
void getdata();
void roots();
void display();
};
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::display()
{
cout<<"First root="<<r1<<endl;
cout<<"second root="<<r2;
}
void main()
{
quadratic Q;
[Link]();
[Link]();
[Link]();
getch();
}
OUTPUT:1
Enter the co-efficients: 1 2 1
Roots are equal
First root =-1
Second root=-1
OUTPUT:2
Enter the co-efficients: 2 -3 1
Roots are positive and different.
First root = 1
Second root= 0.5
OUTPUT:3
Enter the co-efficients: 2 -2 1
Roots are imaginary
8} Write a C++ program to find the area of square/ rectangle/ triangle using
function
overloading.
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class funoverload
{
float s;
public:
double area(double a)
{
return a*a;
}
double area(double l, double b)
{
return l*b;
}
double area(double a, double b, double c)
{
s=(a+b+c)/2.0;
return(sqrt(s*(s-a)*(s-b)*(s-c)));
}
};
void main()
{
double x, y, z;
int ans;
funoverload f1;
cout<<" enter the number of sides(1, 2 and 3): ";
cin>>ans;
if(ans == 1)
{
cout<<"Enter the side: ";
cin>>x;
cout<<"area of the square ="<<[Link](x)<<endl;
}
else
if(ans ==2)
{
cout<<"Enter two sides: ";
cin>>x>>y;
cout<<"area of the rectangle="<<[Link](x,y)<<endl;
}
else
if(ans == 3)
{
cout<<"enter three sides; ";
cin>>x>>y>>z;
cout<<setprecision(8);
cout<<"area of a triangle="<<[Link](x,y,z)<<endl;
}
else
cout<<"invalid input";
getch();
}
OUTPUT:1
Enter the number of sides (1,2 and 3): 1
Enter the side: 4.5
Area of square =20.25
Enter the number of sides (1,2 and 3): 2
Enter the side: 4.5 2.75
Area of square =12.375
Enter the number of sides (1,2 and 3): 3
Enter the side: 2 3.5 2.75
Area of square =2.74462
Enter the number of sides (1,2 and 3): 4
Invalid input
9}Write a C++ program to find cube of a number using inline function.
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class assign
{
private:
int n;
public:
assign(int nn)
{
n=nn;
}
int cube();
};
inline int assign::cube()
{
return(n*n*n);
}
void main()
{
clrscr();
int n;
cout<<"enter the number: ";
cin>>n;
assign N=n;
cout<<"cube of "<<n<<"="<<[Link]();
getch();
}
OUTPUT:
Enter a number: -5
Cube of -5 is =-125
Enter a number: 0
Cube of -5 is =0
Enter a number: 10
Cube of -5 is =1000
10} Write a C++ program to find sum of the series 1 + x + x2 + x3 + ….xn using
constructors.
#include<iostream.h>
#include<conio.h>
#include<math.h>
class copy
{
int var, sum, term;
public:
double calculate();
copy(int temp, int x)
{
var = temp;
term = x;
}
};
double copy::calculate()
{
int p;
sum = 1;
p = term;
for(int i=1; i<=var; i++)
{
sum = sum+p;
p=p*term;
}
return sum;
}
void main()
{
int n, t;
clrscr();
cout<<"enter the base and th power(x and n):";
cin>>t>>n;
copy obj(n,t);
copy copy = obj;
cout<<"sum of the series is "<<[Link]();
getch();
}
OUTPUT:
Enter the base and the power (x and n): 2 5
Sum of series is 63
11} Create a base class containing the data member roll number and name. Also
create a
member function to read and display the data using the concept of single level
inheritance. Create a derived class that contains marks of two subjects and total
marks
as the data members.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
class student
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<"enter the name:";
[Link](name,20);
cout<<"enter rollno:";
cin>>rollno;
}
void display()
{
cout<<"rollno:"<<rollno<<endl;
cout<<"name:"<<name<<endl;
}
};
class marks: public student
{
private:
int m1;
int m2;
int total;
public:
void read1()
{
cout<<"Enter two subject marks:";
cin>>m1>>m2;
total=m1+m2;
}
void display1()
{
cout<<"subject1="<<m1<<endl;
cout<<"subject2="<<m2<<endl;
cout<<"total marks="<<total<<endl;
}
};
int main()
{
marks ob;
clrscr();
[Link]();
ob.read1();
[Link]();
ob.display1();
getch();
return 0;
}
OUTPUT:
Enter the name:Rajat
Enter Roll No:101
Enter two subjects marks:95 98
Roll no:101
Name:Rajat
Subject1= 95
Subject2= 98
Total marks =193