2c Lab Manual C++-Final
2c Lab Manual C++-Final
Department of
Master of Computer Applications
A Laboratory Manual
For
2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee
(DA = 52% of Basic and Income Tax = 30% of the gross salary)
3. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an array of 10
STUDENT objects. Using appropriate functions, find the average of the two better marks for each
student. Print the USN, Name and the average marks of all the students.
4. Write a C++ program to create a class called COMPLEX and implement the following overloading
functions ADD that return a complex number:
a. ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex number
b. ADD(s1, s2) – where s1 and s2 are complex numbers
5. Write a C++ program to create a class called LIST (linked list) with member functions to insert an
element at the front as well as to delete an element from the front of the list. Demonstrate all the
functions after creating a list object.
6. Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles.
7. Write a C++ program to create a class called STACK using an array of integers. Implement the
following operations by overloading the operators ‘+’ and ‘-‘:
a. s1 = s1 + element; where s1 is an object of the class STACK and element is an integer to be
pushed on the top of the stack
b. s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.
Handle the STACK empty and full conditions. Also display the contents of the stack after each
operation, by overloading the << operator.
8. Write a C++ program to create a class called DATE. Accept two valid dates in the form dd/mm/yy.
Implement the following operations by overloading the operators ‘+’ and ‘-‘. After every operation
display the results by overloading the operator <<.
a. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integer
b. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer
9. Create a class called MATRIX using two-dimensional array of integers. Implement the following
operations by overloading the operator ++ which checks the compatibility of two matrices to be
added and subtracted. Perform the addition and subtraction by overloading the + and – operators
respectively. Display the results by overloading the operator <<.
10. Write a C++ program to create a class called OCTAL which has the characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded
operator +.
a. OCTAL h = x; where x is an integer.
b. int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator << . Also display the values of h and y.
11. Write a C++ program to create a class called QUEUE with member functions to add an element and
to delete an element from the queue. Using the member functions, implement a queue of integers
and double. Demonstrate the operations by displaying the contents of the queue after every
operation.
12. Write a C++ program to create a class called DLIST (doubly Linked List) with member functions to
insert a node at a specified position and delete a node from a specified position of the list.
Demonstrate the operations by displaying the content of the list after every operation.
13. Write a C++ program to create a class called STUDENT with data members USN, Name and Age.
Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester, Fees
and Stipend. Enter the data for at least 5 students. Find the semester-wise average age for all UG
and PG students separately.
14. Write a C++ program to create a class called STRING and implement the following operations.
Display the results after every operation by overloading the operator <<.
a. STRING s1 = “VTU”
b. STRING s2 = “BELGAUM”
c. STRING s3 = s1 + s2 (Use copy constructor)
15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to
perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate
the traversals.
16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions
convert a given valid Infix expression into postfix form. Display the infix and postfix expressions.
Note: In the examination each student picks one question from a lot of all the 16 questions.
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_id;
char emp_name[20];
float basic,DA,gross,IT,net_sal;
public:
void read_data();
void net_cal();
void write_data()
{
cout<<"Name : "<<emp_name<<endl;
cout<<"ID : "<<emp_id<<endl;
cout<<"Basic : "<<basic<<endl;
cout<<"DA : "<<DA<<endl;
cout<<"Gross : "<<gross<<endl;
cout<<"IT : "<<IT<<endl;
cout<<"Net_salary : "<<net_sal<<endl;
}
};
void employee::read_data()
{
cout<<"enter name : ";
cin>>emp_name;
cout<<"enter the ID :";
cin>>emp_id;
cout<<"enter Basic : ";
cin>>basic;
}
void employee::net_cal()
{
DA=0.52*basic;
gross=basic+DA;
IT=0.30*gross;
net_sal=gross-IT;
}
void main()
{
clrscr();
employee e1;
e1.read_data();
e1.net_cal();
e1.write_data();
getch();
}
OUTPUT
Enter name : asha
enter the ID :111
enter Basic : 40000
Name : asha
ID : 111
Basic : 40000
DA : 20800
Gross : 60800
IT : 18240
Net_salary : 42560
2. Write a C++ program to read data on N employees and compute the Net_Sal of each employee
(DA = 52% of Basic and Income Tax = 30% of the gross salary)
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class EMPLOYEE
{
char name[10];
int id;
int basic;
float it,net,gross,da;
public:
void readdata();
void cal_net();
void write();
};
void EMPLOYEE::readdata()
{
cout<<"enter the name\n";
cin>>name;
cout<<"enter the id\n";
cin>>id;
cout<<"enter the basic\n";
cin>>basic;
}
void EMPLOYEE::cal_net()
{
da=0.52*basic;
gross=basic+da;
it=0.30*gross;
net=gross-it;
}
void EMPLOYEE ::write()
{
printf("\n%s\t %d\t %d\t %1.2f %1.2f %1.2f %1.2f",name,id,basic,da,it,gross,net);
}
void main()
{
EMPLOYEE e[10];
int n,i;
clrscr();
cout<<"how many records u want";
cin>>n;
cout<<"enter the records\n";
for(i=0;i<n;i++)
{
e[i].readdata();
e[i].cal_net();
}
cout<<"name\t id\t basic\t da\t it\t gross\t net\n";
for(i=0;i<n;i++)
{
e[i].write();
}
getch();
}
Output
how many records u want 3
enter the records
enter the name
asha
enter the id
111
enter the basic
30000
enter the name
laxmi
enter the id
222
enter the basic
10000
enter the name
fiza
enter the id
333
enter the basic
25000
3. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an array of 10
STUDENT objects. Using appropriate functions, find the average of the two better marks for each
student. Print the USN, Name and the average marks of all the students.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int usn,m1,m2,m3;
char name[20];
public:
void getdata();
void putdata();
};
void student::getdata()
{
int i;
cout<<"enter the usn number:";
cin>>usn;
cout<<"enter the name:";
cin>>name;
cout<<"\n enter the 3 marks:\n";
cin>>m1>>m2>>m3;
}
void student::putdata()
{
float avg,min;
min=m1;
if(m2<min)
min=m2;
if(m3<min)
min=m3;
avg=(m1+m2+m3-min)/2;
cout<<endl<<"usn:"<<usn;
cout<<endl<<"name:"<<name;
cout<<endl<<"average marks:"<<avg;
}
main()
{
student s[10];
int i,n;
clrscr();
cout<<endl<<"enter number of students n:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter"<<i+1<<"students data:"<<endl;
s[i].getdata();
}
cout<<endl<<"students information:"<<endl;
for(i=0;i<n;i++)
s[i].putdata();
getch();
return 0;
}
OUTPUT
50 60 50
60 50 60
Usn : 101
Name : RAJESH
Average : 55
Usn : 102
Name : RAMKUMAR
Average : 60
4. Write a C++ program to create a class called COMPLEX and implement the following overloading
functions ADD that return a complex number:
1. ADD(a, s2) – where ‘a’ is an integer (real part) and s2 is a complex number
2. ADD(s1, s2) – where s1 and s2 are complex numbers
#include<iostream.h>
#include<conio.h>
class complex
{
int real, imag;
public:
void getdata();
void display();
friend complex add(int a, complex s2);
friend complex add(complex s1, complex s2);
};
//function to accept real and imaginary parts of a
//complex number
void complex::getdata()
{
cin>>real>>imag;
}
//overloaded ADD function taking one integer and
//one complex number
complex add(int a, complex s2)
{
complex temp;
temp.real=a+s2.real;
temp.imag=s2.imag;
return temp;
}
//overloading ADD function taking both complex parameters
complex add(complex s1, complex s2)
{
complex temp;
temp.real=s1.real+s2.real;
temp.imag=s1.imag+s2.imag;
return temp;
}
//Function to display the complex number
void complex::display()
{
cout<<real<<"+i"<<imag<<endl;
}
void main()
{
clrscr();
OUTPUT :
Enter the first complex value for real, imaginary
5
4
Enter the second complex value for real, imaginary
4
3
1st complex no:5+i4
2nd complex no:4+i3
enter a integer value(real part)
2
S3 complex no(a+s2.real)+imaginary:6+i3
s4 complex no[s1+s2]:9+i7
5. Write a C++ program to create a class called LIST (linked list) with member functions to insert an
element at the front as well as to delete an element from the front of the list. Demonstrate all the
functions after creating a list object.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class LIST
{
private:
struct node
{
int data;
struct node *link;
}*p;
public:
LIST()
{
p=NULL;
}
void insert(int);
void delete1();
int countList();
void display();
};
//Function to insert a node at front
void LIST::insert(int n)
{
node *q;
q = new node;
q->data = n;
q->link = p;
p=q;
}
//Function to delete a node from front
void LIST::delete1()
{
node *q;
if(p == NULL)
{
//cout<<"\nList is empty";
return;
} else {
cout<<"\nThe deleted Node is : "<<p->data<<endl;
q=p;
p=p->link;
delete q;
}
}
//Function to display the contents of the list
void LIST::display()
{
node *q;
if(p==NULL)
cout<<"The list is empty";
else {
for(q=p;q!=NULL;q=q->link)
{
cout<<q->data<<"\t";
}
}
cout<<"\n";
}
void main()
{
clrscr();
LIST l;
int ch, item;
while(1)
{
cout<<"\nSINGLY LINKED LIST"<<endl;
cout<<"*************************"<<endl;
cout<<"\n1.Insert\n2.Delete\n3.Exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the node value"<<endl;
cin>>item;
l.insert(item);
l.display();
break;
case 2:
l.delete1();
l.display();
break;
case 3:
exit(0);
default:
cout<<"enter correct choice"<<endl;
getch();
}
}
}
OUTPUT:
SINGLY LINKED LIST
1. Insert
2. Delete
3. Exit
1. Insert
2. Delete
3. Exit
1.Insert
2. Delete
3. Exit
List is Full.
1.Insert
2. Delete
3. Exit
2 3
1.Insert
2. Delete
3. Exit
1.Insert
2. Delete
3. Exit
List is empty.
6. Write a C++ program to create a template function for Quicksort and demonstrate sorting of
integers and doubles.
#include<iostream.h>
#include<conio.h>
template<class T>
void quicksort(T a[], int low, int high)
{
int pos;
if(low<high)
{
pos=partition(a, low, high);
quicksort(a, low, pos-1);
quicksort(a, pos+1, high);
}
}
template<class T>
int partition(T a[], int low, int high)
{
T key, temp;
int left, right;
key=a[low];
left=low+1;
right=high;
while(1)
{
while((left<high) &&(key>=a[left]))
left++;
while(key<a[right])
right--;
if(left<right)
{
temp=a[left];
a[left]=a[right];
a[right]=temp;
}
else
{
temp=a[low];
a[low]=a[right];
a[right]=temp;
return (right);
}
}
}
void main()
{
clrscr();
int a[10],n,i,low, high;
double b[10];
cout<<"QUICK SORT USING FUNCTION TEMPLATE(To perform int,double array
operatons"<<endl;
cout<<"*************************************************************************"<<end
l;
cout<<"enter array size\n";
cin>>n;
cout<<"Enter the elements of integer array\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the elements of double array\n";
for(i=0;i<n;i++)
cin>>b[i];
low=0;
high=n-1;
quicksort(a, low, high);
quicksort(b, low, high);
cout<<"The sorted list of integer:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
cout<<"\nThe sorted list of double:\n";
for(i=0;i<n;i++)
cout<<"\t"<<b[i];
getch();
}
OUTPUT:
QUICK SORT USING FUNCTION TEMPLATE(T0 perform int, double array )
Enter array size : 3
7. Write a C++ program to create a class called STACK using an array of integers. Implement the
following operations by overloading the operators ‘+’ and ‘-‘:
c. s1 = s1 + element; where s1 is an object of the class STACK and element is an integer to be
pushed on the top of the stack
d. s1 = s1- ; where s1 is an object of the class STACK. ‘-‘ operator pops the element.
Handle the STACK empty and full conditions. Also display the contents of the stack after each
operation, by overloading the << operator.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//#include<PROCESS.H>
#define size 5
class stack
{
private: int s[5];
int top;
public:
stack();
~stack();
friend stack operator+(stack s1,int e);
friend stack operator-(stack s1);
friend ostream &operator <<(ostream & print,stack s);
};
stack::stack()
{
top = -1;
}
stack::~stack()
{
}
stack operator+(stack s1,int e)
{
if(s1.top == 4)
cout<<"stack full";
else
{
s1.top ++;
s1.s[s1.top]= e;
}
return s1;
}
stack operator-(stack s1)
{
if(s1.top == -1)
{
cout<<"stack empty";
}
else
{
cout<<"poped element is"<<s1.s[s1.top];
s1.top =s1.top - 1;
}
return s1;
}
main()
{
stack s1;
int element,ch= 1;
clrscr();
while(1)
{
cout<<"\n 1:push\n 2:pop \n 3:quit";
cout<<"\nenter choice ";
cin >>ch;
switch(ch)
{
case 1: cout <<"enter element ";
cin>>element;
s1 = s1 +element;
cout<<s1;
break;
case 2: s1 = -s1 ;
cout<<s1;
break;
default: exit(0);
}
}
// return(0);
}
OUTPUT:
1. PUSH
2. POP
3. QUIT
3
1. PUSH
2. POP
3. QUIT
2. POP
3 QUIT
1. PUSH
2. POP
3. QUIT
Stack full.
1. PUSH
2. POP
3. QUIT
1. PUSH
2. POP
4. QUIT
1. PUSH
2. POP
3. QUIT
Stack empty.
8. Write a C++ program to create a class called DATE. Accept two valid dates in the form dd/mm/yy.
Implement the following operations by overloading the operators ‘+’ and ‘-‘. After every operation
display the results by overloading the operator <<.
c. no_of_days = d1 – d2; where d1 and d2 are DATE objects, and no_of_days is an integer
d. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class date
{
public:
int dd,mm,yyyy;
// int month[13];
long days;
date(int d,int n,int y);
date getdata(int ,int);
date operator +(long n);
long operator-(date);
friend ostream &operator<<(ostream& ,date);
};
date :: date(int d,int m,int y)
{
dd=d;
mm=m;
yyyy=y;
int feb=((yyyy%4==0&&yyyy%100!=0)||(yyyy%400==0)) ? 29:28;
int month[]={0,31,feb,31,30,31,30,31,31,30,31,30,31};
days=0;
if(d>month[m])
{
cout<<"\nINVALID DATE";
exit(0);
}
for(int i=1;i<mm;i++)
days+=month[i];
days+=dd;
}
long date ::operator -(date d2)
{
long days1=days;
long days2=d2.days;
long d=days1-days2;
for(int i=d2.yyyy;i<yyyy;i++)
d+=((i%4==0&&i%100!=0)||(i%400==0))?366:365;
return d;
}
dt.dd=1;
}
}
return dt;
}
void main()
{
int dd ,mm,yyyy;
clrscr();
cout<<"First Date :\n";
cout<<"Day: ";cin>>dd;
cout<<"Month: ";cin>>mm;
cout<<"Year: ";cin>>yyyy;
date d1(dd,mm,yyyy);
valid(dd,mm,yyyy);
cout<<"enter the second date:\n";
cout<<"Day: ";cin>>dd;
cout<<"month: ";cin>>mm;
cout<<"year: ";cin>>yyyy;
date d2(dd,mm,yyyy);
valid(dd,mm,yyyy);
// isless(d1,d2);
clrscr();
cout<<"\n Date1: "<<d1;
cout<<"\n Date2: "<<d2;
long no_of_days=d1-d2;
cout<<"\nno of days :"<<no_of_days;
cout<<"\nEnter the days to be added to date1:";cin>>no_of_days;
d2=d1+no_of_days;
cout<<"Result date is : "<<d2;
getch();
}
9. Create a class called MATRIX using two-dimensional array of integers. Implement the following
operations by overloading the operator ++ which checks the compatibility of two matrices to be
added and subtracted. Perform the addition and subtraction by overloading the + and – operators
respectively. Display the results by overloading the operator <<.
If (m1==m2) then m3 = m1+m2 and m4 = m1-m2 else display error.
#include<iostream.h>
#include<conio.h>
class matrix
{
private:
int a[5][5],row,col;
public :
//initializing the matrix size
void initialize(int r, int c)
{
row = r;
col = c;
}
void getmatrix();
int operator == (matrix m2);
matrix operator + (matrix m2);
matrix operator - (matrix m2);
friend ostream &operator << (ostream &print,matrix m2);
};
//getting the array elements
void matrix :: getmatrix()
{
for(int i= 0;i<row;i++)
for(int j= 0;j<col;j++)
cin>>a[i][j];
}
//comparing M1(row,col) and M2 matrix(row,col)
int matrix :: operator == (matrix m2)
{
return(m2.row == row&& m2.col == col);
}
//matrix addition
matrix matrix :: operator + (matrix m2)
{
matrix temp;
for(int i= 0;i<row;i++)
for(int j= 0;j<col;j++)
temp.a[i][j]= a[i][j] + m2.a[i][j];
temp.row = m2.row;
temp.col = m2.col;
return temp;
}
//matrix subtraction
matrix matrix :: operator - (matrix m2)
{
matrix temp;
for(int i= 0;i<row;i++)
for(int j= 0;j<col;j++)
temp.a[i][j]= a[i][j] - m2.a[i][j] ;
temp.row = m2.row;
temp.col = m2.col;
return temp;
}
//Displaying the m1,m2,m3,m4 matrix
ostream &operator <<(ostream &print,matrix m)
{
print<<"\n\n";
for(int i= 0;i<m.row;i++)
{
for(int j= 0;j<m.col;j++)
print<<"\t"<<m.a[i][j];
print<<"\n\n";
}
return print;
}
void main()
{
matrix m1,m2,m3,m4;
int r,c,ch;
clrscr();
cout<<"MATRIX ADDITION AND SUBTRACTION"<<endl;
cout<<"**********************************"<<endl;
cout<<"ENTER THE ORDER OF THE MATRIX1:";
cin>>r>>c;
m1.initialize(r,c);
cout<<"ENTER THE ORDER OF THE MATRIX2:";
cin>>r>>c;
m2.initialize(r,c);
if(m1 == m2)
{
cout<<"Enter M1 matrix elements:";
m1.getmatrix();
cout<<"\nENTER M2 matrix elements:";
m2.getmatrix();
cout<<"\nM1 MATRIX:"<<endl;
cout<<m1;
cout<<"\nM2 MATRIX:"<<endl;
cout<<m2;
m3 = m1+m2;
m4 = m1-m2;
cout<<"\nMatrix Addition:"<<m3<<endl;
cout<<"\nMatrix Subtraction:"<<m4<<endl;
}
else
cout<<"Matrix addition and subtraction not possible"<<endl;
getch();
}
OUTPUT
2 4
6 8
Matrix Subtraction
0 0
1 0
10. Write a C++ program to create a class called OCTAL which has the characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded
operator +.
c. OCTAL h = x; where x is an integer.
d. int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator << . Also display the values of h and y.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class octal
{
int num;
public:
octal()
{
num=0;
}
octal(int n)
{
num=n;
}
int operator+(octal n1)
{
return (num+n1.num);
}
friend ostream&operator<<(ostream &print,octal o);
};
void main()
{
int x,k,y,oct;
//cout<<"enter the octal number"<<endl;
//cin>>oct;
octal h(8);
clrscr();
cout<<"number in base 8 is"<<h<<endl;
cout<<"\n ENTER THE DECIMAL NUMBER";
cin>>k;
cout<<"\n THE equivalent in octal is:"<<k<<endl;
y=h+k;
cout<<" RESULT OF Y=h+k IS "<<y<<"\n";
getch();
OUTPUT
Number in base 8 is : 10
Enter Decimal Number :5
Result of y = h + k is : 15
11. Write a C++ program to create a class called QUEUE with member functions to add an element and
to delete an element from the queue. Using the member functions, implement a queue of integers
and double. Demonstrate the operations by displaying the contents of the queue after every
operation.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
queue<int> iq;
queue<double> dq;
int choice,item;
double fitem;
while(1)
{
cout<<"\n Queue using template:-";
cout<<"\n==========================";
cout<<"\n\t 1.insert int\n\t 2.delete int \n\t 3.inert double\n\t 4.delete double\n\t
5.exit";
cout<<"\n==========================";
cout<<"\n Enter your choice:-";
cin>>choice;
cout<<"\n==========================";
switch(choice)
{
case 1:
cout<<"\n enter element to insert:-";
cin>>item;
iq.insert(item);
iq.display();
break;
case 2:
iq.delets();
iq.display();
break;
case 3:
cout<<"\n Enter element to insert:-";
cin>>fitem;
dq.insert(fitem);
dq.display();
break;
case 4:
dq.delets();
dq.display();
break;
case 5:
exit(0);
break;
default:
cout<<"\n Enter appropriate choice:-";
break;
}
getch();
}
OUTPUT
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-1
==========================
enter element to insert:-
1
1
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-3
==========================
Enter element to insert:-3.56
3.56
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-
3
==========================
Enter element to insert:-6.8
3.56 6.8
Queue using template:-
==========================
1.insert int
2.delete int
3.inert double
4.delete double
5.exit
==========================
Enter your choice:-1
enter element to insert:-7
1 7
12. Write a C++ program to create a class called DLIST (doubly Linked List) with member functions to
insert a node at a specified position and delete a node from a specified position of the list.
Demonstrate the operations by displaying the content of the list after every operation.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class Dlist
{
struct node
{
node *llink;
int data;
node *rlink;
}*start;
public:
Dlist()
{
start=NULL;
}
void insert_pos(int, int);
void delete_pos(int);
void display();
};
if(temp==NULL)
{
cout<<"memory allocation failed"<<endl;
exit(0);
}
temp=new node;
temp->data=item;
temp->llink=NULL;
temp->rlink=NULL;
if(pos==1)
{
temp->rlink=start;
start->llink=temp;
start=temp;
return;
}
p=1;
prev=NULL;
cur=start;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
}
else
cout<<"\ninvalid position";
}
void Dlist::delete_pos(int pos)
{
node *next, *temp, *prev, *cur;
int p;
if(start==NULL)
{
cout<<"\nlist is empty\n";
return;
}
if(pos==1)
{
temp=start;
start=start->rlink;
start->llink=NULL;
cout<<"\ndeleted element is:"<<temp->data<<endl;
delete temp;
return;
}
p=1;
prev=NULL;
cur=start;
while(p!=pos &&cur!=NULL)
{
prev=cur;
cur=cur->rlink;
p++;
}
if((p==pos)&&(cur!=NULL))
{
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
cout<<"\ndeleted element is:"<<cur->data<<endl;
delete cur;
return;
}
else
cout<<"\ninvlid position\n";
return;
}
void Dlist::display()
{
node *temp;
if(start==NULL)
{
cout<<"\nlist is empty\n";
return;
}
cout<<"\ncontents of the list\n";
for(temp=start;temp!=NULL;temp=temp->rlink)
{
cout<<temp->data<<endl;
}
}
void main()
{
Dlist d;
int item, pos, ch;
clrscr();
while(1)
{
cout<<"\nDOUBLY LINKED LIST"<<endl;
cout<<"*************************"<<endl;
cout<<"1.insert at position\n";
cout<<"2.delete from position\n";
cout<<"3.display\n4.exit\n";
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the item:";
cin>>item;
cout<<"Enter the position:";
cin>>pos;
d.insert_pos(pos, item);
break;
case 2:
cout<<"\nEnter the position:"<<endl;
cin>>pos;
d.delete_pos(pos);
break;
case 3:
d.display();
break;
case 4: exit(0);
default:
cout<<"Enter correct choice\n";
getch();
}
}
}
OUTPUT
1.Insert at position
2.Delete from position
3.Display
4.Exit
1. Insert at position
2.Delete from position
3.Display
4.Exit
1. Insert at position
2.Delete from position
3.Display
4.Exit
1..Insert at position
2.Delete from position
3.Display
4.Exit
13. Write a C++ program to create a class called STUDENT with data members USN, Name and Age.
Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester, Fees
and Stipend. Enter the data for at least 5 students. Find the semester-wise average age for all UG
and PG students separately.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class student
{
protected:
char name[20];
char USN[11];
int age;
public:
void getstudent();
};
//deriving a class from the class student
class UGstudent:public student
{
int sem;
float fees, stipend;
public:
void getug();
friend void average(UGstudent *ug,int n);
};
//deriving a class from the class student
class PGstudent:public student
{
int sem;
float fees, stipend;
public:
void getpg();
friend void average(PGstudent *pg, int m);
};
//Function to accept the name, usn and age of the student
void student::getstudent()
{
cout<<"enter name, usn and age"<<endl;
cin>>name>>USN>>age;
}
//Function to accept the UG student details
void UGstudent::getug()
{
cout<<"enter semester no, fees and stipend"<<endl;
cin>>sem>>fees>>stipend;
}
//Functin to accept the PG student details
void PGstudent::getpg()
{
cout<<"enter semester no, fees and stipend"<<endl;
cin>>sem>>fees>>stipend;
}
//Function to calculate the average age of UG students
void average(UGstudent ug[], int n)
{
int count[10], i;
float sum[10];
for(i=0;i<n;i++)
{
count[i]=0;
sum[i]=0;
}
for(i=0;i<n;i++)
{
sum[ug[i].sem-1]=sum[ug[i].sem-1]+ug[i].age;
count[ug[i].sem-1]++;
}
for(i=0;i<4;i++)
{
if(count[i]!=0)
{
cout<<"the average age of semester "<<i+1;
cout<<"is:"<<sum[i]/count[i]<<endl;
}
}
}
//Function to calculate the average of PG student
void average(PGstudent pg[], int m)
{
int count[10], i;
float sum[10];
for(i=0;i<m;i++)
{
count[i]=0;
sum[i]=0;
}
for(i=0;i<m;i++)
{
sum[pg[i].sem-1]=sum[pg[i].sem-1]+pg[i].age;
count[pg[i].sem-1]++;
}
for(i=0;i<4;i++)
{
if(count[i]!=0)
{
cout<<"the average age of semester "<<i+1;
cout<<"is:"<<sum[i]/count[i]<<endl;
}
}
}
void main()
{
UGstudent ug[10];
PGstudent pg[10];
int choice, n, m, i;
clrscr();
while(1)
{
cout<<"1.ug student\n2.pg student\n 3.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter no.of ug students"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
ug[i].getstudent();
ug[i].getug();
}
average(ug, n);
break;
case 2:
cout<<"enter no.of ug students"<<endl;
cin>>m;
for(i=0;i<m;i++)
{
pg[i].getstudent();
pg[i].getpg();
}
average(pg, m);
break;
case 3:
exit(0);
default:
cout<<"enter correct choice"<<endl;
getch();
}
}
}
OUTPUT
1. UG student
2. PG student
3. Exit
Raj
1ox07mca11
25
12000
1
1000
1. UG student
2. PG student
3. Exit
ram
1ox07mca12
24
12000
1
1000
14. Write a C++ program to create a class called STRING and implement the following operations.
Display the results after every operation by overloading the operator <<.
a. STRING s1 = “VTU”
b. STRING s2 = “BELGAUM”
c . STRING s3 = s1 + s2 (Use copy constructor)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:
char s[20];
public:
string(char x[]);
string(string &x);
friend string operator+(string s1,string s2);
//string operator+(string s2);
friend ostream &operator<<(ostream &print,string x);
};
strcpy(s,x.s);
cout<<s;
}
//overloaded function for operator + to concatenate two strings
string operator +(string s1,string s2)
{
string temp(s1); //calling the copy constructor
strcat(temp.s, s2.s);
return temp;
}
void main()
{
clrscr();
// string s1="VTU ";
string s1("vtu");
cout<<"first string is:"<<s1;
// string s2="BELGAUM";
string s2("belgaum");
cout<<"second string is:"<<s2;
string s3=s1+s2;
cout<<"resulted string is:"<<s3;
getch();
}
OUTPUT
15. Write a C++ program to create a class called BIN_TREE (Binary Tree) with member functions to
perform in-order, preorder and post-order traversals. Create a BIN_TREE object and demonstrate
the traversals.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class tree
{
private:
struct node
{
node *llink;
int data;
node *rlink;
}*root;
public:
tree()
{
root=NULL;
}
void insert();
void call(int);
void preorder(node *);
void inorder(node *);
{
inorder(root->llink);
cout<<root->data<<"\t";
inorder(root->rlink);
}
}
void tree::postorder(node *root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
cout<<root->data<<"\t";
}
}
void tree::call(int ch)
{
switch(ch)
{
case 1:
preorder(root);
break;
case 2:
inorder(root);
break;
case 3:
postorder(root);
break;
}
}
void main()
{
clrscr();
tree t;
int choice;
while(1)
{
cout<<"\n1.insert\n";
cout<<"2.preorder\n3.inorder\n4.postorder\n";
cout<<"5.exit\n";
cout<<"Enter your choice";
cin>>choice;
switch(choice)
{
case 1:
t.insert();
break;
case 2:
cout<<"Preorder traversal";
t.call(1);
break;
case 3:
cout<<"Inorder traversal";
t.call(2);
break;
case 4:
cout<<"postorder traversal:";
t.call(3);
break;
case 5:
exit(0);
default:
cout<<"Enter correct choide\n";
}
}
}
OUTPUT
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted19
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted15
Current:0x8fdd0000
15<19
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted21
Current:0x8fdd0000
21<19
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice1
Enter the element to be inserted40
Current:0x8fdd0efe
Current:0x8fdd0000
40<21
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice2
Preorder traversal19 15 21 40
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice3
Inorder traversal15 19 21 40
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice4
postorder traversal:15 40 21 19
1.insert
2.preorder
3.inorder
4.postorder
5.exit
Enter your choice5
16. Write a C++ program to create a class called EXPRESSION. Using appropriate member functions
convert a given valid infix expression into postfix form. Display the infix and postfix expressions.
PROGRAM
/*infix to postfix*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class EXP
{
char infix[50],postfix[50],stack[50];
int top;
public:
EXP()
{
top=-1;
}
void getinfix();
void infix_postfix();
void push(char);
int pop();
int preced(char);
void putpostfix();
};
void EXP::getinfix()
{
int len;
cout<<"Enter a valid expression";
cin>>infix;
len=strlen(infix);
infix[len]=')';
infix[++len]='\0';
}
void EXP::infix_postfix()
{
int i,j=0;
char symbol,temp;
push('(');
for(i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
switch(symbol)
{
case'(':push(symbol);
break;
case ')':temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[j]=temp;
j++;
}
push(symbol);
break;
default: postfix[j++]=symbol;
break;
}
}
postfix[j]='\0';
}
void EXP::push(char symb)
{
top=top+1;
stack[top]=symb;
}
int EXP::pop()
{
char symb;
symb=stack[top];
top=top-1;
return(symb);
}
int EXP::preced(char symb)
{
switch(symb)
{
case '^':return 3;
case '*':
case '/':return 2;
case '+':
case '-':return 1;
default: return 0;
}
}
void EXP::putpostfix()
{
cout<<"POSTFIX EXP:"<<postfix;
}
void main()
{
EXP e;
clrscr();
e.getinfix();
e.infix_postfix();
e.putpostfix();
getch();
getch();
}
OUTPUT
Enter a valid expression(A*D)/(C^(E+B)-C)
POSTFIX EXP:AD*CEB+^C-/