0% found this document useful (0 votes)
209 views20 pages

II PUC CS Lab Manual - Student - 240613 - 005416

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views20 pages

II PUC CS Lab Manual - Student - 240613 - 005416

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

II PUC Lab Manual

1. Write a C++ program to find the frequency of presence an element in an array.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class frequency
{
private:
int i,n,m[100],ele,freq;
public:
void getdata();
void findfreq();
void display();
};
void frequency:: getdata()
{
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements of thearray:"<<endl;
for(int i=0;i<n;i++)
cin>>m[i];
cout<<"enter the search element:"<<endl;
cin>>ele;
}
void frequency:: findfreq()
{
freq=0;
for(int i=0;i<n;i++)
if(m[i]==ele)
freq++;
}
void frequency:: display()
{
if(freq>0)
cout<<"Frequency of the search element"<< ele<<
“is"<<freq<<endl;
else
cout<<"Element does not exist"<<endl;
}

void main()
{

Page 1
II PUC Lab Manual

clrscr();
frequency F;
F.getdata();
F.findfreq();
F.display();
getch();
}

Page 2
II PUC Lab Manual

2. Write a C++ program to insert an element into an array at a given position.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
class insertion
{
private:
int i,n,m[100],p,ele;
public:
void getdata();
void insert();
void display();
};

void insertion::getdata()
{
cout<<"Enter the number of elements"<<endl;
cin>>n;

cout<<"Enter the elements of the array"<<endl;


for(int i=0;i<n;i++)
cin>>m[i];

cout<<"Enter the element to be inserted:"<<endl;


cin>>ele;

cout<<"Enter the position (0 to "<<n<<"):"<<endl;


cin>>p;
}

void insertion::insert()
{
if(p>n)
{
cout<<"Invalid position"<<endl;
exit(0);
}

else
{
for(int i=n-1;i>=p;i--)
m[i+1]=m[i];
m[p] = ele;
n=n+1;
cout<<ele<<setw(3)<<" is successfully inserted at"<<setw(3)<<p<<endl;
}
}

Page 3
II PUC Lab Manual

void insertion::display()
{
cout<<"\nThe array after insertion is"<<endl;
for(int i=0;i<n;i++)
cout<<setw(4)<<m[i];
}

void main()
{
clrscr();
insertion I;
I.getdata();
I.insert();
I.display();
getch();
}

-----------INPUT------------ ---------------------OUTPUT-------------

Page 4
II PUC Lab Manual

3.Write a C++ program to delete an element from an array from a given position.

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
class deletion
{
private:
int m[100],i,n,ele,p;
public:
void getdata();
void remove();
void display();
};
void deletion::getdata()
{
cout<<"Enter the number of elements"<<endl;
cin>>n;
cout<<"Enter the elements of the array"<<endl;
for(int i=0;i<n;i++)
cin>>m[i];
cout<<"Enter the position (0 to "<<n-1<<"):"<<endl;
cin>>p;
}
void deletion::remove()
{
if(p>n-1)
{
cout<<p<<"is an invalid position"<<endl;
exit(0);
}
else
{
ele=m[p];
for(int i=p;i<n;i++)
m[i]=m[i+1];
n=n-1;
}
cout<<"Element at"<<p<<"is successfully removed"<<endl;
}

void deletion::display()
{
cout<<"the array after deletion is"<<endl;
for(int i=0;i<n;i++)
cout<<m[i];
}

Page 5
II PUC Lab Manual

void main()
{
clrscr();
deletion D;
D.getdata();
D.remove();
D.display();
getch();
}
Output:
1)

2)

Page 6
II PUC Lab Manual

4) Write a C++ program to sort the elements of an array in ascending order using Insertion
sort.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class sorting
{
private:
int m[100],n,i;
public:
void getdata();
void sort();
void display();
};
void sorting:: getdata()
{
cout<<"Enter the number of elements"<<endl;
cin>>n;
cout<<"Enter th elements of the array"<<endl;
for(int i=0;i<n;i++)
cin>>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--;
}
}
}

Page 7
II PUC Lab Manual

void sorting::display()
{
cout<<"the array after sorting is"<<endl;
for(int i=0;i<n;i++)
cout<<setw(4)<<m[i];
}

void main()
{
sorting S;
clrscr();
S.getdata();
S.sort();
S.display();
getch();
}

Output:

Page 8
II PUC Lab Manual

5) Write a C++ program to search for a given element in an array using Binary
search method.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class Binary
{
private:
int m[100],n,ele,loc;
public:
void getdata();
void putdata();
void search();
};
void Binary::getdata()
{
cout<<"enter the size of an array\n";
cin>>n;
cout<<"enter the elements in sorted order\n";
for(int i=0;i<n;i++)
cin>>m[i];
cout<<"enter the element to be searched\n";
cin>>ele;
}
void Binary::putdata()
{
if(loc>=0)
cout<<"searched element is found at position "<<loc<<endl;
else
cout<<"unsuccessful search\n";
}
void Binary::search()
{
int beg,end,mid;
loc=-1;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(ele==m[mid])

Page 9
II PUC Lab Manual

{
loc=mid;
break;

}
else
if(ele<m[mid])
end=mid-1;
else
beg=mid+1;
}
}
void main()
{
Binary B;
clrscr();
B.getdata();
B.search();
B.putdata();
getch();
}

Output:
1)

2)

Page 10
II PUC Lab Manual

6) Write a C++ program to create a class with data member’s Principle, Time
and Rate. Create a member functions to accept data values to compute
simple interest and to display the result.

#include<iostream.h>
#include<conio.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<<"\nPrinciple: "<<p;
cout<<"\nTime: "<<t;
cout<<"\nRate: "<<r;
cout<<"\nSimple Interest is: "<<si;

}
void interest::compute()
{
si=(p*t*r)/100;
}
void main()
{
interest I;
clrscr();
I.getdata();
I.compute();
I.putdata();
getch();
}

Page 11
II PUC Lab Manual

7) Write a C++ program to create a class with data members a, b, c and


member functions to input data, compute the discriminate based on the
following condition and print the roots.
• If determinant=0, print the roots that are equal.
• If the discriminant is >0, print the real roots.
• If the discriminant<0, print that the roots are imaginary.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
class quadratic
{
private:
double a,b,c,r1,r2,d;
public:
void getdata();
void compute_det();
void putdata();
};
void quadratic::getdata()
{
cout<<"Enter the co-efficients: ";
cin>>a>>b>>c;
}
void quadratic::compute_det()
{
d=b*b-4*a*c;
if(d==0)
{
cout<<"roots are equal:"<<endl;
r1=(-b-sqrt(d))/(2*a);
r2=r1;
}
else
if(d>0)
{
cout<<"roots are positive and real:"<<endl;
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
}

Page 12
II PUC Lab Manual

else
{
cout<<"roots are imaginary";
exit(0);

}
}
void quadratic::putdata()
{
cout<<"First root ="<<r1<<endl;
cout<<"Second root = "<<r2;
}
void main()
{
quadratic o;
clrscr();
o.getdata();
o.compute_det();
o.putdata();
getch();
}
output :
1)

2)

3)

Page 13
II PUC Lab Manual

8) Write a C++ program to find the area of a square/ rectangle/triangle using


function overloading.

#include<process.h>
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<conio.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()
{
clrscr();
double x, y,z;
int ans;
funoverload f1;
cout<<"enter your choice:\n 1 - square \n 2 - rectangle \n 3 - triangle
\n";
cin>>ans;
if(ans==1)
{
cout<<"Enter the side : ";
cin>>x;
cout<<"Area of the square = "<< f1.area(x)<<endl;
}

else
if(ans==2)

Page 14
II PUC Lab Manual

{
cout<<"Enter two sides: ";
cin>>x>>y;
cout<<"Area of the rectange = "<<f1.area(x,y)<<endl;
}

else

if (ans==3)
{
cout<<"Enter three sides: ";
cin>>x>>y>>z;
cout<<setprecision(8);
cout<<"area of a triangle = "<<f1.area(x,y,z)<<endl;
}
else
cout<<"Invalid input";
getch();
}
output:
1)

Page 15
II PUC Lab Manual

9) Write a C++ program to find the cube of a number using inline functions.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class assign
{
private:
int n;
public:
assign(int nn)
{
n= nn;
}
int cube();
};
inline int assign::cube()
{
return(n*n*n);
}
void main()
{
int n;
clrscr();
cout<<"enter the number:";
cin>>n;
assign N = n;
cout<<"cube of"<<n<<"="<<N.cube();
getch();
}

Output:
1)

2)

Page 16
II PUC Lab Manual

10) Write a C++ program to find the sum of series 1+x+x 2+………………...+xn using
constructors.

#include<iostream.h>
#include<conio.h>
class copy
{
private:
int x,n;

public:
int calculate();
copy(int xx, int nn)
{
x=xx;
n=nn;
}

};
int copy::calculate()
{
int nextterm;
int sum=1;
nextterm=x;
for (int i=1;i<=n;i++)
{
sum=sum+ nextterm;
nextterm=nextterm *x;
}
return sum;
}
void main()
{
int n,x;
clrscr();
cout<<"Enter the base and the power(x and n): ";
cin>>x>>n;
copy obj(x,n);
copy cpy =obj;
cout<<"Object1= Sum of the series: " <<obj.calculate()<<endl;
cout<<"Object2= Sum of the series: " <<cpy.calculate()<<endl;
getch();
}

Page 17
II PUC Lab Manual

11) Create a base class containing the data members 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>
class student
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<"Enter roll no. and name "<<endl;
cin>> rollno>>name;
}
void display()
{
cout<<"Roll no: "<< 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;
}
};
Page 18
II PUC Lab Manual

void main()
{
marks ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}
Output:
1)

2)

Page 19
II PUC Lab Manual

12) Create a class containing the following data members register number, name and
fees. Also create a member function to read and display the data using the
concept of pointers to object.

#include<iostream.h>
#include<conio.h>
class student
{
private :
int regno;
char name[20];
float fees;
public :
void get();
void display();
};

void student::get()
{
cout<<"Enter the student register number: ";
cin>>regno;
cout<<"Enter the student name: ";
cin>>name;
cout<<"Enter the student fee: ";
cin>>fees;
}

void student::display()
{

cout<<"\nStudent details: "<<endl;


cout<<"Student register number: "<<regno<<endl;
cout<<"Student name: "<<name<<endl;
cout<<"Student fees: "<<fees;
}
void main()
{
student s, *sp;
clrscr();
sp=&s;
sp->get();
sp->display();
getch();
}
Page 20

You might also like