0% found this document useful (0 votes)
393 views28 pages

2nd PU CS Lab Manual

The document is a lab manual containing 10 programs written in C++. Each program demonstrates a different concept: 1) finding frequency of elements in an array, 2) inserting an element into an array, 3) deleting an element from an array, 4) sorting an array using insertion sort, 5) searching an array using binary search, 6) creating a class to calculate simple interest, 7) creating a class to find roots of a quadratic equation, 8) function overloading to find area of shapes, 9) using an inline function to find the cube of a number, 10) using constructors to find the sum of a series. For each program, the code and sample output are provided.

Uploaded by

Abdus Samad
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)
393 views28 pages

2nd PU CS Lab Manual

The document is a lab manual containing 10 programs written in C++. Each program demonstrates a different concept: 1) finding frequency of elements in an array, 2) inserting an element into an array, 3) deleting an element from an array, 4) sorting an array using insertion sort, 5) searching an array using binary search, 6) creating a class to calculate simple interest, 7) creating a class to find roots of a quadratic equation, 8) function overloading to find area of shapes, 9) using an inline function to find the cube of a number, 10) using constructors to find the sum of a series. For each program, the code and sample output are provided.

Uploaded by

Abdus Samad
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/ 28

Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 1: Write a program to find the frequency of presence of an


element in an array.

Code:

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

int main()
{
clrscr();
int arr[]={1,9,2,8,2,8,1,9};
int n= sizeof(arr)/sizeof(arr[0]);
int visited[10];

for(int i=0;i<n;i++)
{
if(visited[i]!=1)
{
int count=1;
for(int j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count++;
visited[j]=1;
}
}

Compiled By: Husnain Afsar Page 1


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

cout<<arr[i]<<" occurs "<<count<<" times "<<endl;


}
}
getch();
return 0;
}

Output:

Compiled By: Husnain Afsar Page 2


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 2: Write a program to insert an element in an array at a


given position

Code:

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

void main()
{
clrscr();
int a[10],i,n,ele,p;

cout<<" Enter the number of elements: ";


cin>>n;

cout<<"Enter the elements: ";


for(i=0;i<n;i++)
{
cin>>a[i];
}

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


cin>>ele;

cout<<" Enter the position to insert the element (0 to "<<n<<") ";


cin>>p;

if(p>n)
{
cout<<"Invalid position";
}
else
{
for(i=n-1;i>=p; i--)
{
a[i+1]=a[i];
}
a[p]=ele;
n=n+1;

cout<<"The elements after insertion are: ";

Compiled By: Husnain Afsar Page 3


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

for(i=0;i<n;i++)
{
cout<<setw(4)<<a[i];
}
}
getch();
}

Output:

Compiled By: Husnain Afsar Page 4


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 3: Write a program to delete an element from an array from


a given position.

Code:

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

void main()
{
clrscr();
int a[10],i,n,ele,p;

cout<<"Enter the size of array: ";


cin>>n;

cout<<"Enter the array elements: ";


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

cout<<"Enter the position you want to delete (0 to "<<n-1<<"); ";


cin>>p;

if(p > n-1)


cout<<"Invalid Position";
else
{

Compiled By: Husnain Afsar Page 5


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

ele=a[p];
for(i=p;i<n;i++)
a[i]=a[i+1];
n=n-1;
cout<<"The elements after the deletion is ";
for(i=0;i<n;i++)
cout<<setw(4)<<a[i];
}
getch();
}

Output:

Compiled By: Husnain Afsar Page 6


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 4: Write a program to sort the elements of an array in


ascending order using insertion sort.

Code:

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

void main()
{
clrscr();
int a[10],n,i,j,temp;

cout<<"Enter the size of the array: "<<endl;


cin>>n;

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


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

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

Compiled By: Husnain Afsar Page 7


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

a[j-1]=temp;
}
j=j-1;
}
}

cout<<"Elements after insertion in ascending order are: ";


for(i=0;i<n;i++)
cout<<a[i]<<"\t"<<endl;
getch();
}

Output:

Compiled By: Husnain Afsar Page 8


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 5: Write a program to search for a given element in an


array using Binary search method.

Code:

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

void main()
{
int a[10],i,n,m,b,loc,e,pos,ele;
clrscr();
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the search element: ";
cin>>ele;
pos=-1;
b=0;
e=n-1;
while(b<=e)
{
m=(b+e)/2;
if(ele==a[m])
{
pos=m;

Compiled By: Husnain Afsar Page 9


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

break;
}
else
{
if(ele<a[m])
e=m-1;
else
b=m+1;
}
}
if(pos>=0)
cout<<"Position: "<<pos;
else
cout<<"Search is unsuccessful";
getch();
}

Output:

Compiled By: Husnain Afsar Page 10


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 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 display the result.

Code:

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

class SimpleInterest
{
private:
float p,r,t,si;
public:
void readdata();
void compute();
void display();
};

void SimpleInterest:: readdata()


{
cout<<"Enter the Principal, Rate and Time: "<<endl;
cin>>p>>r>>t;
}

void SimpleInterest::compute()
{
si=(p*t*r)/100;
}

Compiled By: Husnain Afsar Page 11


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

void SimpleInterest::display()
{
cout<<"Principal: "<<p<<endl;
cout<<"Rate: "<<r<<endl;
cout<<"Time: "<<t<<endl;
cout<<"Simple Interest is: "<<si<<endl;
}

void main()
{
SimpleInterest si;
clrscr();
si.readdata();
si.compute();
si.display();
getch();
}

Output:

Compiled By: Husnain Afsar Page 12


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 7: Write a program to create 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:
 If determinant=0, print the roots that are equal
 If the discriminant is >0, print the real roots
 If the discriminant is <0, print that the roots are imaginary

Code:

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

class Quadratic
{
private:
int a,b,c;
float disc,x,x1,x2;
public:
void readdata();
void compute();
void display();
};

void Quadratic::readdata()
{
cout<<"Enter the values of a,b,c: "<<endl;
cin>>a>>b>>c;
}

Compiled By: Husnain Afsar Page 13


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

void Quadratic::compute()
{
disc=b*b-4*a*c;
}

void Quadratic::display()
{
compute();
if(disc==0)
{
cout<<"Equal Roots: "<<endl;
x=-b/(2*a);
cout<<"Roots is: "<<x;
}
else if(disc>0)
{
cout<<"Real and Distinct Roots: "<<endl;
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
cout<<"Root1: "<<x1<<endl;
cout<<"Root2: "<<x2<<endl;
}
else
{
cout<<"Imaginary Roots"<<endl;
}
}

Compiled By: Husnain Afsar Page 14


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

void main()
{
Quadratic q;
clrscr();
q.readdata();
q.compute();
q.display();
getch();
}

Output:

Compiled By: Husnain Afsar Page 15


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 8: Program to find the area of a square/rectangle/triangle


using function overloading.

Code:

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

class fnld
{
public:
int area(int a)
{
return a*a;
}

double area(double b, double h)


{
return 0.5*b*h;
}

double area(double w, int h)


{
return w*h;
}
};

void main()

Compiled By: Husnain Afsar Page 16


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

{
clrscr();
fnld f1;
cout<<"Area of square: "<<f1.area(5)<<endl;
cout<<"Area of triangle: "<<f1.area(3.0,5.5)<<endl;
cout<<"Area of rectangle: "<<f1.area(4.5,3)<<endl;
getch();
}

Output:

Compiled By: Husnain Afsar Page 17


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 9: Program to find the cube of a number using inline


function.

Code:

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

inline int cube(int a)


{
return(a*a*a);
}

void main()
{
int x,y;
clrscr();
x=cube(5);
cout<<"Cube of 5 is: "<<x<<endl;
y=cube(10);
cout<<"Cube of 10 is: "<<y<<endl;
getch();
}

Output:

Compiled By: Husnain Afsar Page 18


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 10: Write a program to find the sum of the series 1 + x + x2


+ x3 + ….xn using constructors.

Code:

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

class Series
{
private:
int sum,x,n;
public:
Series(int y, int m)
{
sum=1;
x=y;
n=m;
}
int sumseries();
};

int Series::sumseries()
{
for(int i=1;i<=n;i++)
{
sum=sum+pow(x,i);
}

Compiled By: Husnain Afsar Page 19


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

return sum;
}

void main()
{
int x,n;
clrscr();
cout<<"Enter the base value i.e, X value: "<<endl;
cin>>x;

cout<<"Enter the power value: "<<endl;


cin>>n;

Series s1(x,n);

cout<<"Sum of series using constructors: "<<endl;


cout<<s1.sumseries()<<endl;
getch();
}

Output:

Compiled By: Husnain Afsar Page 20


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 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.

Code:

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

class base
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<"Enter Roll Number and Name"<<endl;
cin>>rollno>>name;
}

void display()
{
cout<<"Roll Number: "<<rollno<<endl;
cout<<"Name: "<<name<<endl;
}
};
class derive: public base

Compiled By: Husnain Afsar Page 21


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

{
private:
int m1,m2,t;
public:
void read1()
{
cout<<"Enter first and second marks: "<<endl;
cin>>m1>>m2;
t=m1+m2;
}

void display1()
{
cout<<"First marks: "<<m1<<endl;
cout<<"Second marks: "<<m2<<endl;
cout<<"Total marks: "<<t<<endl;
}
};

void main()
{
derive ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}

Compiled By: Husnain Afsar Page 22


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Output:

Compiled By: Husnain Afsar Page 23


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 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.

Code:

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

class std
{
private:
int rno;
char name[25];
float fees;

public:
void get();
void display();
};

void std::get()
{
cout<<"Enter the register number: ";
cin>>rno;

cout<<"Enter the name: ";


cin>>name;

Compiled By: Husnain Afsar Page 24


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

cout<<"Enter the fees: ";


cin>>fees;
}

void std::display()
{
cout<<"Register Number: "<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Fees: "<<fees<<endl;
}

void main()
{
std s, *sp;
sp=&s;
clrscr();
sp->get();
sp->display();
getch();
}

Output:

Compiled By: Husnain Afsar Page 25


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

Program 13: Write a program to push items into the stack.


Program 14: Write a program to pop elements from the stack.

Code:

#include<iostream.h>
#include<conio.h>
int s[100], n=100, top=-1;

void push(int val)


{
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++;
s[top]=val;
}
}

void pop()
{
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is: "<<s[top]<<endl;
top--;
}
}

Compiled By: Husnain Afsar Page 26


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

void display()
{
if(top>=0)
{
cout<<"Stack elements are:";
for(int i=top;i>=0;i--)
{
cout<<s[i]<<" ";
}
}
else
{
cout<<"Empty Stack\n";
}
}

int main()
{
clrscr();
int ch,val;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;

do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)

Compiled By: Husnain Afsar Page 27


Lab Manual C++ and Data Structures II PUC, Shaheen PU College, Mysuru

{
case 1: cout<<"Enter the element: ";
cin>>val;
push(val);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: cout<<"EXIT"<<endl;
break;
default: cout<<"INVALID CHOICE"<<endl;
}
}while(ch!=4);
return 0;
}

Output:

Compiled By: Husnain Afsar Page 28

You might also like