You are on page 1of 74

COMPUTER SCIENCE

PRACTICAL FILE

2016-2017

NAME
: SAHIL SINGH
CLASS
: XII A
ROLL NO. : 21
KENDRIYA VIDYALAYA PRAGATI VIHAR

INDEX
1

Sl. no.
I.

III.

V.

VII.

IX.

XI.

XIII.

XV.

Name
Function Overloading
1. Program to calculate interest amount
using Function Overloading.
Class and Object
2. Program to illustrate the concept of
class and object.
Constructor and Destructor
3. Program to illustrate the concept of
constructor and destructor.
Inheritance: Extending Classes
4. Program to illustrate the concept of
Multiple Inheritance.
Data File Handling
5. Program to count the word is as an
independent word in the text file
Dialogues.txt.
6. Program to copy vowels from one file
to another.
7. Program to copy only the specific
contents from one file two another.
Pointer
8. Program to illustrate the passing of
pointers as arguments in a function.
Arrays
9. Program to search an element from an
array using binary search method.
10. Program to insert an element in an
array.
11. Program to delete an element from an
array.
12. Program to sort the elements of an
array using Selection Sort.
13. Program to sort the elements of an
array using Bubble Sort.
14. Program to sort the elements of an
array using Insertion Sort.
15. Program to sort the elements of an
array using merge sort. [1st array if in
ascending order, 2nd array is in
descending order and the resultant
array should be in ascending order.]
Linked Lists
16. Program for Insertion in the end of a
List.
17. Program for Deletion from the
beginning of a List.
18. Program for Pushing in an Array-

Page No.

T. Sign

10

12

14
16

19

20

22
24
26

28
30

32

35

37
40

PROGRAM 01
//Program to calculate interest amount using Function Overloading
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void amount (float princ, int time, float rate)
{
cout<< \n Principal Amount:<<princ;
cout<< \t Time:<<time;
cout<< \t Rate:<<rate;
cout<< \n Simple Interest :<<(princ*time*rate);
}
void amount (float princ, int time)
{
cout<< \n Principal Amount:<<princ;
cout<< \t Time:<<time;
cout<< \t Rate: 0.08 ;
cout<< \n Simple Interest :<<(princ*time*0.08);
}
void amount (float princ, float rate)
{
cout<< \n Principal Amount:<<princ;
cout<< \t Time: 2 years;
cout<< \t Rate:<<rate;
cout<< \n Simple Interest :<<(princ*2 *rate);
}
void amQount (int time, float rate)
{
cout<< \n Principal Amount: 2000;
cout<< \t Time:<<time;
cout<< \t Rate:<<rate;
cout<< \n Simple Interest :<<(2000*time*rate);
}
void amount (float princ)
{
cout<< \n Principal Amount:<<princ;
cout<< \t Time: 2 years;
cout<< \t Rate: 0.06;
cout<< \n Simple Interest :<<(princ*2 *0.06);
}
void main()
3

{
clrscr();
cout<< \n Case 1:;
amount (1000);
cout<< \n Case 2:;
amount (3000.0, 3);
cout<< \n Case 3:;
amount (2300, 4, 0.11);
cout<< \n Case 4:;
amount (2,0.15);
cout<< \n Case 5:;
amount(7,0.07);
}

OUTPUT

PROGRAM 2
// Program to illustrate the concept of class and object
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<stdio.h>
class Flight
{
private:
int Fno;
char desig[30];
float distance;
float fuel;
void Calfuel();
public:
void Feed_Info();
void Show_Info ();
};
void Flight :: Calfuel()
{
if(distance<=1000)
fuel=500;
if(distance>1000 && distance<=2000)
fuel=1000;
if(distance>2000)
fuel=2200;
}
void Flight :: Feed_Info()
{
cout<<"\n Enter the flight no. :";
cin>>Fno;
cout<<"\n Enter Designation:";
gets(desig);
cout<<"\n Enter the distance:";
cin>>distance;
Calfuel();
}
void Flight :: Show_Info()
{
5

cout<< "\n Flight no. :"<<Fno;


cout<< "\n Designation:";
puts(desig);
cout<< "\n Distance:"<<distance;
cout<< "\n Fuel: "<<fuel;
}
void main()
{
Flight F;
F.Feed_Info();
F.Show_Info();
}

OUTPUT

PROGRAM 3
// Program to illustrate the concept of constructor and destructor
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
class Clothing
{
char Code[15];
char Type[15];
int size;
char Material[20];
float Price;
void calc_price()
{
if(strcmp(Type, TROUSER)==0)
Price=1500;
if(strcmp(Type, SHIRT)==0)
Price=1200;
if(strcmp(Material, COTTON)!=0)
Price=Price-(Price*0.25);
}
public:
Clothing( )
{
strcpy(Code, NOT ASSIGNED );
strcpy(Type, NOT ASSIGNED );
strcpy(Material, NOT ASSIGNED);
size=0;
Price=0.0;
}
void Enter( )
{
cout<< \n Enter the code :;
gets(Code);
cout<< \n Enter the Type: ;
gets(Type);
cout<< \n Enter the Material :;
gets(Material);
7

cout<< \n Enter the size :;


cin>>size;
calc_price();
}
void Show( )
{
cout<< \n Code :;
puts(Code);
cout<< \n Type: ;
puts(Type);
cout<< \n Material :;
puts(Material);
cout<< \n Size :<<size;
cout<< \n Price:<<Price;
}
};
void main( )
{
Clothing C;
C.Enter( );
C.Show( );
}

OUTPUT

PROGRAM 4
//Program to illustrate the concept of Multiple Inheritance.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int len=35;
class Employee
{
char name[len];
unsigned long enumb;
public:
void getdata( )
{
cout<<"\n Enter Name : ";
cin>>name;
cout<<"\n Enter Employee Number : ";
cin>>enumb;
}
void putdata( )
{
cout<<"\n Name: ";
puts(name);
cout<<"\t Employee number : "<<enumb;
cout<<"\t Basic Salary: "<<basic;
}
Protected:
float basic;
void getbasic( )
{
cout<<"\n Enter Basic: ";
cin>>basic;
}
};
class Manager : private Employee
{
char title[len];
public:
void getdata()
{
Employee::getdata();
getbasic();
9

cout<<"\n Enter Title : ";


cin>>title;
}
void putdata()
{
Employee :: putdata();
cout<<"\t Title: ";
puts(title);
}
};
int main()
{
Manager m1,m2;
cout<<"Manager 1: ";
m1.getdata();
cout<<"\n Manager 2 : ";
m2.getdata();
cout<<"\n Manager 1 Details : ";
m1.putdata();
cout<<"\n Manager 2 Details : ";
m2.putdata();
return 0;
}

OUTPUT

10

PROGRAM 05
/*Program to count the word is as an independent word in the text file
Dialogues.txt.*/
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char ch[1000];
int count=0;
ofstream fout ("Dialouge.txt");
cout<<"\n Enter data to write in the file Dialouge.txt : ";
gets(ch);
fout<<ch<<" ";
fout.close();
ifstream fin ("Dialouge.txt");
if(!fin)
while (fin)
{
fin>>ch;
if(strcmp(ch,"is")==0||strcmp(ch,"Is")==0 || strcmp(ch,"IS")==0)
count++;
}
fin.close();
cout<<"\nThe word 'is' as an independent word in the text file 'Dialouge.txt':";
cout<<count;
}

11

OUTPUT

PROGRAM 6
12

//Program to copy vowels from one file to another.


#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
char ch[1000];
char ch1;
ofstream fout("text1.txt");
cout<<"\nEnter data to wrrite in the file 'text1.txt': \n";
gets(ch);
fout<<ch<<" ";
fout.close();
ofstream fo("text2.txt");
ifstream fin("text1.txt");
if(!fin)
{
cout<<"\n Invalid File";
exit(0);
}
cout<<"\n\n Data in file 'text2.txt': \n ";
while (fin)
{
fin>>ch1;
if(ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U'||ch1=='a'||ch1=='e'||ch1=='i'||
ch1=='o'||ch1=='u')
{
fo<<ch1<<" ";
cout<<ch1<<" ";
}
}
fin.close();
fo.close();
}

OUTPUT
13

PROGRAM 07
14

/*Program to copy only the specific contents from one file two another.*/
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
struct Sports
{
char Event[20];
char Participant[10][30];
};
Sports S;
void create()
{
fstream afile;
afile.open("SPORTS.DAT", ios::out);
int n;
cout<<"\n Enter how many records: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n Enter thr Event Name ";
gets(S.Event);
for(int j=0;j<3;j++)
{
cout<<"\n Enter the Participants ";
gets(S.Participant[j]);
}
afile.write((char*)&S,sizeof(Sports));
}
afile.close( );
}
void copy( )
{
fstream afile, bfile;
afile.open("SPORTS.DAT",ios::in);
bfile.open("ATHLETIC.DAT",ios::out);
while(afile)
{
afile.read((char*)&S,sizeof(Sports));
if(strcmp(S.Event,"Athletics")==0)
15

{
cout<<"\n Record Found";
bfile.write((char*)&S, sizeof(Sports));
}
}
afile.close();
bfile.close();
}
void Display()
{
fstream bfile;
bfile.open("ATHLETICS.DAT",ios::in);
while(bfile)
{
bfile.read((char*)&S,sizeof(Sports));
for(int j=0;j<10;j++)
puts(S.Participant[j]);
}
bfile.close();
}
int main()
{
create();
copy();
cout<<"Content of copied file\n";
Display();
return 0;
}

OUTPUT
16

17

PROGRAM 08
/*Program to illustrate the passing of pointers as arguments in a function.*/
#include<iostream.h>
#include<conio.h>
void main()
{
void swap(int *x, int *y);
int a=7, b=10;
cout<<"\nOriginal Value ";
cout<<" a="<<a<<" ,b="<<b<<"\n";
swap(&a,&b);
cout<<" Swapped Values \n";
cout<<"a="<<a<<" ,b="<<b<<"\n";
}
void swap(int *x, int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

OUTPUT

18

PROGRAM 09
/*Program to search an element from an array using binary search method.*/
#include<iostream.h>
#include<conio.h>
int Bsearch(int [], int , int);
void main()
{
clrscr();
int AR[50], item, n, index;
cout<<"\n Enter desired array size: ";
cin>>n;
cout<<"\n Enter Array elements (ascending order):";
cout<<"\n";
for(int i=0;i<n;i++)
cin>>AR[i];
cout<<"\n Enter element to be searched for..... ";
cin>>item;
index=Bsearch(AR,n,item);
if(index==-1)
cout<<"\n Sorry !! Given element could not be found\n";
else
{
cout<<"\n Element found at index: "<<index;
cout<<"\n Position : "<<(index+1);
}
}
int Bsearch(int AR[], int size, int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==AR[mid]) return mid;
else if(item>AR[mid]) beg=mid+1;
else last=mid-1;
}
return -1;
}
19

OUTPUT

20

PROGRAM 10
//Program to insert an element in an array.
#include<iostream.h>
#include<process.h>
#include<conio.h>
int findpos(int a[], int s, int item)
{
int pos;
if(item<a[0])
pos=0;
else
{
for(int i=0;i<s-1;i++)
{
if(a[i]<=item && item<a[i+1])
{
pos=i+1;
break;
}
}
if (i==s-1)
pos=s;
}
return pos;
}
void main()
{
clrscr();
int AR[50],item,N,index;
cout<<"\n Enter the size of the array: ";
cin>>N;
cout<<"\n Enter the array elements (ascending order):\n";
for(int i=0;i<N;i++)
cin>>AR[i];
char ch='y';
while (ch=='y' || ch=='Y')
{
cout<<"\nEnter the elements to be inserted: ";
cin>>item;
if(N==50)
21

{
cout<<"\n Overflow!";
exit(1);
}
index=findpos(AR,N,item);
for(i=N;i>index;i--)
AR[i]=AR[i-1];
AR[index]=item;
N=N+1;
cout<<"\nWant to insert more elements? (y/n)...";
cin>>ch;
}
cout<<"The array is...\n";
for(i=0;i<N;i++)
cout<<AR[i]<<" ";
cout<<endl;
}

OUTPUT

22

PROGRAM 11
//Program to delete an element from an array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int a[20],item,n,pos=-1;
char ch='y';
cout<<"\n Enter size of array(max.20): ";
cin>>n;
cout<<"\n Enter array elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
a:cout<<"\n Enter element to be deleted: ";
cin>>item;
if(n==0)
{
cout<<"\n Underflow";
exit(1);
}
for(i=0;i<n;i++)
if(a[i]==item)
pos=i;
if(pos!=-1)
a[pos]=0;
else
cout<<"\n Sorry!! no such element found";
for(i=pos;i<n;i++)
{
a[i]=a[i+1];
}
n-=1;
cout<<"\n Want to delete more elements? (y/n)...";
cin>>ch;
if(ch=='y' || ch=='Y')
goto a;
cout<<"\n The resultant array is...\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
23

cout<<endl;
getch();
}

OUTPUT

24

PROGRAM 12
//Program to sort the elements of an array using Selection Sort.
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void SelSort(int[],int);
void main()
{
clrscr();
int AR[50],n;
cout<<"\n How many elements do you want to create array with (max 50)... ";
cin>>n;
cout<<"\n Enter array elements...";
for(int i=0;i<n;i++)
{
cout<<" ";
cin>>AR[i];
}
SelSort(AR,n);
cout<<"\n\n The sorted array is shown below\n";
for(i=0;i<n;i++)
cout<<" "<<AR[i];
}
void SelSort(int AR[], int size)
{
int small,pos,temp;
for(int i=0;i<size;i++)
{
small=AR[i];
for(int j=i+1;j<size;j++)
{
if(AR[j]<small)
{
small=AR[j]; pos=j;
temp=AR[i];
AR[i]=AR[pos];
AR[pos]=temp;
}
}
}
25

OUTPUT

26

PROGRAM 13
//Program to sort the elements of an array using Bubble Sort.
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void BubbleSort(int [], int );
void main()
{
clrscr();
int AR[50],n;
cout<<"\n How many elements do you want to create array with (max 50)... ";
cin>>n;
cout<<"\n Enter array elements...";
for(int i=0;i<n;i++)
{
cout<<" ";
cin>>AR[i];
}
BubbleSort(AR,n);
cout<<"\n\n The sorted array is shown below\n";
for(i=0;i<n;i++)
cout<<" "<<AR[i];
}
void BubbleSort(int AR[], int size)
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{
if(AR[j]>AR[j+1])
{
temp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=temp;
}
}
}
}

27

OUTPUT

28

PROGRAM 14
//Program to sort the elements of an array using Insertion Sort.
#include<iostream.h>
#include<limits.h>
void InsSort(int [],int );
void main()
{
int ar[50],n;
cout<<"Enter the Number of Elements of Array to be Inserted\n";
cin>>n;
cout<<"Enter Array Elements\n";
for(int i=1;i<=n;i++)
cin>>ar[i];
InsSort(ar,n);
cout<<"\n\nThe sorted array is\n";
for(i=1;i<=n;i++)
cout<<ar[i]<<" ";
void InsSort(int ar[],int size)
{
int tmp,j;
ar[0]=INT_MIN;
for(int i=1;i<=size;i++)
{
tmp=ar[i];
j=i-1;
while(tmp<ar[j])
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=tmp;
}
}
}

29

OUTPUT

30

PROGRAM 15
/*Program to sort the elements of an array using merge sort. [1st array if in
ascending order, 2nd array is in descending order and the resultant array should
be in ascending order.]*/
#include <iostream.h>
void merge(int [], int, int [], int, int []);
void main()
{
int a[100], b[100], m, n, c, sorted[200];
cout<<"Input number of elements in first array\n";
cin>>m;
cout<<"Input Elelemts";
for (c = 0; c < m; c++)
{
cin>>a[c];
}
cout<<"Input number of elements in second array\n";
cin>>n;
cout<<"Input Elelemts\n";
for (c = 0; c < n; c++)
{
cin>>b[c];
}
merge(a, m, b, n, sorted);
cout<<"Sorted array:\n";
for (c = 0; c < m + n; c++)
{
cout<<endl<<sorted[c];
}
}
void merge(int a[], int m, int b[], int n, int sorted[])
{
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;)
{
if (j < m && k < n)
{
if (a[j] < b[k])
{
31

sorted[i] = a[j];
j++;
}
else
{
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = b[k];
k++;
i++;
}
}
else
{
for (; i < m + n;)
{
sorted[i] = a[j];
j++;
i++;
}
}
}
}

32

OUTPUT

33

PROGRAM 16
//Program for Insertion in the end of a List.
#include<iostream.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}*front,*newptr,*save,*ptr,*rear;
Node*create_new_node(int);
void insert(Node*);
void display(Node*);
void main()
{
clrscr();
front=rear=NULL;
int inf;char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter the information for the new node: ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"Cannot create new node!!Aborting!!!";
exit(1);
}
insert(newptr);
cout<<"\n Press Y to enter more nodes, N to exit: ";
cin>>ch;
cout<<"\n The linked queue now is...\n";
display(front);
}
}
Node*create_new_node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
34

return ptr;
}
void insert(Node*np)
{
if(front==NULL)
front=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void display(Node*np)
{
while (np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
}

OUTPUT

35

PROGRAM 17
//Program for Deletion from the beginning of a List.
#include<iostream.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}*start,*newptr,*save,*ptr,*rear;
Node*create_new_node(int);
void insert(Node*);
void display(Node*);
void delnode();
void main()
{
clrscr();
start=rear=NULL;
int inf;char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter the information for the new node: ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"Cannot create new node!!Aborting!!!";
exit(1);
}
insert(newptr);
cout<<"\n Press Y to enter more nodes, N to exit: ";
cin>>ch;
}
do
{
cout<<"\n The list now is...\n";
display(start);
cout<<"\n Want to delete first node(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
delnode();
36

}while(ch=='y'||ch=='Y');
}
Node*create_new_node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert(Node*np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void delnode()
{
if(start==NULL)
cout<<"!!!!!UNDERFLOW!!!!!";
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
void display(Node*np)
{
while (np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
}

37

OUTPUT

38

PROGRAM 18
//Program for Pushing in an Array-Stack.
#include<iostream.h>
#include<process.h>
#include<conio.h>
void insertion();
void display();
const int size=50;
int stack[size], top=-1;
void main()
{
clrscr();
int n;
char ch;
a:cout<<"\n 1 for insertion \t 2 for display \t 3 for exit\n";
cin>>n;
switch(n)
{
case 1:insertion();
break;
case 2:display();
break;
case 3:exit(0);
default: cout<<"\n Wrong choice";
}
cout<<"\n Want more??(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}
void insertion()
{
int ele;
if(top==size-1)
{
cout<<"\n FULL!!\n";
exit(0);
}
cout<<"Enter Element...";
cin>>ele;
39

++top;
stack[top]=ele;
}
void display()
{
if(top==-1)
{
cout<<"EMPTY!!\n";
exit(0);
}
else
{
for(int i=top;i>=0;i--)
cout<<stack[i]<<"\t";
}
}

OUTPUT

40

PROGRAM 19
//Program for Popping from an Array-Stack.
#include<iostream.h>
#include<process.h>
#include<conio.h>
void insertion();
void deletion();
void display();
const int size=50;
int stack[size],top=-1;
void main()
{
clrscr();
int n;
char ch;
a:cout<<"\n 1 for insertion \t 2 for deletion \t 3 for display \t 4 for
exit\n";
cin>>n;
switch(n)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: exit(0);
default: cout<<"\n Wrong Choice!";
}
cout<<"\n Want more?? (y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}
void insertion()
{
int ele;
if(top==size-1)
{
cout<<"\n FULL!";
41

exit(0);
}
cout<<"\n Enter Element...";
cin>>ele;
++top;
stack[top]=ele;
}
void deletion()
{
int ele;
if(top==-1)
{
cout<<"\n EMPTY!";
exit(0);
}
ele=stack[top];
--top;
cout<<"\n Deleted item is "<<ele;
}
void display()
{
if(top==-1)
{
cout<<"\n EMPTY";
exit(0);
}
else
{
for(int i=top;i>=0;i--)
cout<<" "<<stack[i];
}
}

42

OUTPUT

43

PROGRAM 20
//Program for Pushing in Linked-Stack.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
struct node
{
int roll;
char name[20];
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void display();
};
void stack::push()
{
int r;
char n[20];
node *temp;
temp=new node;
cout<<" Enter Roll no. and Name\n";
cout<<" Roll no.: ";
cin>>r;
cout<<"\n Name: ";
gets(n);
temp->roll=r;
strcpy(temp->name,n);
temp->next=NULL;
if(top==NULL)
top=temp;
else
44

{
temp->next=top;
top=temp;
}
}
void stack::display()
{
node *temp;
if(top==NULL)
{
cout<<"EMPTY!";
exit(0);
}
else
{
temp=top;
while(temp!=NULL)
{
cout<<temp->roll<<"\t"<<temp->name<<"\n";
temp=temp->next;
}
}
}
void main()
{
clrscr();
char ch;
int n;
stack s;
a:cout<<" Enter your choice\n";
cout<<" 1 for push \t 2 for display \t 3 for exit\n";
cin>>n;
switch(n)
{
case 1: s.push();
break;
case 2: s.display();
break;
case 3: exit(0);
break;
default: cout<<"WRONG CHOICE!";
45

}
cout<<"Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}

OUTPUT

46

PROGRAM 21
//Program for Popping from Linked-Stack.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
struct node
{
int roll;
char name[20];
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};
void stack::push()
{
int r;
char n[20];
node *temp;
temp=new node;
cout<<" Enter Roll no. and Name\n";
cout<<" Roll no.: ";
cin>>r;
cout<<" Name: ";
gets(n);
temp->roll=r;
strcpy(temp->name,n);
temp->next=NULL;
if(top==NULL)
top=temp;
47

else
{
temp->next=top;
top=temp;
}
}
void stack::pop()
{
node *temp;
if(top==NULL)
{
cout<<"EMPTY!!";
exit(0);
}
else
{
temp=top;
top=top->next;
cout<<"Deleted items\n";
cout<<temp->roll<<"\t"<<temp->name<<"\n";
delete temp;
}
}
void stack::display()
{
node *temp;
if(top==NULL)
{
cout<<"EMPTY!";
exit(0);
}
else
{
temp=top;
while(temp!=NULL)
{
cout<<temp->roll<<"\t"<<temp->name<<"\n";
temp=temp->next;
}
}
}

48

void main()
{
clrscr();
char ch;
int n;
stack s;
a:cout<<" Enter your choice\n";
cout<<" 1 for push \t 2 for pop \t 3 for display \t 4 for exit\n";
cin>>n;
switch(n)
{
case 1: s.push();
break;
case 2: s.pop();
break;
case 3: s.display();
break;
case 4: exit(0);
break;
default: cout<<"WRONG CHOICE!";
}
cout<<"Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}

OUTPUT

49

PROGRAM 22
//Program for Insertion in an Array-Queue.
#include<iostream.h>
#include<process.h>
#include<conio.h>
void insertion();
void display();
const int size=50;
int Q[size],front=-1,rear=-1;
void main()
{
clrscr();
int n;
char ch;
a:cout<<" 1 for insertion \t 2 for display \t 3 for exit\n";
cin>>n;
switch(n)
{
case 1: insertion();
break;
case 2: display();
break;
case 3: exit(0);
break;
default:cout<<"\n WRONG CHOICE!";
}
cout<<" Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}
void insertion()
{
int ele;
if(rear==size-1)
{
cout<<" FULL! ";
exit(0);
}
cout<<" Enter Element...";
50

cin>>ele;
if(front==-1)
{
front=0;
rear=0;
Q[rear]=ele;
}
else
{
rear++;
Q[rear]=ele;
}
}
void display()
{
if(front==-1)
{
cout<<" EMPTY";
exit(0);
}
else
{
for (int i=front;i<=rear;i++)
cout<<" "<<Q[i];
}
}

51

OUTPUT

52

PROGRAM 23
//Program for Deletion from an Array-Queue.
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<stdlib.h>
void insertion();
void deletion();
void display();
const int size=50;
int Q[size],front=-1,rear=-1;
void main()
{
clrscr();
int n;
char ch;
a:cout<<" 1 for insertion\t 2 for deletion\t 3 for display\t 4 for exit\n";
cin>>n;
switch(n)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: exit(0);
default: cout<<"WRONG CHOICE";
}
cout<<"\n Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}
void insertion()
{
int ele;
if(rear==size-1)
{
cout<<"FULL!";
53

exit(0);
}
cout<<"Enter element...";
cin>>ele;
if(front==-1)
{
front=0;
rear=0;
Q[rear]=ele;
}
else
{
rear++;
Q[rear]=ele;
}
}
void deletion()
{
int ele;
if(front==-1)
{
cout<<"EMPTY!!";
exit(0);
}
else if (front==rear+1)
{
ele=Q[front];
front++;
cout<<"\n Deleted Element..."<<ele;
}
else if(front==rear)
{
ele=Q[front];
front=rear=-1;
cout<<"\n Deleted Element..."<<ele;
}
}
void display()
{
if(front==-1)
{
54

cout<<" EMPTY!!!";
exit(0);
}
else
{
for(int i=front;i<=rear;i++)
cout<<" "<<Q[i];
}
}

OUTPUT

55

PROGRAM 24
//Program for Insertion in a Linked-Queue.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
struct node
{
int roll;
char name[20];
node *next;
};
class queue
{
node *rear,*front;
public:
queue()
{
front=NULL;
rear=NULL;
}
void insertion();
void display();
};
void queue::insertion()
{
node *temp;
temp=new node;
cout<<"Enter roll no. and name\n";
cout<<"ROLL No.:";
cin>>temp->roll;
cout<<"Name:";
gets(temp->name);
temp->next=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
}
56

else
{
rear->next=temp;
rear=temp;
}
}
void queue::display()
{
node *temp;
if(front==NULL)
{
cout<<"EMPTY!";
exit(0);
}
else
{
temp=front;
while(temp!=NULL)
{
cout<<temp->roll<<"\t"<<temp>name<<"\n";
temp=temp->next;
}
}
}
void main()
{
clrscr();
char ch;
int n;
queue q;
a:cout<<"Enter your choice\n";
cout<<" 1 for insertion\t 2 for display \t 3 for exit\n";
cin>>n;
switch(n)
{
case 1: q.insertion();
break;
case 2: q.display();
break;
case 3: exit(0);
57

default: cout<<"WRONG CHOICE!!";


}
cout<<"Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}

OUTPUT

58

PROGRAM 25
//Program for Deletion from a Linked-Queue.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
struct node
{
int roll;
char name[20];
node *next;
};
class queue
{
node *rear,*front;
public:
queue()
{
front=NULL;
rear=NULL;
}
void insertion();
void deletion();
void display();
};
void queue::insertion()
{
node *temp;
temp=new node;
cout<<"Enter roll no.and name\n";
cout<<"Roll No.: ";
cin>>temp->roll;
cout<<"Name: ";
gets(temp->name);
temp->next=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
59

}
else
{
rear->next=temp;
rear=temp;
}
}
void queue::deletion()
{
node *temp;
if(front==NULL)
{
cout<<"UNDERFLOW!!";
exit(0);
}
else
{
temp=front;
front=front->next;
delete temp;
cout<<"Deleted..."<<temp->roll<<" "<<temp>name<<"\n";
}
}
void queue::display()
{
node *temp;
if(front==NULL)
{
cout<<"EMPTY!";
exit(0);
}
else
{
temp=front;
while(temp!=NULL)
{
cout<<temp->roll<<"\t"<<temp>name<<"\n";
temp=temp->next;
}
60

}
}
void main()
{
clrscr();
char ch;
int n;
queue q;
a:cout<<"Enter your choice\n";
cout<<" 1 for insertion\t 2 for deletion\t 3for display\t 4 for exit\n";
cin>>n;
switch(n)
{
case 1:q.insertion();
break;
case 2:q.deletion();
break;
case 3:q.display();
break;
case 4: exit(0);
break;
default: cout<<"WRONG CHOICE";
}
cout<<"WANT MORE?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}

61

OUTPUT

62

PROGRAM 26
//Program for Insertion and Deletion in a Circular Queue.
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int s=5;
int front=-1,rear=-1,Q[s];
void insertion();
void deletion();
void display();
void main()
{
clrscr();
int n;
char ch;
a:cout<<" Enter your choice\n";
cout<<" 1 for insertion\t 2 for deletion\t 3 for display\t 4 for exit\n";
cin>>n;
switch(n)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: cout<<"WRONG CHOICE";
}
cout<<" Want more?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')
goto a;
}
void insertion()
{
int ele;
cout<<"Enter an element:";
cin>>ele;
63

if((front==0 && rear==s-1)|| rear==front-1)


cout<<"\nFULL!";
else if (rear==-1)
{
rear=0;
front=0;
Q[rear]=ele;
}
}
void deletion()
{
int ele;
if(front==-1)
cout<<"EMPTY";
else if(front==rear)
{
ele=Q[front];
front=-1;
rear=-1;
}
else if(front==s-1)
{
ele=Q[front];
front=0;
}
else
{
ele=Q[front];
front++;
}
cout<<"DELETED VALUE..."<<ele;
}
void display()
{
int i;
if(front==-1)
cout<<"\n EMPTY";
else if(rear>=front)
for(i=front;i<=rear;i++)
cout<<" "<<Q[i];
else
64

{
for(i=0;i<=rear;i++)
cout<<" "<<Q[i];
for(i=front;i<s;i++)
cout<<" "<<Q[i];
}
}

OUTPUT

65

PROGRAM 27
Write SQL command for (a) to (f) and write the outputs for (g)
on the basis of tables FURNITURE and ARRIVALS.
TABLE: FURNITURE
NO.
1
2
3
4

ITEMNAME
WHITE LOTUS
PINK FEATHER
DOLPHIN
DECENT

5
6
7

COMFORT ZONE
DONALD
ROYAL FINISH

8
9
10

ROYAL TIGER
ECONO SITTING
EATING PARADISE

TYPE
DOUBLE BED
BABY COT
BABY COT
OFFICE
TABLE
DOUBLE BED
BABY COT
OFFICE
TABLE
SOFA
SOFA
DINNING
TABLE

DATEOFSTOCK
23/02/02
20/01/02
19/02/02
01/01/02

PRICE
30000
7000
9500
25000

DISCOUNT
25
20
20
30

12/01/02
24/02/02
20/02/02

25000
6500
18000

25
15
30

22/02/02
13/12/01
19/02/02

31000
9500
11500

30
25
25

TABLE: ARRIVALS
NO.
11

ITEMNAME
WOOD COMFORT

12
13

OLD FOX
MICKY

TYPE
DOUBL
E BED
SOFA
BABY
COT

DATEOFSTOCK
23/03/03

PRICE
25000

DISCOUNT
25

20/02/03
21/02/03

17000
7500

20
15

a) To show all information about the baby cots from the


FURNITURE table.
b) To list the ITEMNAME which are priced at more than 15000
from the table FURNITURE.
c) To list ITEMNAME and TYPE of those items, in which
DATEOFSTOCK is before 22/01/02 from the FURNITURE table
in descending order of ITEMNAME.
d) To display ITEMNAME AND DATEOFSTOCK of those items,
in which the DISCOUNT percentage is more than 25 from
FURNITURE table.

66

e) To count the number of items, whose TYPE is SOFA from the


FURNITURE table.
f) To insert a new row in the ARRIVALS table with the following
data.
14,VELVET TOUCH, DOUBLE BED,{25/03/03},25000,30
g) To display ITEMNAME AND DATEOFSTOCK of those items,
in which the DISCOUNT percentage is less than 40 from
FURNITURE table.
h) To list ITEMNAME and PRICE of those items, in which
DATEOFSTOCK is before 12/01/02 from the FURNITURE table
in descending order of ITEMNAME.
i) To show all information about the sofa from the FURNITURE
table.
j) To list the ITEMNAME which are priced at more than 10500
from the table FURNITURE.
k) To count the number of items, whose TYPE is SOFA from the
FURNITURE table.
l) To display ITEMNAME AND DATEOFSTOCK of those items,
in which the DISCOUNT percentage is more than 35 from
FURNITURE table.
m) To list the ITEMNAME which are priced at more than 25000
from the table FURNITURE.
n) Give the output of the following SQL statements:
i. SELECT COUNT(DISTINCT TYPE) FROM
FURNITURE;
ii. SELECT MAX(DISCOUNT) FROM FURNITURE,
ARRIVALS;
iii. SELECT AVG(DISCOUNT) FROM FURNITURE
WHERE TYPE= BABY COT;
iv. SELECT SUM(PRICE) FROM FURNITURE
WHERE DATEOFSTOCK< {12/02/02};
v. SELECT AVG(DISCOUNT) FROM ARRIVALS
WHERE TYPE= SOFA;
vi. SELECT MAX(DISCOUNT) FROM FURNITURE,
ARRIVALS;
vii. SELECT COUNT(DISTINCT TYPE) FROM
ARRIVALS;
67

viii. SELECT SUM(PRICE) FROM ARRIVALS WHERE


DATEOFSTOCK< {22/03/02};
ix. SELECT ITEMNAME FROM ARRIVALS WHERE
PRICE>15000;
x. SELECT AVG(DISCOUNT) FROM ARRIVALS
WHERE TYPE= SOFA;
xi. SELECT MAX(DISCOUNT) FROM FURNITURE,
ARRIVALS;
xii. SELECT COUNT(*) FROM FURNITURE WHERE
TYPE= SOFA;
NOTE: OUTPUTS OF THE ABOVE MENTIONED QUERIES
SHOULD BE BASED ON THE ORIGINAL DATA GIVEN IN BOTH
THE TABLES I.E., WITHOUT CONSIDERING THE INSERTION
DONE IN (F) PART OF THIS QUESTION.

ANSWERS:
a) SELECT * FROM FURNITURE WHERE TYPE= BABY COT;
b) SELECT ITEMNAME FROM FURNITURE WHERE
PRICE>15000;
c) SELECT ITEMNAME, TYPE FROM FURNITURE WHERE
DATEOFSTOCK<{22/01/02} ORDER BY ITEMNAME;
d) SELECT ITEMNAME, DATEOFSTOCK FROM FURNITURE
WHERE DISCOUNT>25;
e) SELECT COUNT(*) FROM FURNITURE WHERE TYPE=
SOFA;
f) INSERT INTO ARRIVALS VALUES ( 14,VELVET TOUCH,
DOUBLE BED,{25/03/03},25000,30);
g) SELECT ITEMNAME, DATEOFSTOCK FROM FURNITURE
WHERE DISCOUNT<40;
h) SELECT ITEMNAME, PRICE FROM FURNITURE WHERE
DATEOFSTOCK<{12/01/02} ORDER BY ITEMNAME;
i) SELECT * FROM FURNITURE WHERE TYPE= SOFA;
j) SELECT ITEMNAME FROM FURNITURE WHERE
PRICE>10500;
68

k) SELECT COUNT(*) FROM FURNITURE WHERE TYPE=


SOFA;
l) SELECT ITEMNAME, DATEOFSTOCK FROM FURNITURE
WHERE DISCOUNT>35;
m) SELECT ITEMNAME FROM FURNITURE WHERE
PRICE>25000;
n) (i)5 (ii)30 (iii)18-33 (iv)66500 (v)20 (vi)30 (vii)3 (viii)24500

(ix)

(x)20 (xi)30 (xii)2

ITEMNAME
WOOD COMFORT
OLD FOX

PROGRAM 28
Study the following table DOCTORS and SALARY and write
SQL commands for the questions (i) ti (iv) and give the outputs
for SQL queries (v) to (vi):
Table: DOCTOR
ID
NAME
101
104
107

John
Smith
George

114
109

Lara
K

DEPT

SEX

ENT
ORTHOPEDIC
CARDIOLOG
Y
SKIN
MEDICINE

M
M
M

EXPERIENC
E
12
5
10

F
F

3
9
69

105
117
111
130

George
Johnson
Lucy
Bill
Morphy

ORTHOPEDIC
ENT
MEDICINE
ORTHOPEDIC

Table: SALARY
ID
BASIC
101
104
107
114
109
105
130

12000
23000
32000
12000
42000
18900
21700

M
F
F
M

ALLOWANC
E
1000
2300
4000
5200
1700
1690
2600

10
3
12
15
CONSULTATIO
N
300
500
500
100
200
300
300

a) Display NAME of all doctors who are in MEDICINE having


more than 10 years experience from the table DOCTOR.
b) Display the average salary of all the doctors working in ENT
department using the tables DOCTOR and SALARY.Salary=
BASIC + ALLOWANCE.
c) Display the minimum ALLOWANCE of female doctors.
d) Display highest consultation fee among all male doctors.
e) Display NAME of all doctors who are in ORTHOPEDIC
having less than 08 years experience from the table DOCTOR.
f) Display the average salary of all the doctors working in SKIN
department using the tables DOCTOR and SALARY.
g) Display the minimum allowance of MALE doctors.
h) Display highest consultation fee among all FEMALE doctors.
i) Display the average salary of all the doctors working in
ORTHOPEDIC department using the tables DOCTOR and
SALARY.
a) To list NAME and SEX of those items, in which EXPERIENCE
is less than 10 from the DOCTOR table in descending order of DEPT.
70

b) To show all information about the ENT from the DOCTOR table.
c) To list the ID which HAVE BASIC PAY more than 10000 from
the table SALARY.
d) To count the number of ID, whose TYPE is MEDICINE from
the DOCTOR table.
j) SELECT count(*) from DOCTOR where SEX = F;
k) SELECT NAME, DEPT, BASIC from DOCTOR, SALARY
WHERE DPET= ENT AND DOCTOR.ID = SALARY.ID
l) SELECT count(*) from DOCTOR where DEPT = ENT;
m) SELECT NAME, BASIC from DOCTOR, SALARY WHERE
DPET= MEDICINE AND DOCTOR.ID = SALARY.ID
a) SELECT COUNT(DISTINCT TYPE) FROM DOCTOR;
b) SELECT MAX(BASIC) FROM SALARY;
c) SELECT AVG(EXPERIENCE) FROM DOCTOR WHERE
DEPT= ORTHOPEDIC;
d) SELECT SUM(ALLOWANCE) FROM SALARY WHERE
BASIC< 20000;
n) SELECT DEPT, BASIC from DOCTOR, SALARY WHERE
DPET= SKIN AND DOCTOR.ID = SALARY.ID
o) SELECT NAME, BASIC from DOCTOR, SALARY WHERE
DPET= MEDICINE AND DOCTOR.ID = SALARY.ID
e) SELECT COUNT(*) FROM SALARY;
f)
SELECT MIN(CONSULTATION) FROM SALARY;

Answers :
a) SELECT NAME FROM DOCTOR WHERE DEPT=
MEDICINE AND EXPERIENCE > 10;
b) SELECT AVG(BASIC + ALLOWANCE) FROM SALARY,
DOCTOR WHERE DEPT= ENT AND DOCTOR.ID =
SALARY.ID;
c) SELECT MIN(ALLOWANCE) FROM SALARY, DOCTOR
WHERE SEX= F AND DOCTOR.ID = SALARY.ID;
d) SELECT MAX(CONSULTATION) FROM SALARY, DOCTOR
WHERE SEX= M AND DOCTOR.ID = SALARY.ID;
e) SELECT NAME FROM DOCTOR WHERE DEPT=
ORTHOPEDIC AND EXPERIENCE < 8;
71

f)

SELECT AVG(BASIC + ALLOWANCE) FROM SALARY,


DOCTOR WHERE DEPT= SKIN AND DOCTOR.ID =
SALARY.ID;
g) SELECT MIN(ALLOWANCE) FROM SALARY, DOCTOR
WHERE SEX= M AND DOCTOR.ID = SALARY.ID;
h) SELECT MAX(CONSULTATION) FROM SALARY, DOCTOR
WHERE SEX= F AND DOCTOR.ID = SALARY.ID;
i) SELECT AVG(BASIC + ALLOWANCE) FROM SALARY,
DOCTOR WHERE DEPT= ORTHOPEDIC AND
DOCTOR.ID = SALARY.ID;
a) SELECT NAME, SEX FROM DOCTOR WHERE
EXPERIENCE<10 ORDER BY DEPT;
b) SELECT * FROM DOCTOR WHERE TYPE= ENT;
c) SELECT ID FROM SALARY WHERE BASIC>10000;
d) SELECT COUNT(*) FROM DOCTOR WHERE TYPE=
MEDICINE;
j) 4
k) John ENT 12000
l) 2
m) K George 42000
n) 9
o) 42000
p) 10
q) 7890
r) SKIN
12000
s) K George 42000
t) 7
u) 100

72

PROGRAM 29
Obtain a simplified form for a Boolean expression
F (u, v, w, z) = (0, 1, 3, 5, 7, 9, 11, 12, 13, 14, 15) using Karnaugh
Map.

wz
uv

uv

uv

wz

wz

wz

12

13

15
14

11

10

uv

There are 4 groups:


Octet (m1, m3, m5, m7, m13, m15, m9, m11) reduces to z.
Quad 1(m12, m13, m14, m15) reduces to uv.
Quad 2(m10, m11, m14, m15) reduces to uw.
Pair (m0, m1) reduces to uvw.
The final expression is: uvw + uw +uv +z

73

PROGRAM 30
Reduce the following Boolean expression using K-Map:
F (A, B, C, D) = (5, 6, 7, 8, 9, 12, 13,14,15)

C+D

C+D

C+D

C+D

12

13

15

14

11

10

A+B
A+B
A+B
A+B
Three quads:
Quad 1(M5, M7, M13, M15) reduces to B+D.
Quad 2(M6, M7, M14, M15) reduces to B+C.
Quad 3(M8, M9, M12, M13) reduces to A+C.
The final expression is: (B+D). (B+C). (A+C)

74

You might also like