Upload
ashutosh-kumar
View
242
Download
1
Embed Size (px)
Citation preview
7/25/2019 Practical File of computer science
1/61
COMPUTERSCIENCE
PRACTICAL FILE
XII
1
7/25/2019 Practical File of computer science
2/61
Index
S.No Program Teachers Sign.
1. Function overloading
2. Class using private function
3. Class using constructor
4. Single inheritance
5. Multiple inheritance
6. Multilevel inheritance
7. Writing in text file
8. Program to use multiple files in succession.
9. Reading and writing in file with class bjects.10. Program to use multiple files in succession.
11. Linear search
12. Binary search
13. Selection sort
14. Bubble sort
15. Insertion sort
16. Merging of two arrays
17. Sum of rows of a 2-D array18. Sum of columns of a 2-D array
19. Sum of Diagonal element of a 2-D array
20. Insertion,Deletionand traversal in array
queue
21. Insertion,Deletion and traversal in linked
queue
2
7/25/2019 Practical File of computer science
3/61
22. Insertion,Deletion and traversal in array
stack
23. Insertion,Deletion and traversal in linked
stack
24. Insertion and deletion in circular queue
25.
Program to illustrate function overloading
#include
#include
#include
int divide(int a,int b)
{
if(b == )
return !1 " if((ab)==)
return 1"
else
return "
$ int divide(int a)
{
int %=a&2"
int fla'= 1"
for(int i=2"(i
7/25/2019 Practical File of computer science
4/61
int ch, res, a, b"
do
{ cout > ch"
cout > a >> b"
res = divide(a,b)"
cout ch
7/25/2019 Practical File of computer science
5/61
Write a program to display record of a topper student or specific student
depending upon the user choice.
#include
#include#include && for 'ets( )
#include
class ;tudent { rivate3
int rollno"
char name2? "
float mars "
char 'rade " ublic3
7/25/2019 Practical File of computer science
6/61
void read;tudent( )
{
cout = )
'rade =DHD "
else if ( mars >= :)
'rade =D-D" else 'rade =DD "
$
$"
int main( )
{int i"
;tudent XIIa1? " for( i= " i< " i)
{
cout
7/25/2019 Practical File of computer science
7/61
cout > choice"
s4itch(choice)
{
case 1 3 cout hi'hmars)
{os=i"
hi'hmars = XIIai?.'etBars( ) "
$
$
XIIaos?.dis;tudent( ) "
brea "
case 3 brea"
default 3 cout
7/25/2019 Practical File of computer science
8/61
N
7/25/2019 Practical File of computer science
9/61
Class With Constructor#include
#include
#include
#include>'siOe"
cout
7/25/2019 Practical File of computer science
10/61
void disla*()
{ cout
7/25/2019 Practical File of computer science
11/61
Write a program to implement single inheritance.
#include
#include
&& 6emonstratin' the class inheritance
class base{
rivate3
int counter" && Aeresents rivate class data
ublic3
base()
{
counter = "
$
void ne4set(int n)
{
counter = n"
$
void factorial(void)"
$"
class derived3 ublic base
{
ublic3
derived()3 base()
{ $"
void chan'ecounter(int n)
{ne4set(n)"
factorial()"
$
$"
void base33factorial(void)
{
int i"
lon' double fact = 1."
for (i = 1" i
7/25/2019 Practical File of computer science
12/61
cin >> num"
tderived.chan'ecounter(num)"
'etch()"
return "
$
OUTPUT
12
7/25/2019 Practical File of computer science
13/61
Write a program todemonstrate the hierarichal inheritance
#include #include
class -0ol*'on
{ rotected3
int 4idth, hei'ht"
ublic3
void setRvalues (int a, int b)
{
4idth = a" hei'ht = b"
$$"
class -Aectan'le3 ublic -0ol*'on
{
ublic3
int area()
{
return (4idth Q hei'ht)"
$
$"
class -Jrian'le3 ublic -0ol*'on
{
ublic3
int area()
{
return (4idth Q hei'ht & 2)"
$
$"
int main ()
{
&&clrscr()"
-Aectan'le rect"-Jrian'le tr'l"
rect.setRvalues (:, )"
tr'l.setRvalues (:, )"
cout
7/25/2019 Practical File of computer science
14/61
OUTPUT
1:
7/25/2019 Practical File of computer science
15/61
Write a program to illustrate multilevel inheritance
#include
#include
#include
class mlo*ee && Hase class or suer class of emlo*ee{
rivate3
int emcode"
char emname2?"char emdesi'1?"
float emsalar*"ublic3
void eminutRdata(void)"
void emdisla*(void)"
$"
&& derived class of base class
class -ustomer 3 ublic mlo*ee{
rivate3int custcode"
char custname2?"
char custdesi'1?"
double balance"ublic3
void custinutRdata(void)"
void custdisla*(void)"
$"
&& derived class of derived class
class mR-ust 3 rivate -ustomer
{
ublic3
void 'etRdata()"
void sho4Rdata()"
$"
void mR-ust33'etRdata(void)
{
eminutRdata()"
1
7/25/2019 Practical File of computer science
16/61
custinutRdata()"
$
void mR-ust33sho4Rdata()
{
emdisla*()"
custdisla*()"
$
void mlo*ee33eminutRdata(void){
cout > emcode"
cout > emsalar*"
$
void -ustomer33custinutRdata(void)
{
cout > custcode"
cout > balance"
$
void mlo*ee33emdisla*(void)
{
cout
7/25/2019 Practical File of computer science
17/61
cout
7/25/2019 Practical File of computer science
18/61
Write a program to implement multiple inheritance
#include
#include
#include
class mlo*ee && Hase class or suer class of mlo*ee{ rivate3
int emcode"
char emname2?"
char emdesi'1?"
float emsalar*"
ublic3
void eminutRdata(void)"
void emdisla*(void)"
$"
&& Hase class or suer class of -ustomer
class -ustomer
{ rivate3
int custcode"
char custname2?"
char custdesi'1?"
double balance"
ublic3
void custinutRdata(void)"
void custdisla*(void)"
$"
&& 6erived class for multile inheritanceclass mR-ust 3 ublic mlo*ee, ublic -ustomer
{ ublic3
void 'etRdata()"
void sho4Rdata()"
$"
void mR-ust33'etRdata(void)
{ eminutRdata()"
custinutRdata()"1N
7/25/2019 Practical File of computer science
19/61
$
void mR-ust33sho4Rdata()
{ emdisla*()"
custdisla*()"
$
void mlo*ee33eminutRdata(void)
{ cout > emcode"
cout > emsalar*"
$
void -ustomer33custinutRdata(void)
{ cout > custcode"
cout > balance"
$
void mlo*ee33emdisla*(void)
{ cout
7/25/2019 Practical File of computer science
20/61
mR-ust emcust" && 6eclares Eb%ect of t*e mlo*ee and -ustomer
emcust.'etRdata()"
emcust.sho4Rdata()"
'etch()"
return "
$
OUTPUT
2
7/25/2019 Practical File of computer science
21/61
Write a program get roll numbers and marks of the students of a class and
store these details into a file called Marks.dat
#include
#include
int main()
{ ofstream fileout" && stream decided and declared!stes 1 2
fileout.oen(+mars.dat+,ios33outTios33a)" && file lined!ste
char ans =D*D" &&rocess as reUuired!ste : be'ins
int rollno" float mars"
4hile(ans==D*D ans==D*D)
{ cout
7/25/2019 Practical File of computer science
22/61
Write a programfor reading and writing class obects.
#include
#include
#include
class student { -har name:?"
char 'rade"
float mars"
ublic3
void 'etdata(void)"
void disla*(void)" $
void student33'etdata(void)
{ char ch"
cin.'et(ch)"
cout
7/25/2019 Practical File of computer science
23/61
coutmars"
cout
7/25/2019 Practical File of computer science
24/61
Write a program to search in a file having records maintained through classes.
#include
#include
class stu { int rollno"
2:
7/25/2019 Practical File of computer science
25/61
char name2?"
char -lass:?"
float mars"
char 'rade"
ublic3
void 'etdata()
{ cout = C) 'rade = DGD"
else if(mars >= F) 'rade = DHD"
else if(mars >= ) 'rade = D-D"
else if(mars >= :) 'rade = D6D"
else 'rade=DD"
$
void utdata()
{ cout
7/25/2019 Practical File of computer science
26/61
Write a program to insert data in a sorted file.
#include
#include
2F
7/25/2019 Practical File of computer science
27/61
#include && for rename() and remove()
class stu{ int rollno"
char name2?"
char -lass:?"
float mars"
char 'rade"
ublic3
void 'etdata(){ cout = C) 'rade = DGD"
else if(mars >= F) 'rade = DHD"
else if(mars >= ) 'rade = D-D"
else if(mars >= :) 'rade = D6D"
else 'rade=DD"
$
void utdata()
{ cout
7/25/2019 Practical File of computer science
28/61
{ fi.read((charQ)stud,siOeof(stud))"
fo.4rite((charQ)stud,siOeof(stud))"
$
$
fi.close()"
fo.close()"
remove(+stu.dat+)"
rename(+tem.dat+),+stu.dat+)"fi.oen(+stu.dat+,ios33in)"
cout
7/25/2019 Practical File of computer science
29/61
Write a program to delete a record from a file.
#include
#include
#include && for rename() and remove()
#include> rollno"
cout = C) 'rade = DGD"
else if(mars >= F) 'rade = DHD"
else if(mars >= ) 'rade = D-D"
else if(mars >= :) 'rade = D6D"
else 'rade=DD"
$
void utdata()
{ cout
7/25/2019 Practical File of computer science
30/61
else
file.4rite((charQ)s1, siOeof(stud))"
$
if(found == DfD)
cout
7/25/2019 Practical File of computer science
31/61
Write a program to search a element using !inear search
#includeint Msearch(int?,int,int)"
int main()
{ int GA?,IJB,5,inde" cout
7/25/2019 Practical File of computer science
32/61
element found at inde3, osition3F
Write a program to search a element using"inary search
#include #include
void clrRscreen(void)"
void main()
{
int ran'e?, J, i, %, 5KB"
int first = , last, mid = , find = , search"clrRscreen()"
'oto*(, )"
cout ran'e%?)
{
J = ran'ei?"
ran'ei? = ran'e%?"
ran'e%? = J"
$
$
$
cout
7/25/2019 Practical File of computer science
33/61
cin >> search"
4hile ((first
7/25/2019 Practical File of computer science
34/61
Jhe osition of the element is 3
#nsertion in an array at particular location
#include
#include
void main()
{int ran'e1?, 5, num, loc, rev, i"
clrscr()"
cout > num"
cout = loc)
{
ran'erev? = ran'erev!1?"
rev = rev ! 1"
$
ran'erev? = num"
5 = 5 1"cout
7/25/2019 Practical File of computer science
35/61
OUTPUT
nter the len'th of *our arra* 3 F
nter the arra* values
Lalue 1. 2
Lalue 2. 12Lalue . 2
Lalue :. :
Lalue . 2
Lalue F. :
nter the inserted value 3 F
nter the location to insert in arra* 3
Jhe resulted outut is 3
2
12
F
2
:
2
:
7/25/2019 Practical File of computer science
36/61
Write a program to deleted from an array range at loc place
#include
#include
void main()
{
int ran'e1?, 5, loc, rev, i, num"
clrscr()"
cout > loc"cout
7/25/2019 Practical File of computer science
37/61
else
cout
7/25/2019 Practical File of computer science
38/61
$emonstration of the program for selection sort
#include
#include main()
{
int ran'e1?, loc, lo4est, J, 5, i, %, "
clrscr()"
cout > ran'ei?"
$
for (i = " i < 5 ! 1" i)
{
lo4est = ran'ei?"
loc = i"
for (% = i 1" % < 5" %)
{
if (lo4est > ran'e%?){
loc = %"
lo4est = ran'e%?"
$
$
J = ran'ei?"
ran'ei? = ran'eloc?"
N
7/25/2019 Practical File of computer science
39/61
ran'eloc? = J"
$
cout
7/25/2019 Practical File of computer science
40/61
$emonstration of the program for "ubble sort
#include
#include
#include
int arra*N? = {2, C, :N, C, 12, P2, NF, $"
void bubble(int a?, int 5)"
int main(void){
int i"utchar(D/nD)"
cout a%1?)
{
:
7/25/2019 Practical File of computer science
41/61
tem = a%?"
a%? = a%1?"
a%1? = tem"
$
$
OUTPUT
nter the arra* elements 3 2
C
:N
C
12
P2
NF
Knsorted elements...
2 C :N C 12 P2 NF
;orted elements...
12 2 C :N C NF P2
:1
7/25/2019 Practical File of computer science
42/61
$emonstration of program insertion sort
#include
#include
void main()
{
int ran'e1?, i, %, 5, J, "
clrscr()"
cout > ran'ei?"
$
for (i = 1" i < 5" i)
{J = ran'ei?" && tracts the first element of the unsorted art
% = i ! 1"
4hile ((J < ran'e%?) (% >= ))
{
ran'e% 1? = ran'e%?"
% = % ! 1"
$
:2
7/25/2019 Practical File of computer science
43/61
ran'e% 1? = J"
for ( = " < 5" ) && 6isla*s the outut after ever* insertion
cout
7/25/2019 Practical File of computer science
44/61
:
:
F
C
C
C
P
Write a program to calculate the sum of rows
#include
#include
void Ao4Rsum(int B?F?)
{int sum, i, %"
for (i=" i
7/25/2019 Practical File of computer science
45/61
$
OUTPUT
enter elements of arra*1
2
:
F
arra* is
1 2
:
F
;um of Ao4s 3 1 is 3
;um of Ao4s 3 2 is 3 C
;um of Ao4s 3 is 3 11
$emonstration for M%&'% sort operation by using two sorted arrays.
#include
#include void main()
{
int ran'11?, ran'21?, ran'1?"
int B, 5, i, %, , J"
clrscr()"
cout > ran'2%?"
i = " && Gssi'n to start from th osition in arra*
% = "
= "
:
7/25/2019 Practical File of computer science
46/61
4hile ((i < B) (% < 5))
{
if (ran'1i? < ran'2%?)
{
ran'? = ran'1i?"
= 1"
i = i 1"
$else
{
ran'? = ran'2%?"
= 1"
% = % 1"
$
$
4hile (i
7/25/2019 Practical File of computer science
47/61
:
:
F
C
N
Write a program to display the sum of both the diagonal elements of a two
dimensional array#include
#include
const B = F"
const 5 = F"
void sum()
{
clrscr()"
int BGJAIXB?5?, i, %, s1 = , s2 = "
cout > BGJAIXi?%?"
$
&& 6isla* the arra* elements
for(i=" i
7/25/2019 Practical File of computer science
48/61
for(%=" %
7/25/2019 Practical File of computer science
49/61
C N P
sum of main dia'onal is 1
sum of other dia'onal is 1
Write a program illustrates the basic operations add( delete and display in array
)ueue.#include #include
#include
#include
#include
#define BGX 1 && ;ho4s maimum arra* len'th
int UueueBGX?" && 6eclares arra* 'lobal variable
int front, rear" && 6eclares inte'er front and read
&& unction rotot*es of add Uueue, delete Uueue, and sho4 Uueue in arra* imlementation
void addRW(int Uueue?, int val, int rear)" && Gdd Uueue
int delRW(int Uueue?, int front, int rear)" && 6elete Uueuevoid sho4RW(int Uueue?, int front, int rear)" && ;ho4 Uueue
void main()
{int choice, val"
char ot = DVD" && Jo continue the do loo in case
rear = !1" && InitialiOation of Wueue
front = !1"
clrscr()"
:P
7/25/2019 Practical File of computer science
50/61
do
{
cout > choice"
s4itch (choice)
{
case 13
do
{
cout > ot"
$ 4hile (touer(ot) == DVD)"
brea"
case 23
ot = DVD" && InitialiOe for the second loo
do
{
val = delRW(Uueue, front, rear)"
if (val 9= !1)
cout > ot"
$ 4hile (touer(ot) == DVD)"
brea"
case 3
sho4RW(Uueue, front, rear)"
brea"case :3
eit()"
$$
4hile (choice 9= :)"
$&& unction bod* for add Uueue 4ith arra*
void addRW(int Uueue?, int val, int rear)
{
if (rear == BGX)
{
7/25/2019 Practical File of computer science
51/61
cout
7/25/2019 Practical File of computer science
52/61
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above 1
nter the value to be added in the Uueue 2
6o *ou 4ant to add more elements 7 *
nter the value to be added in the Uueue F6o *ou 4ant to add more elements 7 *
nter the value to be added in the Uueue F
6o *ou 4ant to add more elements 7 n
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above 2
Lalue deleted from Wueue is 2
6o *ou 4ant to delete more elements 7 n
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above Jhe values areF
F
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue :. it from Benu
nter *our choice from above :
2
7/25/2019 Practical File of computer science
53/61
Write a program to illustrates the basic operation add ( delete and display in
linked )ueue .
#include
#include #include
#include
#include
&& 6eclares a Uueue structure
struct node
{int data"
node Qlin"
7/25/2019 Practical File of computer science
54/61
$"
&& unctions rotot*e for add Uueue, delete Uueue, and sho4 Uueue
node QaddRW(node Qrear, int val)" && Gdd Uueue
node QdelRW(node Qfront, int val)" && 6elete Uueue
void sho4RW(node Qfront)" && ;ho4 Uueue
&& Bain ro'rammin' lo'ic
void main()
{ node Qfront, Qrear"
int val, choice"
char ot = DVD" && Jo continue the do loo in case
front = rear = 5KMM" && InitialiOation of Wueue
clrscr()"
do
{
cout > choice"
s4itch (choice)
{
case 13
do
{
cout
7/25/2019 Practical File of computer science
55/61
$ 4hile (touer(ot) == DVD)"
brea"
case 3
sho4RW(front)"
brea"
case :3
eit()"
$$
4hile (choice 9= :)"
$
&& unction bod* for add Uueue elements
node QaddRW(node Qrear, int val)
{
node Qtem"
tem = ne4 node"
tem!>data = val"
tem!>lin = 5KMM"
rear!>lin = tem"
rear = tem"
return (rear)"
$
&& unction bod* for delete Uueue elements
node QdelRW(node Qfront, int val)
{
node Qtem"
clrscr()"
if (front == 5KMM)
{cout data"
tem!>lin = 5KMM"
delete tem"$
return (front)"
$&& unction bod* for sho4 Uueue elements
void sho4RW(node Qfront)
{
node Qtem"
tem = front"
7/25/2019 Practical File of computer science
56/61
clrscr()"
cout lin"
$
$
OUTPUT
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above 1
nter the value to be added in the Uueue F
6o *ou 4ant to add more elements 7 *
nter the value to be added in the Uueue CN
6o *ou 4ant to add more elements 7 *P
nter the value to be added in the Uueue
6o *ou 4ant to add more elements 7 n
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above 1nter the value to be added in the Uueue F
6o *ou 4ant to add more elements 7 *
nter the value to be added in the Uueue CN
6o *ou 4ant to add more elements 7 *
nter the value to be added in the Uueue P
6o *ou 4ant to add more elements 7 nF
7/25/2019 Practical File of computer science
57/61
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above
Lalue deleted from Wueue is F
6o *ou 4ant to delete more elements 7 n
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above
Jhe Wueue values are
CN
P
Bain Benu
1. Gddition of Wueue
2. 6eletion from Wueue
. Jraverse of Wueue
:. it from Benu
nter *our choice from above :
1.Write a program to implement array-stac
#include
#include
int pop(int[],int&);
int push(int[],int&,int);
void display (int [],int);
const int size= 50;
int main()
{ int stack[size],item,top=-1,res;
C
7/25/2019 Practical File of computer science
58/61
char ch='y';
while(ch=='y'||ch=='Y')
{coutitem;
res=push(stack,top,item);
if(res==-1)
{cout
7/25/2019 Practical File of computer science
59/61
top--;
}
return ret;}
void display(int stack[],int top)
{ if(top==-1)
return;
cout
7/25/2019 Practical File of computer science
60/61
Write a program to implementLinked-Stack.
#include
#include
#include
struct Node { int info; Node * next;
} *top, *newptr, *save, *ptr;
Node * Create_New_Node( int );
void Push( Node* );
void Display( Node* );
void main( )
{ int inf; char ch = y;
top = NULL; clrscr( );
while ( ch == y || ch == Y )
{ cout > inf;
newptr = Create_New_Node( inf );
if ( newptr == NULL )
{ cout info = n; ptr -> next = NULL;
return ptr;
}
void Push( Node* np)
{ if ( top == NULL ) top = np;
else { save = top; top = np;
np -> next = save;
}
}
void Display( Node* np )
{ while ( np != NULL )
{ cout info ;
F
7/25/2019 Practical File of computer science
61/61
np = np -> next;
}
cout !!!
Press Y to enter more nodes, N to exity
Enter INFOrmation for the new node8Now the linked-stack is :
8 -> 5 -> !!!
Press Y to enter more nodes, N to exity
Enter INFOrmation for the new node15
Now the linked-stack is :
15 -> 8 -> 5 -> !!!
Press Y to enter more nodes, N to exit.