0% found this document useful (0 votes)
29 views21 pages

1-12(Lab Programs)

Uploaded by

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

1-12(Lab Programs)

Uploaded by

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

Page |1

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;

public:

void getdata();

void compute();

void display();

};

void frequency :: getdata()

cout<<"How many elements do you want to enter into an array "<<endl;

cin>>n;

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

for(int i=0;i<n;i++)

cin>>a[i];

cout<<"Enter the element for which the frequency has to be found"<<endl;

cin>>ele;

void frequency :: compute()

freq=0;

for(int i=0;i<n;i++)

if(a[i]==ele)
Page |2

freq++;

void frequency :: display()

if(freq>0)

cout<<"Frequency of the element is "<<freq;

else

cout<<"Sorry Buddy,Element doesnt exist"<<endl;

void main()

frequency f;

clrscr();

f.getdata();

f.compute();

f.display();

getch();

Output 1

Output2
Page |3

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

#include<iostream.h>

#include<conio.h>

#include<process.h>

#include<iomanip.h>

class insert

private:

int a[100],ele,pos,n;

public:

void getdata();

void compute();

void display();

};

void insert:: getdata()

cout<<"How many elements do you want to enter into array?"<<endl;

cin>>n;

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

for(int i=0;i<n;i++)

cin>>a[i];

cout<<"Enter the element to be inserted at the given position"<<endl;

cin>>ele;

cout<<"Enter the position into which the element is to be inserted "<<endl;

cin>>pos;

}
Page |4

void insert:: compute()

if(pos>n)

cout<<"Invalid position"<<endl;

getch();

exit(0);

for(int i=n-1;i>=pos;i--)

a[i+1]=a[i];

a[pos]=ele;

n++;

cout<<"Element inserted successfully"<<endl;

void insert:: display()

cout<<"The array elements after insertion are as follows"<<endl;

for(int i=0;i<n;i++)

cout<<a[i]<<setw(4);

void main()

insert i;

clrscr();

i.getdata();
Page |5

i.compute();

i.display();

getch();

Output 1

Output 2

3. Write a 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 a[100],pos,n,ele;

public:

void getdata();

void compute();
Page |6

void display();

};

void deletion:: getdata()

cout<<"How many elements do you want to enter into array"<<endl;

cin>>n;

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

for(int i=0;i<n;i++)

cin>>a[i];

cout<<"Enter the position from which the element has to be deleted"<<endl;

cin>>pos;

void deletion::compute()

if(pos>n-1)

cout<<"Invalid position"<<endl;

getch();

exit(0);

ele=a[pos];

for(int i=pos+1;i<n;i++)

a[i-1]=a[i];

n--;

cout<<"The element "<<ele<<" is removed succesfully"<<endl;

void deletion::display()
Page |7

cout<<"The elements after deletion are as follows"<<endl;

for(int i=0;i<n;i++)

cout<<a[i]<<setw(4);

void main()

deletion d;

clrscr();

d.getdata();

d.compute();

d.display();

getch();

Output 1

Output 2
Page |8

4.Write a 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 a[100],n;
public:
void getdata();
void compute();
void display();
};
void sorting::getdata()
{
cout<<"How many elements do you want to enter into array?"<<endl;
cin>>n;
cout<<"Enter the array elements now "<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void sorting::compute()
{
int i,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--;
}
}
}
void sorting::display()
{
cout<<"The array elements after sorting are as follows"<<endl;
for(int i=0;i<n;i++)
Page |9

{
cout<<a[i]<<setw(4);
}
}
void main()
{
clrscr();
sorting s;
s.getdata();
s.compute();
s.display();
getch();
}

Output

5. Write a program to search for a given element in an array using Binary


search method.
#include<iostream.h>
#include<conio.h>
class Binarysearch
{
private:
int a[100],n,ele,loc;
public:
void getdata();
void compute();
void display();
};
void Binarysearch::getdata()
{
cout<<"How many elements do you want to enter into array"<<endl;
cin>>n;
cout<<"Enter the elements into the array now in ascending order"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element for which the location has to be found"<<endl;
cin>>ele;
}
void Binarysearch::compute()
P a g e | 10

{
int low,high,mid;
low=0;
high=n-1;
loc=-1;
while(low<=high)
{
mid=(low+high)/2;
if(ele==a[mid])
{
loc=mid;
break;
}
else if(ele<a[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
}
void Binarysearch::display()
{
if(loc>=0)
{
cout<<"Location of the element in array is "<<loc;
}
else
{
cout<<"Sorry Buddy,Element doesnt exist"<<endl;
}
}
void main()
{
clrscr();
Binarysearch b;
b.getdata();
b.compute();
b.display();
getch();
}
P a g e | 11

Output 1

Output 2

6. Write a program to create a class with data members principle, time and rate.
Create member functions to accept data values to compute simple interest
and to display the result.

#include<iostream.h>
#include<conio.h>
class Simpleinterest
{
private:
double p,t,r,si;
public:
void getdata();
void compute();
void display();
};
void Simpleinterest:: getdata()
{
cout<<"Enter the value of Principal,Time and Rate"<<endl;
cin>>p>>t>>r;
}
void Simpleinterest:: compute()
{
si=(p*t*r)/100;
}
void Simpleinterest::display()
{
cout<<"Principle="<<p<<endl;
cout<<"Time="<<t<<endl;
cout<<"Rate="<<r<<endl;
cout<<"Simple Interest="<<si<<endl;
}
void main()
{
Simpleinterest s;
clrscr();
P a g e | 12

s.getdata();
s.compute();
s.display();
getch();
}

Output 1

7. Write a program to create a class with data members a, b, c and member functions
to input data, compute the discriminant based on the following conditions and
print the roots.

1. If determinant=0, print the roots that are equal


2. If the discriminant is>0, print the real roots
3. If the discriminant<0, print that the roots are imaginary and exit the
program

#include<iostream.h>

#include<conio.h>

#include<math.h>

#include<process.h>

class Quadratic

private:

double a,b,c,r1,r2;

public:

void getdata();

void compute();

void display();

};

void Quadratic :: getdata()

{
P a g e | 13

cout<<"Enter the co-efficients(a,b and c)"<<endl;

cin>>a>>b>>c;

void Quadratic::compute()

double d;

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 postive and different"<<endl;

r1=(-b+sqrt(d))/(2*a);

r2=(-b-sqrt(d))/(2*a);

else

cout<<"Roots are imaginary"<<endl;

getch();

exit(0);

void Quadratic::display()

cout<<"The value of Root 1 ="<<r1<<endl;

cout<<"The value of Root 2 ="<<r2<<endl;

}
P a g e | 14

void main()

clrscr();

Quadratic q;

q.getdata();

q.compute();

q.display();

getch();

Output 1

Output 2

Output 3

8. Program to find the area of a square/rectangle/triangle using function


overloading.
#include<iostream.h>

#include<conio.h>

#include<math.h>

class functionoverloading

private:
P a g e | 15

double 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;

return sqrt (s*(s-a)*(s-b)*(s-c));

};

void main()

clrscr();

functionoverloading f;

double x,y,z,choice;

cout<<"Press 1 to find area of Square"<<endl;

cout<<"Press 2 to find area of Rectangle"<<endl;

cout<<"Press 3 to find area of Triangle"<<endl;

cin>>choice;

if (choice==1)

cout<<"Enter the sides of square(x)"<<endl;

cin>>x;

cout<<"The area of square is "<<f.area(x);


P a g e | 16

else if(choice==2)

cout<<"Enter length and breadth of Rectangle(x,y)"<<endl;

cin>>x>>y;

cout<<"The area of rectangle is "<<f.area(x,y);

else if(choice==3)

cout<<"Enter the three sides of traingle(x,y,z)"<<endl;

cin>>x>>y>>z;

cout<<"The area of triangle is "<<f.area(x,y,z);

else

cout<<"Invalid input"<<endl;

getch();

Output 1

Output 2
P a g e | 17

Output 3

Output 4

9. Program to find the cube of a number using inline functions.


#include<iostream.h>
#include<conio.h>
inline int cube(int a)
{
return (a*a*a);
}
void main()
{
clrscr();
int x;
cout<<"Dear Student, Enter the number for which the cube has to be found"<<endl;
cin>>x;
cout<<"The cube of a number is "<<cube(x);
getch();
}
Output 1

Output 2

10. Write a program to find the sum of the series 1+ x + x2 + ... + xn using constructors.
P a g e | 18

#include<iostream.h>

#include<conio.h>

#include<math.h>

class sumofseries

private:

int sum,power,base;

public:

sumofseries(int a,int b)

base=a;

power=b;

int compute();

};

int sumofseries::compute()

sum=1;

for(int i=1;i<=power;i++)

sum=sum+pow(base,i);

return sum;

void main()

clrscr();

int x,y;
P a g e | 19

cout<<"Enter the base and power(x and y)"<<endl;

cin>>x>>y;

sumofseries obj1(x,y);

sumofseries obj2=obj1;

cout<<"The sum of series is "<<obj2.compute()<<endl;

getch();

Output

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:
char name[20];
int regno;
public:
void getdata1()
{
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your Register Number"<<endl;
cin>>regno;
}
void putdata1()
{
cout<<"Name="<<name<<endl;
cout<<"Register Number="<<regno<<endl;
}
};
class marks:public student
{
private:
P a g e | 20

double m1,m2,total;
public:
void getdata2()
{
cout<<"Enter the marks of subject1 and subject2"<<endl;
cin>>m1>>m2;
total=m1+m2;
}
void putdata2()
{
cout<<"Subject1 mark="<<m1<<endl;
cout<<"Subject2 mark="<<m2<<endl;
cout<<"Total Marks="<<total<<endl;
}
};
void main()
{
clrscr();
marks m;
m.getdata1();
m.getdata2();
m.putdata1();
m.putdata2();
getch();
}

Output

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

#include<iostream.h>
#include<conio.h>
class pointers
P a g e | 21

{
private:
char name[20];
int regno;
double fees;
public:
void getdata();
void display();
};
void pointers::getdata()
{
cout<<"Enter your name"<<endl;
cin>>name;
cout<<"Enter your Register Number"<<endl;
cin>>regno;
cout<<"Enter the fees amount"<<endl;
cin>>fees;
}
void pointers::display()
{
cout<<"Name="<<name<<endl;
cout<<"Register Number="<<regno<<endl;
cout<<"Fees="<<fees<<endl;
}
void main()
{
clrscr();
pointers p,*sp;
sp=&p;
sp->getdata();
sp->display();
getch();
}

Output

You might also like