C++ Practical File
C++ Practical File
C++ Practical File
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void add();
void del();
void display();
struct node
{
int x ;
struct node *next ;
};
struct node *s,*e,*p ;
void main ()
{
s = NULL;
e = NULL;
int ch ;
do
{
clrscr ();
cout<<"1. Add to queue"<<endl ;
cout<<"2. Remove from queue"<<endl ;
cout<<"3. Display queue"<<endl ;
cout<<"4. Exit "<<endl ;
cout<<"enter your choice " ;
cin>>ch ;
switch (ch)
{
case 1 : add ();
break ;
1|Page
break ;
default: exit(0) ;
}
}while(ch!=4);
}
void add ()
{
p = new node;
cout<<"Enter a number ";
cin>>p->x;
if (s==NULL && e==NULL)
{
s = p;
e = p;
p->next = NULL;
return p;
}
else
{
e->next = p;
p->next = NULL;
e = p;
}
cout<<"The node is successfully added inside the
queue";
getch ();
}
void del ()
{
if (s == NULL && e == NULL)
{
cout<<"Queue underflow";
getch ();
}
else
{
p = s;
s = s->next;
}
2|Page
if (s==NULL)
e = NULL;
p->next = NULL;
cout<<"Node deleted successfully and the value
is "<<p->x;
delete p;
getch ();
}
}
void display()
{
p = s;
while (p!=NULL)
{
cout<<p->x<<endl;
p = p->next;
}
getch ();
}
3|Page
Q2. Write a menu driven program to implement linked
queue of numbers using class
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x ;
struct node *next ;
};
class queue
{
struct node *s,*e,*p ;
public:
queue();
void add();
void del();
void display();
};
queue::queue ()
{
s = NULL;
e = NULL;
}
void queue::add()
{
p = new node;
cout<<"Enter a number ";
cin>>p->x;
if (s==NULL && e==NULL)
{
s = p;
e = p;
p->next = NULL;
return ;
}
else
4|Page
{
e->next = p;
p->next = NULL;
e = p;
}
cout<<"The node is successfully added inside the
queue";
getch ();
}
void queue::del ()
{
if (s==NULL && e==NULL)
{
cout<<"Queue underflow";
getch ();
}
else
{
p = s;
s = s->next;
if (s==NULL)
e = NULL;
p->next = NULL;
cout<<"Node deleted successfully and the value
is "<<p->x;
delete p;
getch ();
}
}
void queue::display ()
{
p = s;
while (p!=NULL)
{
cout<<p->x<<endl;
p = p->next;
}
getch ();
}
5|Page
void main ()
{
queue q;
int ch;
do
{
clrscr ();
cout<<"1. Add to queue"<<endl;
cout<<"2. Remove from queue"<<endl;
cout<<"3. Display queue"<<endl;
cout<<"4. Exit "<<endl;
cout<<"enter your choice ";
cin>>ch;
switch (ch)
{
case 1 : q.add ();
break;
default: exit(0);
}
}while(ch!=4);
}
6|Page
Q3. Write a menu driven program to implement linked
stack of numbers
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void display();
struct node
{
int x;
struct node *next;
};
struct node *top,*p;
void main ()
{
top = NULL;
int ch;
do
{
clrscr ();
cout<<"1. Push number onto stack"<<endl;
cout<<"2. Pop number from stack"<<endl;
cout<<"3. Display entire stack"<<endl;
cout<<"4. Exit"<<endl;
cout<<"enter your choice ";
cin>>ch;
switch (ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
default: exit(0);
}
7|Page
}while(ch!=4);
}
void push ()
{
p = new node;
cout<<"Enter a number ";
cin>>p->x;
if (top==NULL)
{
top = p;
p->next = NULL;
}
else
{
p->next = top;
top = p;
}
cout<<"The node is successfully pushed onto the
stack";
getch ();
}
void pop ()
{
if (top==NULL)
{
cout<<"Stack underflow";
getch ();
}
else
{
p = top;
top = top->next;
p->next = NULL;
cout<<"Node popped successfully and the popped
value is "<<p->x;
delete p;
getch ();
}
}
8|Page
void display ()
{
p = top;
while (p!=NULL)
{
cout<<p->x<<endl;
p = p->next;
}
getch ();
}
9|Page
Q4. Write a menu driven program to implement linked
stack of numbers using class
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next;
};
class stack
{
struct node *top,*p;
public:
stack ();
void push();
void pop();
void display();
};
stack::stack()
{
top = NULL;
}
void stack::push()
{
p = new node;
cout<<"Enter a number ";
cin>>p->x;
if (top==NULL)
{
top = p;
p->next = NULL;
}
else
{
p->next = top;
top = p;
}
10 | P a g e
cout<<"The node is successfully pushed onto the
stack";
getch ();
}
void stack::pop ()
{
if (top==NULL)
{
cout<<"Stack underflow";
getch ();
}
else
{
p = top;
top = top->next;
p->next = NULL;
cout<<"Node popped successfully and the popped
value is "<<p->x;
delete p;
getch ();
}
}
void stack::display ()
{
p = top;
while (p!=NULL)
{
cout<<p->x<<endl;
p = p->next;
}
getch ();
}
void main()
{
stack s;
int ch;
do
{
clrscr ();
cout<<"1. Push number onto stack"<<endl;
11 | P a g e
cout<<"2. Pop number from stack"<<endl;
cout<<"3. Display entire stack"<<endl;
cout<<"4. Exit"<<endl;
cout<<"enter your choice ";
cin>>ch;
switch(ch)
{
case 1 : s.push();
break;
case 2 : s.pop();
break;
case 3 : s.display();
break;
default: exit(0);
}
}while(ch!=4);
}
12 | P a g e
Q5. Write a program to create a circular linked list of N
numbers where in is accepted from the user. Then display
the contents of list
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void display();
struct node
{
int x;
struct node *next;
};
struct node *e,*p;
void main ()
{
e = NULL;
int n;
cout<<"How many nodes do you want? ";
cin>>n;
int i = 1;
while(i<=n)
{
p = new node;
cout<<"Enter a number ";
cin>>p->x;
if (e==NULL)
{
e = p;
p->next = p;
}
else
{
p->next= e->next;
e->next = p;
e = p;
}
i++;
}
}
13 | P a g e
void display()
{
p = e->next;
while(p!=e)
{
cout<<p->x<<endl;
p = p->next;
}
cout<<e->x;
getch();
}
14 | P a g e
Q6. Consider the following structure
struct student
{
int rno;
char name[20];
int marks[5]; marks of 5 subjects
}
Write a c++ program that will create a binary file
"stu.dat" having some records
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
struct student
{
int rno;
char name[20];
int marks[5]; // marks of 5 subjects
};
void main ()
{
fstream.f;
f.open("stu.dat",ios::out | ios::binary);
student s;
char ch;
do
{
clrscr ();
cout<<"Enter roll number ";
cin>>s.rno;
cout<<"Enter name ";
gets(s.name);
for(int i=0;i<5;i++)
{
cout<<"Enter marks of subject number
"<<i+1<<" ";
cin>>s.marks[i];
}
15 | P a g e
f.write((char*)&s,sizeof(s));
cout<<"Want to continue y/n ";
ch = getche();
}while (tolower(ch)=='y');
f.close();
}
16 | P a g e
Q7. Consider the following structure
struct student
{
int rno;
char name[20];
int marks[5]; marks of 5 subjects
}
Write a c++ program that will display all the
records of the binary file "stu.dat" created previously
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
struct student
{
int rno;
char name[20];
int marks[5]; // marks of 5 subjects
};
void main ()
{
fstream f;
f.open("stu.dat",ios::in | ios::binary);
student s;
f.read((char*)&s, sizeof(s));
if(!f)
{
cout<<"File not present";
getch();
return;
}
while(!f.eof())
{
cout<<"Roll number "<<s.rno<<endl;
cout<<"Name "<<s.name<<endl;
for(int i=0;i<5;i++)
{
17 | P a g e
cout<<"Marks of subject number "<<i+1<<"
"<<s.marks[i]<<endl;
}
f.read((char *)&s,sizeof(s));
}
getch();
f.close();
}
18 | P a g e
Q8. Consider the following structure
struct student
{
int rno;
char name[20];
int marks[5]; marks of 5 subjects
}
Write a c++ program that accets a roll number value
from the user and then searches for that roll number
inside the binary file stu.dat.
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
struct student
{
int rno;
char name[20];
int marks[5]; // marks of 5 subjects
};
void main ()
{
fstream f;
f.open("stu.dat",ios::in | ios::binary);
student s;
int temp ;
cout<<"Enter student roll number to be searched ";
cin>>temp;
if (!f)
{
cout<<"File not present";
getch ();
return;
}
f.read((char*)&s, sizeof(s));
clrscr();
while(!f.eof())
{
19 | P a g e
if(s.rno==temp)
{
cout<<"Roll number "<<s.rno<<endl;
cout<<"Name "<<s.name<<endl;
for(int i=0;i<5;i++)
{
cout<<"Marks of subject number
"<<i+1<<" "<<s.marks[i];
cout<<endl;
}
}
f.read((char*)&s, sizeof(s));
}
getch();
f.close();
}
20 | P a g e
Q9. Consider the following structure
struct student
{
int rno;
char name[20];
int marks[5]; marks of 5 subjects
}
Write a c++ program that accepts a roll number value
from the user and then delete the record of the specified
roll number from the fle stu.dat(that was created
previously)
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
struct student
{
int rno;
char name[20];
int marks[5]; // marks of 5 subjects
};
void main ()
{
fstream f1,f2;
f1.open("stu.dat",ios::in | ios::binary);
f2.open("temp.dat",ios::out | ios::binary);
student s ;
int temp ;
cout<<"Enter student roll number to be deleted ";
cin>>temp ;
if(!f1)
{
cout<<"File (stu.dat) does not exit";
getch ();
return ;
}
f1.read((char*)&s, sizeof(s));
clrscr ();
21 | P a g e
while(!f1.eof())
{
if(s.rno!=temp)
{
f2.write((char*)&s, sizeof(s));
}
f1.read((char*)&s, sizeof(s));
}
getch();
f1.close();
f2.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
}
22 | P a g e
Q10. Consider the following structure
struct student
{
int rno;
char name[20];
int marks[5]; //marks of 5 subjects
}
Write a c++ program that accepts a roll number value
from the user and then modify the record of the specified
roll number from the fle stu.dat(that was created
previously) with new specified values
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
struct student
{
int rno;
char name[20];
int marks[5]; // marks of 5 subjects
};
void main ()
{
fstream f1,f2;
f1.open("stu.dat",ios::in | ios::binary);
f2.open("temp.dat",ios::out | ios::binary);
student s;
int temp;
cout<<"Enter student roll number to be deleted ";
cin>>temp;
if(!f1)
{
cout<<"File (stu.dat) does not exit";
getch ();
return;
}
f1.read((char*)&s, sizeof(s));
23 | P a g e
clrscr ();
while(!f1.eof())
{
if(s.rno!=temp)
{
f2.write((char*)&s, sizeof(s));
}
else
{
cout<<"Existing details are "<<endl;
cout<<"Roll number "<<s.rno<<endl;
cout<<"Enter name "<<s.name<<endl;
for(int i=0;i<5;i++)
{
cout<<"Marks of subject number "<<i+1<<"
";
cout<<s.marks[i]<<endl;
}
cout<<"Press any key to continue"<<endl;
getch ();
cout<<"Enter new details "<<endl;
cout<<"Enter name ";
gets(s.name);
for(i=0;i<5;i++)
{
cout<<"Enter marks of subject number
"<<i+1<<" ";
cin>>s.marks[i];
cout<<endl;
}
f2.write((char*)&s, sizeof(s));
}
f1.read((char*)&s, sizeof(s));
}
getch();
f1.close();
f2.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
}
24 | P a g e
Q11. Write a program that will implement bubble sort
algorithm inside an integer array of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a[10];
int i;
for(i=0;i<10;i++)
{
cout<<"Enter a number ";
cin>>a[i];
}
int boundary = 9;
while (boundary>0)
{
i = 0;
while (i<boundary)
{
if(a[i]>a[i+1])
{
int tub = a[i];
a[i] = a[i+1];
a[i+1] = tub;
}
i ++;
}
boundary -- ;
}
cout<<"The sorted array is "<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
getch();
}
25 | P a g e
Q12. Write a program that will implement selection sort
algorithm inside an integer array of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a[10];
int i;
for(i=0;i<10;i++)
{
cout<<"Enter a number ";
cin>>a[i];
}
int boundary = 9;
while (boundary>0)
{
int largest = a[0];
int index = 0;
i = 0;
while (i<=boundary)
{
if(a[i]>largest)
{
largest = a[i];
index = i;
}
i ++;
}
a[index] = a[boundary];
a[boundary] = largest;
boundary --;
}
cout<<"The sorted array is "<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
getch();
}
26 | P a g e
Q13. Write a program that will implement insertion sort
algorithm inside an integer array of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a[10];
int i;
for(i=0;i<10;i++)
{
cout<<"Enter a number ";
cin>>a[i];
cout<<endl;
}
i = 1;
while(i<10)
{
int j = i;
while (j>0)
{
if(a[j]<a[j-1])
{
int tub = a[j];
a[j] = a[j-1];
a[j-1] = tub;
}
else
break ;
j --;
}
i ++;
}
cout<<"The sorted array is "<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
getch();
}
27 | P a g e
Q14. Write a program that will implement linear search
algorithm inside an integer array of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a[10];
int i;
for(i=0;i<10;i++)
{
cout<<"Enter a number ";
cin>>a[i];
cout<<endl;
}
cout<<"Enter a number to be searched ";
int n;
cin>>n;
int flag = 0;
i = 0;
while (i<10)
{
if(a[i]==n)
{
flag = 1;
cout<<"Number found at position "<<i+1;
}
i ++;
}
if (flag==0)
{
cout<<"Number not present ";
}
getch();
}
28 | P a g e
Q15. Write a program that will implement binary search
algorithm inside an integer array of 10 numbers.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a[10];
int i;
for (i=0;i<10;i++)
{
cout<<"Enter a number ";
cin>>a[i];
cout<<endl;
}
//now sort the array
int boundary = 9;
while (boundary>0)
{
i = 0;
while (i<boundary)
{
if(a[i]>a[i+1])
{
int tub = a[i];
a[i] = a[i+1];
a[i+1] = tub;
}
i ++;
}
boundary --;
}
cout<<"Enter a number to be searched ";
int n;
cin>>n;
int flag = 0;
int l=0,h=9,m;
while (l<=h)
{
29 | P a g e
m = (l+h)/2;
if (n>a[m])
{
l = m+1;
}
else
{
if (n<a[m])
{
h = m-1;
}
else
{
cout<<"Number is present at position
"<<m+1;
flag = 1;
break;
}
}
}
if(flag==0)
cout<<"Number not present ";
getch();
}
30 | P a g e
Q16. Consider a DATABASE named EMPLOYEE with following
tables
EMP
ENO NAME DOB SALARY DEPTNO
1 HITESH 1995-02-10 10000 10
2 HIMANSHU 1998-12-20 20000 10
3 ANKIT 1999-08-25 15000 20
4 JITESH 1997-05-20 20000 30
5 ASHWIN 1999-07-29 25000 10
DEPT
DEPTNO DNAME CITY
10 ACCOUNTS DELHI
20 HRD GURGAON
30 IT NOIDA
31 | P a g e
B. Write commands to insert given records inside tale
EMP and DEPT.
Ans. (i)(a) insert into emp values(1,’HITESH’,’1995-02-
10’,10000,10);
(b) Insert into emp values(2,’HIMANSHU’,’1998-
12-20’,20000,10);
(c) Insert into emp values(3,’ANKIT’,’1999-08-
25’,15000,20);
(d)Insert into emp values(4,’JITESH’,’1997-05-
20’,20000,30);
(e) Insert into emp values(5,’ASHWIN’,’1999-07-
29’,25000,10);
32 | P a g e
Q3. Write sql commands for the following :
33 | P a g e
(ix) Display details of those employess from emp table
where name starts with ‘H’.
Ans. Select * from emp where name like ‘H%’;
34 | P a g e
35 | P a g e