Sei sulla pagina 1di 148

Practical report

of

computer science

SESSION 2019-2020

SUMBITTED BY
KAMAL KISHORE CHAUHAN

Under the guidance of


Mr. N.CHANDRASHEKAR
For the partial fulfilment of class
Class XII CBSE

Submitted to
JAWAHARLAL NEHRU SCHOOL
B.H.E.L, BHOPAL

1
ACKNOWLEDGMENT

I would like to express my special thanks of gratitude to


my teacher Bhavana Namdeo as well as our principal
Shraddha Muzumdar who helped me in doing a lot of
Research and I came to know about so many new things
I am really thankful to them for finalizing this file

Secondly I would also like to thank my parents and


friends who helped me a lot in finalizing this file within
the limited time frame

YOURE SINCERELY
Kamal Kishore chauhan

2
CERTIFICATE
This is to certify that Kamal Kishore Chauhan of Class
XII has successfully completed his practical file with the
help of C++ Programming under my guidance and
supervision. I am satisfied with their initiatives and the
effort for the completion of this practical file as a part
of curriculum of CBSE Class XII Examination.

Date :-

Place :- Bhopal

Signature of internal Signature of external


Examiner Examiner

3
PREFERANCE
This file provides interaction about general
OOPs(object oriented programming) concept and
database concept to help understand the structure and
mechanism of the programs.
Each functions of the programs has been provided with
algorithm and with screenshots so that readers can
understand better, what is happening during the course
of program usage.
The program does not hold well in all places of
situation, thus limitation of the program have been
listed at the end of the file.

4
INDEX
Section A Pg No.
1. Bubble sort 9
2. Insert sort 14
3. Selection sort 19
4. Insertion of an element in an array 24
5. Deletion of an element in an array 31
6. Merging in array 37
7. Binary search 42
8. Creation and printing of a linked list 47
9. Insertion in a linked list 52
10. Deletion of a linked list 58
11. Searching in a linked list 64
12. Queue as a link list 70
13. Stack as a link list 78
14. Stack as an array 85
15. Queue as an array 93
16. Circular queue 99
17. Matrix Multiplication 108
18. Reversal of three string 114
19. Creation of file using object 114

5
20. Function Overloading 118
21. Constructor Overloading 124
22. Inherited Member 136
23. Public Derivation of class 140
24. Prog. for reading and writing 144

Section B

6
1. Furniture and arrivals
2. Doctors and salary
3. Flights and fairs

7
C++
Programs
(Section A)

8
PRACTICAL 1
DOCUMENTATION:
Name of the program:
Bubble Sort
Name of Source File:
Bubble.cpp
Size of Source File:
1 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h
process.h

9
ALGORITHM
1. for I=L To U
{
2. for J=L To [(U-1)-I]
//Need not consider already settled heavy elements
{
3. if AR[J]>AR[J+1] then
{//swap the values
4. temp=AR[U]
5. AR[J] AR[J+1]
6. AR[J+1]=temp;
} //end of if
} //end of inner loop
} //end of outer loop
7. END

10
CODING
#includec<iostream.h>
#includce<conio.h>
#include<process.h>
void main()
{
clrscr();
int list[20],I,j,temp,n;
cout<<"n \t\t BUBBLE SORT”;
cout<<"\n Enter the size of the array(max 20):”;
cin>>n;
cout<<"\n Enter the array";
for(i=0;i<n;+i)
cin>>list[i];
for(i=0;i<n-1;++i)
{
for(j=0;j<n-1;++i)
{
if(list[j]<small)
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
11
}
}
cout<<"\n\n Sorted array is: \n"
for(i=0;i<n;++i)
cout<<list[i]<" ";
getch();
}

12
OUTPUT
Bubble sort

Enter the size of the array: 6


Enter the array: 9 7 4 6 1 2
Sorted array is:
124679

13
PRACTICAL 2
DOCUMENTATION
Name of the program:
Insert Sort
Name of Source File:
Insert.cpp
Size of Source File:
1 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOS80X Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

14
ALGORITHM
1. A[0]=minimum integer-value
2. Repeat steps 3 through 8 for k= 1,2,3...n-1
{
3. temp=A[k]
4. ptr=k-1
5. Repeat Steps 6 to 7 while temp<A[ptr]
{
6. A[ptr+1]=A[ptr]
7. ptr=ptr-1
}
8. A[ptr+1]=temp
}
9. END

15
CODING
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int list[20],i,j,temp,n;
cout<<”\t\t INSERT SORT";
cout<<"\n Enter the size of the array(max 20)";
cin>>n;
cout<<"\n Enter the array:”;
for(i=0;i<n;++i)
cin>>list[i];
for(i=1;i<n;++i)
{
temp=list[i];
j=i-1;
while(temp<list[i]&&j>=0)
{
List[j+1]=list[j];
j=j-1;
}
list[j+1]=temp;
}
16
cout<<\n Insert Sorted Array is \n";
for(i=0;i<n;++i)
cout<list[i]<<”";
getch();
}

17
OUTPUT:
INSERT SORT

Enter the size of the array: 5


Enter the array: 9 7 4 6 1
Sorted array is: 1 4 6 7 9

18
PRACTICAL 3
DOCUMENTATION:
Name of the program:
Selection sort
Name of Source File:
Constructor.cpp
Size of Source File:
1 KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h
process.h

19
ALGORITHM
1. Small=AR[L] //Initialize small with first array elements
2. forl=L To U do
{
3. small=AR[L], pos=I
//Loop to find the smallest element and its position
4. for J=I To U do
5. {if AR[J]<Small then
6. (small=AR[J]
7. pos=J
}
8. J=J+1
} //End of inner loop
//Swap the smallest element with I th element
9. temp AR[I]
10. AR[I]=small
11. AR[pos]=temp
} //End of outer loop
12. END

20
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int list[20],i,j,temp,small,pos,n;
cout<<"\n \t\t SELECTION SORT";
cout<<"\n Enter the size of the array(max 20):";
cin>>n;
cout<<"\n Enter the array:";
for(i=0;i<n;++i)
cin>>list[i];
for(i=0;i<n-1;++i)
{
Small=list[i];
pos=I;
for(j=i+1;j<n;++j)
{
if(list[j]<small)
small=list[j];
pos=j;
}
21
temp=list[i];
list[i]=list[pos];
list[pos]=temp;
}
cout<<"\n\n Sorted array is:\n";
for(i=0;i<n;++i)
cout<<list[i]<<” ";
getch();
}

22
OUTPUT
SELECTION SORT

Enter the size of the array: 6


Enter the array: 6 8 2 5 3 4
Sorted array is
234568

23
PRACTICAL 4
DOCUMENTATION
Name of the program:
Insertion in an array
Name of Source File:
insinar.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
findpos()
Library header files:
iostream.h
conio.h
process.h

24
ALGORITHM:
/*First the appropriate position for ITEM is to be determined
i.e., if the appropriate position is 1+1 then ARI)ITEMK =AR[
1],15T
specifies maximum possible index in array, U specifies index of
last
element in Array*/
1. ctr=L
2. if LST=U then /* Initialize the counter *
{
Print"overflow:"
Exit from program
}
3. if AR[ctr]>ITEM then
pos=1
else
{
4. Repeat steps 5 and 6 until ctr>=U
5. if AR[ctr]<=ITEM and ITEM<=AR[ctr+ 1] then
{
pos=ctr+1
break
}
6. ctr=ctr+1

25
/*End of Repeat here*/
7. if ctr=U then
pos=U+1
} /*end of if step3*/
/* shift the elements to create space*/
8. ctr=U /*Initialize the counter*/
9. while ctr>= pos perform steps 10 through 11
{
10. AR[ctr+1]-AR [ctr]
11. ctr=ctr-1
}
12. AR[pos]=ITEM /*Insert the element*/
13. END

26
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
int findpos(int[],int,int);
void main()
{
Clrscr();
int ar[50],item,index,n;
cout<<"\n Enter the size of the array(max 50)"
cin>>n;
cout<<”\n Enter the array(in ascending order)";
for(int i=0;i<++i)
clrscr();
char ch='y';
while(ch==y'||ch=='Y')
{
cout<<"\n Enter the element to be inserted";
if(n==50)
{
cout<<"\n Overflow
exit(1);
}
Index=findpos(ar,n,item);
27
for(i=0;i<index;++1)
ar[i]=ar[i-1];
ar[index]=item;
n+=1;
cout<<"\n Want to insert more elements(y/n)??";
cin>>ch;
}
cout<<\n The new array is";
for(i=0;i<n;++i)
cout<<ar[i]<"” ;
getch();
}
int findpos(in tar[],int size,int item)
if(item<ar[0])
pos=0;
else
{
for(int i=0;i<size-1;++i)
if(ar[i]<=item && item<ar[i+1])
{
pos=i+1;
break;
}
}
if(i==size-1)

28
pos=size;
}
return pos;
}

29
OUTPUT
Enter the size of the array (max 50): 6
Enter the array (in ascending order): 2 6 9 10 12 15
Enter the element to be inserted: 11
Want to insert more elements(y/n)??: n
The array now is as shown: 2 6 9 10 11 12 15

30
PRACTICAL 5
DOCUMENTATION:
Name of the program:
Deletion in an array
Name of Source File:
delinst.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
isearch()
Library header files:
iostream.h
conio.h
process.h

31
ALGORITHM
/* Consider that ITEM's search in array AR is successful at
location
pos *
Case 1 Shifting upwards (or in left side)
/* Initialize counter*/
1. ctr=pos
2. Repeat steps 3 and 4 until ctr>=U
3. AR[ctr]=AR[ctr+ 1]
4. ctr=ctr+1
/*End of Repeat*/
Case 2 Shifting downwards (or in right side)
/*Initialize counter*/
1. ctr=pos
2. Repeat steps 3 and 4 until ctr<=1
3. AR[ctr]=AR[ctr-1]
4.ctr=ctr1
/* End of Repeat */

32
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
int isearch(int [],int,int);
void main()
{
clrscr();
int ar[50],item,index,n;
cout<<"\n Enter the size of the array( max 50)";
cin>>n;
cout<<"\n Enter the array";
for(int i-0;i<n;++i)
cin>>ar[i];
clrscr();
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter the element to be deleted";
cin>>item;
if(n==0)
{
cout<<"\n Underflow";
exit(1);
33
}
Index=isearch(ar,n,item);
if(index!=-1)
ar[index]=0;
else
cout<”\n Element not present in array";
cout<<"\n The array is now as shown”;
cout<”\n Zero is the deleted element";
for(i=0;i<n;++i)
cout<<ar[i]<<””;
Cout<<"\n Emptied space is shifted to end";
for(i=index;i<n;++i)
ar[i]=ar[i+1];
n-=1;
cout<<"\n Want to delete more?(y/n)";
cin>>ch;
}
cout<<"\n The new array is";
for(int i=0;i<size;++i)
cout<<ar[i]<<" ";
getch();
}
int Isearch(in tar[],int size,int item)
{
for(int i-0;i<size;++i)

34
{
if(ar[i]==item)
return I;
}
return -1;
}

35
OUTPUT
Enter the size of the array (max 50): 6
Enter the array: 2 4 5 3 7 9 12 15 33 40
Enter the element to be deleted: 9
The array now is as shown: 2 4 5 3 7 0 12 15 33 40
Want to delete more elements? (y/n):n
Array after shifting all empty paces is: 2 4 5 3 7 12 15 33 40

36
PRACTICAL 6
DOCUMENTATION:
Name of the program:
Merging of 2 arrays
Name of Source File:
merge.cpp
Size of Source File:
1 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h
process.h

37
ALGORITHM
1 ctrA=m-1; ctrB=n-1; ctrC=0;
2. while ctrA>=0 and ctrB>=0 perform steps 3 through 10
3. If A[ctrA]<=B[ctrB] then
4. C[ctrC] =A[ctrA]
5. ctrC=ctrC+1
6. ctrA=ctrA-1}
7. else
8. C[ctrC]=B[ctrB]
9. ctrC ctrC+1
10. ctrB=ctrB-1}
11. if ctrA<O then
12. while ctrB>=0 perform steps 13 through 15
13. C[ctrC]=B[ctrB]
14. ctrC=ctrC+1
15. ctrB ctrB-1
16. if ctrB<0 then
17. while ctrA>=0 perform steps 18 through 20
18. C[ctrC]=B[ctrA]
19. ctrC=ctrC+1
20. ctrA =ctrA-1

38
CODING
#include<iostream.h>
#include<conio.h
#include<process.h>
void main()
}
system("cls");
int ar1[20],ar2[20],c[40],1,j,k,n,m,mn;
cout<<\n Enter the size of array 1:";
cin>>m;
cout<<"\n Enter the array 1 descending)\n";
for(i-0;i<m;++i)
cin>>ar1[i];
cout<<"\n Enter the size of array 2: ";
cin>>n;
cout<<"\n Enter the array 2(descending)\n";
for(i=0;i<n;++i)
cin>>ar2[i];
mn=m+n;
for(k=0,i=m-1,j=n-1; (i>=0) && (j>=0);)
if(ar1[i]<ar2[j])
c[k++]=ar1[i--];
else
c[k++]=ar2[j--];
39
}
if(i>=0)
{
while(i>= 0)
c[k++]=ar1[i--];
}
else
{
while(j>=0)
c[k++]=ar1[j--];
}
cout<<"\n\n Merged array is:\n";
for(i=0;i<m;i++)
cout<<c[i]<<””;
getch();
}

40
OUTPUT
Enter the size of array 1: 4
Enter the array 1 (descending): 9 6 3 1
Enter the size of array 2: 7
Enter the array 2(descending): 15 11 8 7 5 4 2
Merged array is: 1 2 3 4 5 6 7 8 9 11 15

41
PRACTICAL 7
DOCUMENTATION:
Name of the program:
Binary search
Name of Source File:
Bin sear.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
b_search{)
Library header files:
iostream.h
conio.h

42
ALGORITHM
1. Set beg L, last= U //In c++, L is 0 and I is size=1
2. Repeat steps 3 through 6 until beg>last /*INT() is used to
extract integer part*/
3. mid=INT((beg+last)/2)
4. if AR[mid]==ITEM then
{
print"Search Successful"
print ITEM, "found at", mid
break//go out of the loop
5. if AR[mid]<ITEM then
beg=mid+1
6. if AR[mid]>ITEM then
last=mid-1
//End of repeat
7. if beg!=last
print"Unsuccessful Search"
8. END.

43
CODING
#include<iostream.h>
#include<conio.h>
int b_search(int [], int ,int);
void main()
{
clrscr();
int ar[50],item,m,n,index;
cout<<"\n\t\t\t BINARY SEARCH";
cout<<\n Enter the size of the array";
cin>>n;
cout<<"\n Enter the array(in ascending order)";
for(int i-0;i<n;++i)
cin>>ar[i];
cout<<"\n Enter the element to be searched";
cin>>item;
index=b_search(ar,n, item);
if(index==- 1)
cout<<"\n Search Unsuccessful";
else
cout<<"\n Element"<<item<<" found at position"<<index+1;
getch();
}
int b_search(in tar[],int size,int item)
44
int beg,mid,end;
beg=0;
end=size-1;
while(beg<=end)
{
mid-(beg+end)/2;
if(ar[mid]==item)
return mid;
if(ar[mid]<item)
beg=mid+1;
else
end=mid-1;
}
return -1;
}

45
OUTPUT
Enter size of the array: 7
Enter the array(in ascending order): 2 5 7 8 9 10 15
Enter element to be searched: 8
Element found at position 4

46
PRACTICAL 8

DOCUMENTATION:
Name of the program:
Creation and printing of link list
Name of Source File:
Cre-linlis.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windo ws 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

47
ALGORITHM
1. Declare pointers START,PTR,NEWPTR REAR
2. ptr=START
3. NEWPTR=new Node
4. if NEWPTR=NULL
5. print "no space available!! Aborting!!"
6. EXIT.
7. else
{
8. NEWPTR->LINK=NULL
}
9. if START=NULL then
10. START=NEWPTR
11. REAR=NEWPTR
}
12. REAR-> LINK=NEWPTR
13. REAR=NEWPTR
14. END.

48
CODING
#include<iostream.h
#include<conio.h>
struct student
{
int rollno;
char name[20];
student *next
};
void main()
{
clrscr();
int i,n,roll,found;
student *first,*last,*ptr,*x;
cout<<"\n CREATING A LINK LIST";
cout<<"\n\n\n Enter size";
cin>>n;
if(n<=0)
{
cout<<"\n ERROR";
return;
}
first=new student;
cout<<"\n Enter roll no. and name'
49
cin>>first->roll no>>first->name;
first->next=NULL;
last=first;
for(i=1;i<n;++i)
{
x=new student;
cout<<"\n Enter roll no. and name";
cin>>x->rollno>>x->name;
x->next=NULL;
last->next=x;
last=x;
}
cout<<"\n List is";
ptr=first;
while(ptr!-NULL)
{
cout<<"\n\n Roll no."<<ptr->roll no;
cout<<"\n Name"<<ptr->name;
ptr=ptr->next;
}
getch();
}
}

50
OUTPUT
CREATING AND PRINTINGA LINK LIST
Enter size: 3
Enter rollno and name: 1
Amit
Enter rollno and name: 2
Karan
Enter rollno and name: 3
Raj
List is
Roll no, 1 Name: Amit
Roll no.:2 Name: Karan
Roll no.:3 Name: Raj

51
PRACTICAL 9
DOCUMENTATION
Name of the program:
Insertion in a link list
Name of Source File:
Insertion.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

52
ALGORITHM
1. Declare pointers START,PTR, NEWPTR REAR
2. ptr=START
3. NEWPTR=new Node
4. if NEWPTR=NULL
5. print "no space available!! Aborting!!"
6. EXIT.
7. else
{
8. NEWPTR->LINK=NULL
{
9. if START=NULL then
}
10. START=NEWPTR
11. REAR=NEWPTR
}
12. REAR-> LINK=NEWPTR
13. REAR=NEWPTR
14. END.

53
CODING
#include<iostream.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
student *next
void main()
{
clrscr();
int I,n,roll,found;
student *first,*last,*ptr,*x;
cout<<"\n INSERTION A LINK LIST";
cout<<"\n\n\n Enter size";
cin>>n;
if(n<=0)
{
cout<<"\n ERROR";
return;
}
first=new student;
cout<<"\n Enter roll no. and name";
cin>>first->rollno>>first->name;
54
first->next=NULL;
last=first;
for(i=1;i<n;++i)
{
x=new student;
cout<<"\n Enter roll no. and name";
cin>>x->rollno>>x->name;
x->next=NULL;
last->next=x;
last=x;
}
cout<<"\n Enter Roll no. after which insertion is to be done";
cin>>roll;
ptr=first;
while(ptr!-NULL & ptr->rollno!-roll)
ptr=ptr->next;
if(ptr->rollno==roll)
{
x=new student;
cout<<"\n Enter roll no. and name";
cin>>x->rollno>>x->name;
x->name=ptr->next;
ptr->next=x;
}
else

55
{
cout<<"\n Insertion not possible";
return;
}
cout<<"\n List after insertion";
ptr=first;
while(ptr!=NULL)
{
cout<<"\n\n Roll no."<ptr->rollno;
cout<<"\n Name"<<ptr->name;
ptr=ptr->next;
}
delete first;
getch();
return;
}

56
OUTPUT
Enter size: 3
Enter rollno and name: 1
Amit
Enter rollno and name: 2
Karan
Enter rollno and name: 3
Raj
Enter Roll no. after which insertion is to be done 2
Ravi
List after insertion
Roll no.: 1Name: Amit
Roll no.:2 Name: Karan
Roll no.:3 Name: Ravi
Roll no.:4 Name: Raj

57
PRACTICAL 10
DOCUMENTATION:
Name of the program:
Deletion in a link list
Name of Source File:
Deletion.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

58
ALGORITHM
1. If START=NULL THEN
2. Print "UNDERFLOW"
3. else
4. { ptr=start
5. Start=ptr->LINK
6. delete ptr }
7. END.

59
CODING
#include<iostream.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
student *next;
};
void main()
{
clrscr();
int I,n,roll;
student first, last,*ptr,*x,*back;
cout<<"\n DELETION IN A LINK LIST";
cout<<"\n\n\n Enter size";
cin>>n;
if(n<=0)
{
cout<<"\n ERROR";
return;
}
first=new student;
cout<<"\n Enter roll no and name";
60
cin>>first->rollno>>first->name;
first->next=NULL;
last=first;
for(i=1;i<n;++i)
{
x=new student;
cout<<"\n Enter roll no. and name";
cin>>x->rollno>>x->name;
x->next=NULL;
last->next=x;
last=x;
}
cout<<"\n Enter the roll no. to be deleted";
cin>>roll;
if(first->rollno== roll)
{
ptr=first;
first=first->next;
delete ptr;
}
else
{
ptr=first->next;
back=first;
while(ptr!-NULL)

61
{
back->next=ptr->next;
delete ptr;
break;
}
back=ptr;
ptr=ptr->next;
}}
cout<<"\n List after deletion";
ptr=first;
while(ptr!=NULL)
{
cout<<"\n\n Roll no."<<ptr->rollno;
cout<<"\n Name">>ptr->name;
ptr=ptr->next;
}
delete first;
getch();
return;
}

62
OUTPUT
DELETION IN A LINK LIST
Enter size: 3
Enter rollno and name: 1
Amit
Enter rollno and name: 2
Karan
Enter rollno and name: 3
Raj
Enter Roll no. to be deleted 2
List after deletion
Roll no.:1 Name: Amit
Roll no.:3 Name: Ravi

63
PRACTICAL 11
DOCUMENTATION:
Name of the program:
Searching in link list
Name of Source File:
Searching.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

64
ALGORITHM
1. if(first==NULL)
a) print list empty
b) Stop
2. ptr=first
3. while ptr=NULL
4. if data(ptr)==x
a. print item found
b. Stop
5. ptr=next(ptr)
6. print item found
7. End.

65
CODING
#include<iostream.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
student *next;
}
void main();
{
clrscr();
int I,n,roll,found;
student *first,*last,*ptr,*x;
cout<<"\n SEARCH IN A LINK LIST";
cout<<"\n\n\n Enter size";
cin>>n;
if(n<=0)
{
cout<<"\n ERROR";
return;
}
first=new student;
cout<<"\n Enter roll no. and name";
66
cin>>first->rollno>>first->name;
first->next-NULL;
last=first;
for(i=1;i<n;++i)
x=new student;
cout<<"\n Enter roll no. and name"
cin>>x->rollno>>x->name;
x->next=NULL;
last->next-x;
last=x;
}
cout<<\n Enter the roll no. to be searched for";
cin>>roll;
if(first==NULL)
{
cout<n List empty";
return;}
ptr=first;
found=0;
while(ptr!-NULL && found-0)
{
if(ptr->rollno==roll)
found=1;
else
ptr=ptr->next;}

67
if(found==1)
{
cout<<"n RECORD FOUND"
cout<<"\n Roll no."<<ptr->rollno;
cout<<"\n Name"<<ptr->name;}
else
cout<<"\n Record Not Found";
delete first;
getch();
return;
}

68
OUTPUT
SEARCHING IN A LINK LIST
Enter size: 3
Enter rollno and name: 1
Amit
Enter rollno and name: 2
Karan
Enter rollno and name: 3
Raj
Enter the rollno to be searched for: 2
Record found:
Rollno. 2
Name: Karan

69
PRACTICAL 12
DOCUMENTATION:
Name of the program:
Queue as a link list
Name of Source File :
D_Queue.cpp
Size of Source File:
2KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
insert()
del()
create_new_node()
display()
Library header files:
iostream.h
stdlib.h
process.h

70
ALGORITHM
Addition in a Queue
In this a node x with value is added into a linked queue.
Condition rear-NULL should check for queue empty
Steps-
1. x=new node
2. data(x)=val
3. next(x)=NULL
4. if front=NULL
a) rear=x
b) front=rear
else
c) next(rear)=x
d) rear=x
Deletion in a Queue
In this a node from the end of the link queue is deleted.
Condition front=NULL is checked for queue empty
Steps-
1. if(front)=NULL
a) print empty queue
b) end
2. x=front
3. front=new found
4. delete x
71
5. if(front=NULL)
then rear= NULL
6. END.

72
CODING
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int info;
node*next;
}*rear,front, newptr,*save, ptr;
node *create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert(node*)
{
if (front==NULU)
front=rear-np;
else
{
rear->next-np;
rear=np;
73
}
}
void display(node np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"!!!\n";
}
void del()
{
if(front==NULL)
cout<<"UNDERFLOW\n";
else
{
ptr=front;
front=front->next;
delete ptr;
}
}
void main()
{
front=rear=NULL

74
int inf;
char ch='y';
front=NULL;
system("cls");
while((ch-='y')|I(ch=='Y'))
{
cout<<"\n Enter information for the new node....";
cin>>inf;
newptr=create_new_node(inf)
if(newptr=NULL)
{
cout<<"\n Cannot create new node !! Aborting!!\n";
exit(1);
}
insert(newptr);
cout<<"\n Now the linked queue is : \n";
display(front);
cout<<"\n Press Y to enter mode nodes and N to exit..."
cin>>ch;
}
system("cls");
do
{
cout<<"\n The linked - queue now is (fron...to...rear): \n";
display(front);

75
cout<<"\n Want to delete the first node (y/n)":
cin>>ch;
if(ch=='y'||ch=='Y')
del();
} while((ch=='y')|| (ch==Y'));
}
getch();
return;
}

76
OUTPUT
Enter information for the new code...3
Press Y to enter more nodes N to exit...Y
Enter information for the new code...4
Press Y to enter more nodes N to exit...Y
Enter information for the new code...15
Press Y to enter more nodes N to exit...N
The linked Queue now is (Front to rear):
3-> 4-> 15-> !!!
Want to delete first node?(y/n).. .y
The linked Queue now is (Front to rear):
4-> 15-> !!!
Want to delete first node?(y/n)....y
The linked Queue now is (Front to rear):
15-> !!!
Want to delete first node?(y/n)...n

77
PRACTICAL 13
DOCUMENTATION:
Name of the program:
Stack as a link list
Name of Source File:
D_stack.cpp
Size of Source File:
2KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
Create_new_node()
push()
display()
Library header files:
iostream.h
stdlib.h
process.h

78
ALGORITHM
Pushing in a Linked-stack
/*Get new node for the ITEM to be pushed*/
1. NEWPTR =new node
2. NEWPTR-> INFO=ITEM; NEWPTR->LINK=NULL
/*Add new node at the top*/
3. If TOP NULL then //Stack is empty
4. TOP=NEWPTR
5. else
{
6. NEWPTR->LIN K=TOP
7. TOP=NEWPTR
}
8. END.
Popping from a Linked Stack
1. If TOP=NULL
2. Print" Stack Empty ,UNDERFLOW!!"
3. Else
{
4. Print TOP->INFO
5. TOP=TOP->LINK}
6. END.

79
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node*next;
};
node*push(node*top,int val);
node*pop(node*top, int&val);
void display(node*top);
void main()
{
clrscr();
node*top;
int val,ch;
top=NULL;
do
{
cout<<"\t MENU";
cout<<"\n1.PUSH";
cout<<"\n2.POP";
cout<<"\n3.DISPLAY";
80
cout<<"\n4.EXIT";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the value";
cin>>val;
top=push(top,val);
break;
case 2: top=pop(top,val);
if(val!= -9999)
cout<<"\n The popped value is";
cout<<val;
break;
case 3: display(top);
break;
case 4: exit(0);
default:cout<<"\n Wrong choice";
}
}
while((ch>=1)&&(ch<=4));
getch();
}
node*push(node*top,int val)

81
{
node*x;
x=new node;
x->data=val;
x->next=top;
top=x;
return(top);
}
node*pop(node*top,int&val)
{
node*x;
if(top==NULL)
{
cout<<"\n stack empty";
val=-9999;
}
else
{
x=top;
top=top->next;
val=x->data;
}
return(top);
}
void display(node*top)

82
{
node*ptr;
ptr=top;
cout<<"\n The list is";
while(ptr!=NULL)
{
cout<<"\n data="<<ptr->data;
ptr=ptr->next;
}
}

83
OUTPUT
Enter information for the new code….6
Press Y to enter more nodes N to exit... Y
Enter information for the new code...8
Press Y to enter more nodes N to exit...Y
Enter information for the new code...9
Press Y to enter more nodes N to exit...N
The stack now is:
9->8->6-> !!!
Want to pop an element?(y/n)..
The stack now is:
8->6-> !!!
Want to pop an element?(y/n)...
The stack now is.
6-> !!!
Want to pop an element?(y/n)...n

84
PRACTICAL 14
DOCUMENTATION:
Name of the program:
Stack as an array
Name of Source File:
Func_ove.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
Constructor-stack)
Member function- push()
Pop()
Display()
Library header files:
iostream.h
conio.h
process.h

85
ALGORITHM
//*Assuming that array stack can hold maximum N
elements*//
1. top-1 //* Initialize stack with invalid subscript value to
show that it is empty. Legal subscript values are 0....(N-1)*/
2. Read Item //ltem, which is to be pushed//
3. if(top-=N-1) them
4. {print "Overflow!!"
{
5. else
6. top-top+1/*Increment top to move to next empty position
hold new item*/
7. Stack[top]=Item}
8. END.
Popping from Array-Stack
1. if top-=-1 then
}
2. Print "Underflow!!"
Exit from program
3. Else
4. print stack(top)
5. top-top-1/* top moved, so that the top most element
becomes inaccessible which is a sort of a deletion*/ }
6. END.

86
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
int s[10],top,n;
public:
stack()
{
top=-1;
n=10;
}
void push(int val);
int pop();
void display();
};
void stack::push( int val)
{
if(top<n)
{
top++;
s[top]=val;
}
87
else
cout<<"\n Stack full";
}
int stack::pop()
{
int v;
if(top>=0)
{
v=s[top];
top--;
return v;
}
else
{
cout<<"\n Stack Empty";
return -99;
void stack::display()
{
if(top>=0)
{
for(int i=top;i>=0;--i)
cout<<s[i]<<””;
}
getch();
}

88
void main()
{
clrscr();
stack s;
int ch,val;
cout<<"\n Stack as an array";
do
{
cout<<"\n MENU:";
cout<<"\n 1.Push";
cout<<"\n2. Pop"
cout<<"\n3. Display";
cout<<"\n4. Exit";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n Enter the value to be pushed";
cin>>val;
s.push(val);
break;
case 2: val=s.pop();
if(val!=-99)
cout<<"\n Popped Element is "<<val;
else

89
cout<<"\n Underflow";
break;
case 3:couts<"\n Stack is t";
s.disp();
break;
case 4: exit(0);
default: cout<<"\n Invalid Choice";
break;
}
}
while(cj>0&&ch<5);
getch();}

90
OUTPUT
MENU
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 1
Enter the value to be pushed 60
MENU
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 1
Enter the value to be pushed 20
MENU
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 3
Stack is: 20 60
MENU

91
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 2
Popped value is 20
MENU
1. Push
2. Pop
3. Display
4. Exit
Enter your choice 3
Stack is: 60
MENU
1. Push
2. Pop
3. Display
4. Exit
Enter your choice

92
PRACTICAL 15
DOCUMENTATION:
Name of the program:
Queue as an array
Name of Source File:
StaticQueue.cpp
Size of Source File:
2 КВ
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
add()
del()
display()
Library header files:
iostream.h
conio.h
process.h

93
ALGORITHM
Insertion
1. if rear= NULL
2. rear=front=0
3. QUEUE[0]=ITEM
4. else if rear= N-1 then
5 print "QUEUE FULL, OVERFLOW!!"
6. else
7. QUEUE[rear+1]=ITEM
8. rear=rear+1
9. END.
Deletion
1. if front=NULL
2. print "Queue Empty, Underflow!!"
3. else
4. ITEM=QUEUE[front]
5. if front=rear then
6. front=rear=NULL
7. else
8. front=front+1
9. END.

94
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
int n;
void add(int q[],int front,int &rear,int val)
{
if(rear<n)
{
q[rear]=val;
rear++;
}
else
cout<<"\n Queue Full";
}
int del(int q[],int &front,int rear)
{
int v;
if(front!=rear)
{
v=q[front++];
return v;
}
else
95
cout<<"\n Queue empty";
return -99;
}
void display(int q[].int front, int rear)
{
int i;
if(front<rear)
{
for(i=front;i<rear;++i)
cout<<q[i]<<"t";
}
}
void main()
{
clrscr();
int q[10],front, rear,val,choice;
cout<<"\n \t\t STATIC QUEUE";
cout<<"\n Enter the size of the queue(max 10)";
cin>>n;
front=rear=0;
do
{
cout<<"\n\t\t MENU"
cout<<"\n 1.ADD";
cout<<"\n 2,DELETE

96
cout<<"\n 3.DISPLAY
cout<<"\n 4.EXIT";
cout<<"\n Enter your choice";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter the value to be added";
cin>>val;
add(q,front,rear,val);
break;
case 2: val=del(q,front,rear)
if(val!=-99)
cout<<"\n Deleted value is "<<val;
break;
case 3: cout<<"\n Queue is\t";
display(q,front,rear);
break;
case 4: exit(0);
default:cout<"\n Wrong choice";
break;
}
}while(choice>0 && choice<5);
getch();
}

97
OUTPUT
Static Queue
Enter the size of Queue (max 10) 2
1. Add
2. Delete
3. Display
4. Exit
Enter your choice 1 Enter the value to be added 60
1. Add
2. Delete
3. Display
4. Exit
Enter your choice 1 Enter the value to be added 50
1. Add
2. Delete
3. Display
4. Exit
Enter your choice 2 Deleted value is 60
1. Add
2. Delete
3. Display
4. Exit
Enter your choice 4

98
PRACTICAL 16
DOCUMENTATION:
Name of the program:
Circular Queue
Name of Source File:
CQ.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
insert()
del()
display()
Library header files:
iostream.h
conio.h
process.h

99
ALGORITHM
Insertion
//Assuming that the circular queue is stored in QU with size N.
Checking if Queue already filled or not //
1. if(FRONT=0 AND REAR=N-1) OR (FRONT-REAR+ 1)then
{ print"Overflow!";
}
else
{
2. if FRONT NULL then //QUis initially empty
}
Set FRONT=0
REAR=O
}
3. else if REAR=N-1 then
set REAR=0
else
4. set REAR=REAR+1
} //End of if
5. Set QU[REAR]=I_ITEM //to insert the new item I_ITEM
6. END.

100
Deletion
Assuming that the circular queue is stored in QU with size N.
This algorithm will delete an element from the queue and assign
it to
D-ITEM. */
1. if FRONT =NULL then
print" Underflow! !"
return
{
else
}
2. Set D_ITEM QU[FRONT]
/*Now make FRONT point to the next element in the queue */
3. if FRONT-REAR then
FRONT=NULL
REAR=NULL
{
4. else if FRONT=N-1 then
FRONT=0
else
5. FRONT=FRONT+1
}
6. END.

101
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
int size=10;
void insert(int q[],int ins,int &front, int &rear)
{
if(front==0 && rear==size-1)||(front== rear+1))
}
cout<<"\n Overflow";
getch();
return;
}
else if(rear==-1)
front=rear=0;
else if((rear==size-1))
rear=0;
else
rear++;
q[rear]=ins;
}
void del(int q[],int &front,int &rear)
{
if(front==-1)
102
{
cout<<"\n Queue empty";
getch();
return;
}
else
}
cout"\n The deleted element is"<<q[front];
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;
else
front++;
}
}
void display(int q[],int rear,int front)
{
int i=0;
if(front=-1)
{
cout<<"\n Queue empty";
getch();
return;
}

103
if(rear>-front)
{
for(int i=0;i<rear;++i)
cout<<"-";
cot<<">>>";
for(i=front;i<rear;++i)
cout<<q[i]<<"<"
out<<q[rear]<<"<<<"endl;
}
else
{
for(i=0;i<rear;++i)
cout<<q[i]<<”<-“;
cout<<”>>>”;
for(;i<front;++i)
cout<"-“;
cout<<">>>";
for(i=front;i<size;++i)
cout<<q[i]<<"<";
cout<<"\t...Wrap around";
}
}
void main()
{
clrscr();

104
int ch,q[30], ins, front=-1;
int rear=-1;
cout<<"\n CIRCULAR QUEUE";
cout<<"\n Enter the size of the array(max 30)";
cin>>size;
do
{
cout<<"\n\t\t MENU:"
<<"\n 1.Insertion"
<<“\n 2.Deletion"
<<\n 3.Display"
<<\n 4.Exit";
cout<<"\n\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n Enter the element to be inserted ";
cin>>ins;
insert(q,ins,front, rear);
break;
case 2: del(q,front, rear);
break;
case 3:display(q,rear,front);
break;
case 4: exit(0);

105
default: cout<<'"\n Wrong choice";
getch();
}while((ch>0)& &[ch<5)};
getch();
}

106
OUTPUT
Circular Queue
Enter the size 2
2.Delete
3.Display
4.Exit
Enter your choice 1 Enter the value to be added 60
1. Add
2.Delete
3.Display
4.Exit
Enter your choice 1 Enter the value to be added 50
1.Add
2. Delete
3.Display
4.Exit
Enter your choice 2 Deleted value is 60
1. Add
2.Delete
3.Display
4. Exit
Enter your choice 4

107
PRACTICAL 17
DOCUMENTATION:
Name of the program:
Multiplication of 2 matrices
Name of Source File:
Matrix.cpp
Size of Source File:
2KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
None
Library header files:
iostream.h
conio.h

108
ALGORITHM
1.for i-1 to m do /*Read the first matrix*/
{
2. for j-1 to p do
3. Read A[i,j]
{
4. for i-1 to p do /*Read the Second matrix*/
{
5. for j=1 to p do
6. Read B[i,j]
{
/*Calculate the product*/
7. for i-1 to m do//for C++,0 to m-1 do
8. {
9. for j-1 to q do // for C++,0 to q-1 do
10. { C[i,j]=0
11. for k=1 to n do
12. C[i,j]=C[i,j]+A[i,k]*B[k,j]
}
}

109
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
}
int a[50][50],b[50][50],c[50][50] i,j, m,n,p,q;
clrscr();
cout<<"\n Enter the rows and columns of matrix A"<endl;
cin>>m>>n;
cout<<"\n Enter the rows and columns of matrix B"<<endl;
cin>>p>>q;
if(n==p)
{
cout<<"\n Multiplication of matrix possible";
}
else
{
cout<<"\n Multiplication of matrix not possible";
exit(0);
}
cout<<"\n Enter the elements of matrix A="<<endl;
for(i=0;i<m;++i)
{
110
for(j-0;j<n;++j)
cin>>a[i][i];
}
cout<"\n Enter the elements of matrix B="<sendl;
for(i=0;i<p;++i)
{
for(j=0;j<q;++j)
cin>>b[i][i];
}
cout<<"\n Matrix A is: ";
for(i=0;i<m;++i)
{
cout<<endl;
for(j=0;j<n;++j)
cout<<a[i][j]<" ";
cout<<"\n Matrix B is:";
for(i=0;i<p;++i)
{
cout<<endl;
for (j=0;j<q;++j)
cout<<b[i][j]<" ";
}
cout<<"\n Product of two matrices:"<<endl;
for(i=0;i<m;++i)
{

111
cout<<endl;
for(j=0;j<q;++j)
{
c[i][j]=0;
for(int k=0;k<n;++k)
c[i][i]=c[i][j]+(a[i](k)*b[k][j]);
cout<<c[i][i]<<" ";
}
}
getch();
}

112
OUTPUT
Enter number of rows and columns of matrix A 2 3
Enter number of rows and columns of matrix B 3 2
Multiplication Possible
Enter the elements of matrix A
123
123
Enter the elements of matrix B
12
12
12
Product of two matrices:
6 12
6 12

113
PRACTICAL 18
DOCUMENTATION:
Name of the program:
Reversal of three strings
Name of Source File:
Rev_string.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
getsname()
putdata()
Library header files:
string.h
stdio.h

114
CODING
#include <stdio.h>
#include <string.h>
FILE *fout;
void main()
{
char name[20];
char *getname();/* Fuction prototype */
void switcheroo(char *);/* Fuction prototype */
fout=fopen("a:lab5out.dat","w");
strcpy(name,getname());
while(strcmp("q",name))
{
switcheroo(name);
fprintf(fout,"\n");
strcpy(name,getname());
}
}
char *getname()
{
char name[20];
printf("\n\nEnter in a name to print backwards or 'q' to quit:
");
gets(name);

115
return name;
}
void switcheroo(char *name)
{
int x,i;
x=strlen(name);
for(i=(x-1);i>=0;--i)
{
putchar(name[i]);
fprintf(fout,"%c",name[i]);
}
}

116
OUTPUT
Enter in a name to print backwards or 'q' to quit: ROHIT
TIHOR

117
PRACTICAL 19
DOCUMENTATION:
Name of the program:
Creation of file using objects
Name of Source File:
File_han.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
getdata()
putdata()
Library header files:
iostream.h
fstream.h
conio.h
stdlib.h
stdio.h
process.h

118
ALGORITHM
1. Open the output file in write only mode
2. Repeat step
a) till you want to enter more records
b) Write record to out file
3. Close the out file
4. END

119
CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
class student
{
char name[45];
char grade;
float marks;
public:
void getdata();
void dispdata();
};
void student::getdata()
}
getch();
cout<<"\n Enter name";
gets(name);
Cout<\n Entergrade";
cin>grade;
cout<<"\n Enter marks";
120
cin>>marks;
}
} void student::display()
{
cout<"\n Name:";
puts(name);
cout<<"\n Grade:"<<grade
cout<<"\n Marks:"<<marks<<endl;
}
void main()
{
clrscr()
student ars[3];
fstream f;
f.open("stu",ios:out|ios:in|ios:binary)
if(!f)
{
cout<<"\n File Cannot Be Opened....."
exit(0);
}
cout<<"\n Enter the details for 3 students\n"
<<"\n Press any key to continue....";
for(int i=0;i<3;i++)
arts[i].getdata();
f.write((char*)& arts[i],sizeof(student));

121
}
f.seekg(0);
cout<<"\n The contents of stu.dat are shown below"
for(i=0;i<3;++i)
{
f.read((char *)&arts[i],sizeof(student));
arts[i].display();
{
f.close();
getch();
}

122
OUTPUT
Enter details of 3 students
Enter name: Rohan Mishra
Enter grade: B
Enter marks: 66
Enter name: Amit singh
Enter grade: A
Enter marks: 75
Enter name: Rajat Kapoor
Enter grade: A
Enter marks: 99
The contents of "stu.dat" are shown below
Name: Rohan Mishra grade: B marks: 66
Name: Amit singh grade: A marks: 75
Name: Rajat Kapoor grade: A marks: 99

123
PRACTICAL 20

DOCUMENTATION:
Name of the program:
Function overloading
Name of Source File:
Func_ove.cpp
Size of Source File:
1KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v 3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
amount()
Library header files:
iostream.h
conio.h
stdlib.h
process.h

124
ALGORITHM
Define four functions having
1.Same name but different Arguments
a) void amount(float p, int t, float r)
b) void amount(float p, int t)
c) void amount(float p, float r)
d) void amount(float p)
2. Call the function in main
3. Display the results
4. End

125
CODING
#include<iostream.h
#include<conio.h>
#include<stdlib.h
#include<process.h>
void amount(float princ,int time,float rate) //#1
{
cout<"\n Principal amount:"<<princ;
cout<<”\t Time:"<<time;
cout<<"\t Rate:"<<rate;
cout<"\n Interest amount:"<<{princ*time*rate)<<endl;
}
void amount(float princ, int time) //#2
{
cout<<"\n Principal amount:"<<princ;
cout<<"\t Time:"<<time;
cout<<"\t Rate: 0.08";
cout<<"\n Interest amount:"<<(princ*time*0.08)<<endl;
}
void amount(float princ, float rate) //#3
{
cout<<"\n Principal amount:<<princ;

126
cout<<"\t Time: 2 years";
cout<<"\t Rate: "<<rate;
cout<<"\n Interest amount :"<<(princ*2*rate)<<endl;
}
void amount(int time,float rate)//#4
{
cout<<"\n Principal amount: 2000";
cout<<"\t Time:"<<time;
cout<<"\t Rate: "<<rate;
cout<<"\n Interest amount"<<(2000* time*rate)<<endl;
}
void amount(float princ) //#5
{
cout<<"\n Principal amount:"<<princ;
cout<<"\t Time: 2 years";
cout<<"\t Rate: 0.08";
cout<<"\n Interest mount :"<<(princ*2* (0.08))<<endl;
}
void main()
{
clrscr();
cout<<"\n Case 1:";
amount(2000.0F);
cout<<"\n Case 2:";
amount(2500.0F,3);

127
cout<<"\n Case 3:";
amount(2300.0F3,0.11F);
cout<<"\n Case 4:";
amount(2,0.12F);
cout<<"\n Case 5:";
amount(6,0.07F);
getch();
}

128
OUTPUT
Case 1:
Principal amount: 2000 Time: 2 years Rate: 0.08
Interest amount: 320
Case 2:
Principal amount: 2500 Time: 3 years Rate: 0.08
Interest amount: 600
Case 3:
Principal amount: 2300 Time: 3 years Rate: 0.11
Interest amount: 759
Case 4:
Principal amount: 2000 Time: 2 years Rate: 0.12
Interest amount: 480
Case 5:
Principal amount: 2000 Time: 6 years Rate: 0.07
Interest amount: 840

129
PROGRAM 21
DOCUMENTATION:
Name of the program:
Constructor overloading
Name of Source File:
Constructor.cpp
Size of Source File:
2 KB
OS Used:
Microsoft Windows 7
Version of C++:
Turbo C++ v3.0 with DOSBOX Emulator 2.0
Name of Functions Used:
deposit()
calc_amt()
display()
Library header files:
iostream.h
conio.h
stdlib.h
process.h

130
ALGORITHM
Definition of class deposit
A) In public part give definitions of
4 constructors taking no arguments,
3 arguments and 2 arguments
B) Definition of 2 function
calc()-calculating amount
disp()-for displaying calculated values
2. Implicit calling of constructor
3. Calling function calculate
4. Calling function display
5. END

131
CODING
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
class deposit
{
long int princ;
int time;
float rate;
float total_amt;
public:
deposit();
deposit(long p, int t, float r);
deposit(long p, int t);
deposit(long p, float r);
void calc_amt();
void display();
};
deposit::deposit(long p, int t, float r)
{
princ=p;
time= t;
rate=r;
132
}
deposit::deposit ()
{
princ=time=rate=0;
}
deposit::deposit(long p, int t)
{
princ=p;
time-t;
rate=8;
{
deposit::deposit(long p, float r)
}
Princ=p;
time=2;
rate=r;
}
void deposit::calc_amt()
{
total_amt-princ+(princ* rate * time)/100;
}
void deposit::display
{
cout<<"\n Principal amount: Rs."<<princ;
cout<<"\n Period of investment:"<<time<<"years";

133
cout<<\n Rate of investment:"<<rate;
cout<<"\n Total amount: Rs."<<total_amt<<endl;
}
void main()
{
clrscr();
deposit d1,d2(2000, 2,0.00 7f),d3 (4000, 1), d4(3000,0.12f);
d1.calc_amt();
d2.calc_amt();
d3.calc_amt();
d4.calc_amt();
cout<<"\n Object 1\n";
d1.display();
cout<<"\n Object 2\n";
d2.display);
cout<<"\n Object 3 n";
d3.display();
cout<<"\n Object 4\n";
d4.display();
getch();
}

134
OUTPUT
Object 1
Principal amount: Rs. O Period of investment: O year
Rate of interest: 0 Total amount: Rs.0
Object 2
Principal amount: Rs. 2000 Period of investment: 2 year
Rate of interest: 0.07 Total amount: Rs228.0
Object 3
Principal amount: Rs. 4000 Period of investment: 1 year
Rate of interest: 0.08 Total amount: Rs.4320
Object 4
Principal amount: Rs. 3000 Period of investment: 2 year
Rate of interest: 0.12 Total amount: Rs.3720

135
PRACTICAL 22

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class EMPLOYEE
{
private:
char name[30];
unsigned long enumb;
public:
void getdata()
{
cout<<"Enter name: ";
gets(name);
cout<<"Enter Employee Number: ";
cin>>enumb;
}
void putdata()
{
cout<<"Name: "<<name<<"\t";
cout<<"Emp. No: "<<enumb<<"\t";
cout<<"Basic Salary: "<<basic;

136
}
protected:
float basic;
void getbasic()
{
cout<<"Enter Basic: ";
cin>>basic;
}
};
class MANAGER:public EMPLOYEE
{
private:
char title[30];
public:
void getdata()
{
EMPLOYEE::getdata();
getbasic();
cout<<"Enter Title: ";
gets(title);
}
void putdata()
{
EMPLOYEE::putdata();
cout<<"\tTitle: "<<title<<"\n";

137
}
};
void main()
{
clrscr();
MANAGER m1, m2;
cout<<"Manager 1\n";
m1.getdata();
cout<<"\nManager 2\n";
m2.getdata();
cout<<"\n\t\tManager 1 Details\n";
m1.putdata();
cout<<"\n\t\tManager 2 Details\n";
m2.putdata();
getch();
}

138
OUTPUT
Manager1
Enter Name Vaidehi sharma
Enter Employee Number 5192
Enter Basic : 12700
Enter Title : Mrs.
Manager2
Enter Name Irshad Khan
Enter Employee Number 3222
Enter Basic: 11000
Enter Title : Mr.
Manager1 Details
Name: Vaidehi Sharma Emp. Number: 5192 Basic Salary : 12700 Title : Mrs.

Manager2 Details
Name: Irshad Khan Emp. Number: 3222 Basic Salary 11000 Title Mr.

139
PRACTICAL 23
DOCUMENTATION
NAME OF PROGRAM: CLASS.CPP
SORCE CODE: ILLUSTRATION OF PUBLIC DERIVATION
SIZE OF THE PROGRAM: 1 Kb
OPERATING SYSTEM USED: WINDOWS7\ WINDOWS10
C++ VERSION: 4.0
NAME OF FUNCTION: N.A

140
CODING
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int LEN=25;
class Employee
{
private:
char name[10];
unsigned long enumb;
public:
void getdata()
{
cout<<"Enter name:";
gets(name);
cout<<" Enter Employee Number:";
cin>>enumb;
}
void putdata()
{
cout<<"Name;" <<name<<"\t";
cout<<"Emp.Number:"<<enumb<<"\t";
cout<<"Basic salary:<<basic;
}
141
protected:
float basic;
void getbasic()
{
cout<<" Enter basic:";cin>>basic;
}
};
class manager:public Employee
{
private:
char title[LEN];
public:
void getdata()
{
Employee::getdata ();
getbasic();
cout<< Enter Title:";
gets(title);cout<<"\n";
}
void putdata()
{
Employee::putdata ();
cout<<"\t Title;"<<title<<"\n";
}
};

142
int main()
clrscr();
manager m1,m2;
cout<<"Manager1\n";m1.getdata ();
cout<"Manager2\n"m2.getdata();
cout<"\t\tManager1 details\n";m1. putdata ( );
cout<<"\t\tManager 2 details\n";m2.putdata();
getch();
return 0;
}

143
PRACTICAL 24
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
class Student
{
char name[40]:
char grade;
float marks;
public:
void getdata(void);
void display(void);
};
void Student :: getdata(void)
{ char ch;
cin.get(ch) ;
cout <<”Enter name:”;
cin.getline(name, 40);
cout <<"Enter grade:”;
cin >> grade ;
cout <<Enter marks :";
cin >> marks;
cout<<”\n”;
}
144
void Student :: display(void)
{ cout <<" Name:"<< name <"t"
<" Grade: " << grade << " \t "
<<"Marks : " <<marks << " \t "<<" \n ";
}
int main()
{ system (" cls ");
Student arts[3]i
fstream filin;
filin.open(" Stu.dat ", ios :: in||ios :: out);
if (!filin)
{ cout <<" Cannot open file!! \n ";
return 1;
}
cout << "Enter details for 3 students In "
for(int i 0; i< 3; i++)
{ arts[il.getdata();
filin.write((char *) & arts[i], sizeof (arts[i]));
}
filin.seekg(0);
cout <<"The contents of stu.dat are shown below. \n ";
for(i=0;i<3; i++)
{
filin.read((char *) & arts[i], sizeof (arts[i]));
arts[i].display() ;

145
}
filin.close()
return 0;
}

146
OUTPUT
Enter details for 3 students
Enter name : Darpan Aggarwal
Enter grade :B
Enter marks :66

Enter name :Manit Kaur


Enter grade :A
Enter marks :75

Enter name :Zeba Khan


Enter grade :B
Enter marks :67

The contents of stu.dat are shown below.


Name: Darpan Aggarwal Grade: B Marks: 66
Name: Manit Kaur Grade: A Marks: 75
Name: Zeba Khan Grade: B Marks: 67

BIBLOGRAPHY
147
I. Computer Science with C++ class 11
-Sumita Arora
II. Computer Science with C++ Class 12
-Sumita Arora
III. Computer Science with C++ Class 11
-A.K Sharma
IV. Computer Science with C++ Class 12
-A.K Sharma
V. Turbo C++ by Robert Laufore
VI. www.pscode.com

148

Potrebbero piacerti anche