Compterpracticalfile
Compterpracticalfile
PRACTICAL FILE
BY :
SIDDHANT BANYAL
Roll no:
Computer science practical 2015-2016
Session: 2015-2016
INDEX
S.NO. Programme
1 WAP using classes to stimulate result preparation system for 10 students
2 Define a class tour in C++
3 Define a class outfit in C++
4 Write a menu based program using function to perform the following
(i) Search an element in an array using linear search.
(ii)Search an element in an array using binary search
2
Computer science practical 2015-2016
example if input is 1 2 3 4 5 6 7 8 9
3
Computer science practical 2015-2016
3, 14, 7, 1, 2, 4, 27, 5, 9, 10
2,1, 7, 14, 3, 10, 9, 5, 27, 4
Write a menu based program to do the following.
1) sum of both the diagonals
2 ) sum of each row
3) sum of each column
12 4) display middle row and middle column
Write a menu based program to do the following
1) display 1st upper half diagonal elements
2) display 2nd upper half diagonal elements
3) display 1st lower half diagonal elements
13 4) display 2nd lower half diagonal elements
Write a menu based program to do the following
1) swap first and last row
2) swap first and last column
3) display alternating elements
14 4) display elements residing at the edges
Write a menu based program to do the following in linked list
1) add node to the beginning
2) delete node from the following
15 3) display all nodes
Write a menu based program to do the following in linked list
1) add node to the last
2) delete node from the last
16 3) display all nodes
Write a menu based program to do the following functions within
stack as a linked list
1) Push
2) Pop
17 3) Display
Write a menu based program to do the following in linked queue
1) Insertion of a node
2) Deletion of a node
18 3) Display all the nodes
Write a menu based program to do the following in stack as an array
1) Push
2) Pop
19 3) Traverse
Write a menu based program to do the following in a queue as an array
4
Computer science practical 2015-2016
1) Insertion
2) Deletion
20 3) Display
Write a menu based program to do the following in a circular queue as an array
1) Insertion
2) Deletion
21 3) Display
Write a menu based program to do the following on a binary file "stud.dat"
1) add a new record
2) display all the records
3) search record based on name
22 4) delete record based on name
Write a menu based program to do the following on a binary file "stud.dat"
1) add a new record
2) display all the records
3) search record
23 4) update record
Write a menu based program to do the following on a binary file "stud.dat"
1) add a new record
2) display all the records
3) search a record
24 4) delete a record
Write a menu based program to do the following on a binary file "stud.dat"
1) add a new record
2) display all the records
3) search records based on marks
25 4) delete records based on marks
Write a menu based program to do the following an a binary file "stud.dat"
1) add a new record
2) display all the records
3) search records based on roll no
26 4) delete records based on roll no
Write a menu based program to do the following on a binary file "stud.dat"
1) add a new record
2) display all the records
3) display the info all the students whose percentage is between 80 to 90
27 4) display the info of all the students whose name
5
Computer science practical 2015-2016
Signature :_______________________________
6
Computer science practical 2015-2016
<50 F
>=50<60 D
>=60<75 C
>=75<90 B
>=90<100 A
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student {
int rn;
char name[20];
float marks1, marks2, marks3;
public:
void input();
int getpercent();
char getgrade();
void result();
char *getname();
int getrn();
};
student s1[10];
int i;
void student::input()
{
cout << "ENTER INFORMATION FOR STUDENT " << i + 1 << endl;
cout << endl;
cout << "Enter roll no." << endl;
cin >> s1[i].rn;
7
Computer science practical 2015-2016
if (p < 50) {
return 'F';
} else if (p >= 50 && p < 60) {
return 'D';
} else if (p >= 60 && p < 75) {
return 'C';
} else if (p >= 75 && p < 90) {
return 'B';
} else if (p >= 90 && p <= 100) {
return 'A';
}
}
char *student::getname()
{
return s1[i].name;
}
int student::getrn()
{
return s1[i].rn;
}
void student::result()
8
Computer science practical 2015-2016
{
cout << endl;
cout << "-----------------------------------" << endl;
cout << " RESULT " << endl;
cout << endl;
cout << " Roll No.: " << getrn() << endl;
cout << " Name: " << getname() << endl;
cout << " Percentage Marks: " << getpercent() << endl;
cout << " Grade: " << getgrade() << endl;
cout << "-----------------------------------" << endl;
}
Void main ()
{
clrscr();
student s2;
for (i = 0; i < 10; i++) {
s2.input();
}
for (i = 0; i < 10; i++) {
s2.result();
}
getch();
}
9
Computer science practical 2015-2016
10
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
11
Computer science practical 2015-2016
#include<string.h>
class Tour
char TCode[10];
int NoofAdults;
int NoofKids;
int Kilometers;
float TotalFare;
public:
Tour()
strcpy(TCode,"NULL");
NoofAdults=0;
NoofKids=0;
Kilometers=0;
TotalFare=0;
void AssignFare()
if(Kilometers<500)
TotalFare=NoofAdults*200+NoofKids*100;
else if(Kilometers<1000)
12
Computer science practical 2015-2016
TotalFare=NoofAdults*300+NoofKids*150;
else
TotalFare=NoofAdults*500+NoofKids*250;
void Enter_Tour()
gets(TCode);
cin>>NoofAdults>>NoofKids>>Kilometers;
AssignFare();
void Show_Tour()
cout<<"TCode - "<<TCode<<endl;
cout<<"Kilometers - "<<Kilometers<<endl;
};
void main()
13
Computer science practical 2015-2016
clrscr();
Tour T;
T.Show_Tour();
T.Enter_Tour();
T.Show_Tour();
getch();
14
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class outfit {
int OSize;
char OCode[20];
char OType[30];
char OFabric[20];
15
Computer science practical 2015-2016
float OPrice;
voidInitPrice() {
if (OFabric == "DENIM") {
if (strcmp(OType,"TROUSER")) {
OPrice = 1500;
} else if (strcmp(OType,"TROUSER")) {
OPrice = 2500;
} else {
if (strcmp(OType,"TROUSER")) {
} else if (strcmp(OType,"JACKET")) {
public:
outfit() {
OSize = 0;
OPrice = 0;
16
Computer science practical 2015-2016
void input() {
cin>>OFabric;
cin>>OType;
cin>>OCode;
cin>>OSize;
InitPrice();
void display() {
cout<< "Code for the cloth is " << " " <<OCode<<endl;
cout<< "Price of the chosen cloth is" << " " <<OPrice<<endl;
};
void main()
outfit o1;
17
Computer science practical 2015-2016
o1.input();
o1.display();
getch();
18
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
//liner search
int i,found=0;
for(i=0;i<n;i++)
if (a[i]==item)
found=1;
break;
19
Computer science practical 2015-2016
if(found==1)
cout<<"\nFound at "<<++i;
else
cout<<"\nNOT FOUND";
int beg,last,mid,found=0;
beg=0;
last=size-1;
while(beg<=last)
mid=(beg+last)/2;
if(a[mid]==item)
found=1;
break;
else if(a[mid]<item)
beg=mid+1;
20
Computer science practical 2015-2016
else if (a[mid]>item)
last=mid-1;
if (found==1)
else
void main()
int a[50],i,size,item;
int ch=0;
clrscr();
cin>>size;
for(i=0;i<size;i++)
cin>>a[i];
21
Computer science practical 2015-2016
cin>>item;
cin>>ch;
if(ch==1)
lsearch(a,size,item);
if(ch==2)
bsearch(a,size,item);
getch();
22
Computer science practical 2015-2016
#include<iostream.h>
23
Computer science practical 2015-2016
#include<conio.h>
void insert();
void deleteelement();
void duplicate();
int a[20],ch ;
char ans;
void main()
clrscr();
do
cout<<"enter ur choice"<<endl;
cin>>ch;
switch(ch)
case 1:
insert();
break;
case 2:
deleteelement();
break;
24
Computer science practical 2015-2016
case 3:
duplicate();
break;
default:
cout<<"invalid choice"<<endl;
break;
cout<<"wanna continue"<<endl;
cin>>ans;
while(ans=='y'); \
getch();
void insert()
int a[20],i,pos,num,n;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
25
Computer science practical 2015-2016
cout<<a[i]<<" ";
cin>>pos;
cin>>num;
for(i=n;i>=pos;i--)
a[i]=a[i-1];
a[pos-1]=num;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
void deleteelement()
int a[20],i,n,pos;
cin>>n;
for(i=0;i<n;i++)
26
Computer science practical 2015-2016
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cin>>pos;
for(i=pos-1;i<n-1;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
cout<<a[i]<<endl;
void duplicate()
int a[20],i,j,n,flag;
cin>>n;
27
Computer science practical 2015-2016
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<n;i++)
flag=0;
for(j=i+1;j<n;j++)
if(a[i]==a[j])
a[j]=0;
flag=1;
if(flag==1)
a[i]=0;
28
Computer science practical 2015-2016
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
29
Computer science practical 2015-2016
2) 3 7 9 5 12 4 14 8 10 6
3) 6 10 8 14 4 12 3 7 9 5
#include<iostream.h>
#include<conio.h>
int i;
for(i=0;i<size;i++)
30
Computer science practical 2015-2016
if(arr1[i]%2==0)
arr1[i]=3*arr1[i];
else
arr1[i]=2*arr1[i];
for(i=0;i<size;i++)
cout<<arr1[i]<<" ";
cout<<endl;
int i,j,k,arr2[10];
for(i=0,j=size-1,k=0;i<size;i++)
if(arr1[i]%2==0)
arr2[j]=arr1[i];
j--;
else
arr2[k]=arr1[i];
k++;
31
Computer science practical 2015-2016
for(i=0;i<size;i++)
cout<<arr2[i]<<" ";
cout<<endl;
int i,j,arr2[10];
for(i=0,j=0;i<size;i++)
if(arr1[i]%2==0)
arr2[j]=arr1[i];
j++;
for(i=0;i<size;i++)
if(arr1[i]%2!=0)
arr2[j]=arr1[i];
j++;
for(i=0;i<size;i++)
cout<<arr2[i]<<" ";
32
Computer science practical 2015-2016
cout<<endl;
void main()
int arr1[10],s,ch;
char ans;
do
clrscr();
X:
cin>>s;
if(s>10||s<1)
cout<<"invalid entry"<<endl;
goto X;
cout<<"enter info"<<endl;
for(int i=0;i<s;i++)
cin>>arr1[i];
33
Computer science practical 2015-2016
cin>>ch;
switch(ch)
case 1:
rearrange1(arr1,s);
break;
case 2:
rearrange2(arr1,s);
break;
case 3:
rearrange3(arr1,s);
break;
default:
cout<<"invalid entry"<<endl;
cin>>ans;
}while(ans=='y');
getch();
34
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
35
Computer science practical 2015-2016
int size,ch,j,i,k=0,A[100][100],B[100];
clrscr();
cin>>ch;
switch(ch)
case 1:
cin>>size;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
cin>>A[i][j];
TwoDtoOneD(A,size);
break;
case 2:
36
Computer science practical 2015-2016
cin>>size;
for(i=0;i<size;i++)
cin>>B[i];
OneDtoTwoD(B,size);
break;
default:
cout<<"Wrong Input";
getch();
int A[100],i,j,k=0;
A[k]=B[i][j];
37
Computer science practical 2015-2016
k++;
cout <<A[k]<<"\n";
int A[100][100],j,k=0;
int s=sqrt(size);
if(pow(s,2)==size)
cout<<"Conversion possible"<<endl;
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
A[i][j]=B[k];
k++;
for(i=0;i<3;i++)
38
Computer science practical 2015-2016
for(j=0;j<3;j++)
cout<<A[i][j]<<" ";
cout<<endl;
else
39
Computer science practical 2015-2016
Write a menu based program to pass an integer type of an array and its size as a
parameter to a function and rearrange the elements as per the following :
4 27 5 9 10 3 14 7 1 2
1)27 4 9 5 3 10 7 14 2 1
2)10 7 14 2 1 27 4 9 5 3
3)3 5 9 4 27 1 2 14 7 10
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
{ int i,temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
for(i=0;i<size;i=i+2)
temp = arr[i];
arr[i] = arr[i+1];
40
Computer science practical 2015-2016
arr[i+1] = temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
int i,temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
for(i=0;i<size/2;i++)
temp = arr[i];
arr[i] = arr[i+size/2];
arr[i+size/2] = temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
41
Computer science practical 2015-2016
int i,j,temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
for(i=0,j=size-1;i<size/2;i++,j--)
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
for(i=0;i<size;i++)
cout<<" "<<arr[i];
void main()
clrscr();
int a[100];
char ans;
int i,size,ch;
cin>>size;
42
Computer science practical 2015-2016
for(i=0;i<size;i++)
cin>>a[i];
do
clrscr();
cin>>ch;
switch(ch)
case 1 : arr1(a,size);
break;
case 2 : arr2(a,size);
break;
case 3 : arr3(a,size);
break;
break;
cin>>ans;
43
Computer science practical 2015-2016
while(ans=='Y'||ans=='y');
getch();
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
44
Computer science practical 2015-2016
int i,ds,j;
ds=0;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
if(i==j||i+j==size-1)
ds=ds+arr[i][j];
int i,j;
int rs=0;
for(i=0;i<size;i++)
rs=0;
for(j=0;j<size;j++)
45
Computer science practical 2015-2016
rs=rs+arr[i][j];
int i,j;
for(i=0;i<size;i++)
int cs=0;
for(j=0;j<size;j++)
cs=cs+arr[j][i];
if(size%2==0)
for(int i=0;i<size;i++)
46
Computer science practical 2015-2016
for(int j=0;j<size;j++)
if(i==size/2||j==size/2)
cout<<arr[i][j];
cout<<endl;
else
void main()
clrscr();
int a[100][100];
char ans;
int i,j,size,ch;
cin>>size;
for(i=0;i<size;i++)
47
Computer science practical 2015-2016
for(j=0;j<size;j++)
cin>>a[i][j];
do
clrscr();
cin>>ch;
switch(ch)
case 1 :
arr1(a,size);
break;
case 2 : arr2(a,size);
break;
case 3 :
arr3(a,size);
break;
48
Computer science practical 2015-2016
case 4 :
arr4(a,size);
break;
break;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
49
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
int i,j;
for(i=0;i<s;i++)
for(j=0;j<s;j++)
if(i<=j)
50
Computer science practical 2015-2016
cout<<arr[i][j]<<" ";
else
cout<<" ";
cout<<endl;
{ int i,j;
for(i=0;i<s;i++)
for(j=0;j<s;j++)
if(i+j<s)
cout<<arr[i][j]<<" ";
else
cout<<" ";
cout<<endl;
{ int i,j;
for(i=0;i<s;i++)
51
Computer science practical 2015-2016
for(j=0;j<s;j++)
if(i>=j)
cout<<arr[i][j]<<" ";
else
cout<<" ";
cout<<endl;
{ int i,j;
for(i=0;i<s;i++)
for(j=0;j<s;j++)
if(i+j>=s-1)
cout<<arr[i][j]<<" ";
else
cout<<" ";
cout<<endl;
void main()
52
Computer science practical 2015-2016
int i,j,ch,size,arr[10][10];
char ans;
cin>>size;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
cin>>arr[i][j];
do
clrscr();
cout<<"Press\n";
cin>>ch;
switch(ch)
case 1:
53
Computer science practical 2015-2016
upper1(arr,size);
break;
case 2:
upper2(arr,size);
break;
case 3:
lower1(arr,size);
break;
case 4:
lower2(arr,size);
break;
default:
cin>>ans;
}while(ans=='y');
54
Computer science practical 2015-2016
getch();
#include<conio.h>
#include<iomanip.h>
55
Computer science practical 2015-2016
void main()
clrscr();
int a[50][50];
char ans;
int i,j,row,col,ch;
cout<<"Enter no of rows"<<endl;
cin>>row;
cout<<"Enter cols"<<endl;
cin>>col;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>a[i][j];
do
56
Computer science practical 2015-2016
clrscr();
cin>>ch;
switch(ch)
case 1 : arr1(a,row,col);
break;
case 2 : arr2(a,row,col);
break;
case 3 : arr3(a,row,col);
break;
case 4 : arr4(a,row,col);
break;
break;
cin>>ans;
while(ans=='Y'||ans=='y');
57
Computer science practical 2015-2016
getch();
int i,j,temp;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<" "<<arr[i][j];
cout<<endl;
for(j=0;j<c;j++)
temp = arr[0][j];
arr[0][j] = arr[r-1][j];
arr[r-1][j] = temp;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
58
Computer science practical 2015-2016
cout<<" "<<arr[i][j];
cout<<endl;
int i,j,temp;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<" "<<arr[i][j];
cout<<endl;
for(i=0;i<r;i++)
temp = arr[i][0];
arr[i][0] = arr[i][c-1];
arr[i][c-1] = temp;
for(i=0;i<r;i++)
59
Computer science practical 2015-2016
for(j=0;j<c;j++)
cout<<" "<<arr[i][j];
cout<<endl;
int i,j,k,temp;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<" "<<arr[i][j];
cout<<endl;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
k=0;
60
Computer science practical 2015-2016
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(k%2==0)
cout<<" "<<arr[i][j];
k++;
cout<<endl;
cout<<endl;
int i,j,temp;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
61
Computer science practical 2015-2016
cout<<" "<<arr[i][j];
} cout<<endl;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if(i==0||j==0||i==r-1||j==c-1)
cout<<" "<<arr[i][j];
else
cout<<" ";
} cout<<endl;
62
Computer science practical 2015-2016
#include<iostream.h>
63
Computer science practical 2015-2016
#include<conio.h>
struct Node
int info;
Node*next;
};
class list
Node*start;
public:
list()
start=NULL;
void AddNodeLast();
void DeleteNodeLast();
void TraverseNode();
};
void main()
clrscr();
list l;
int ch;
char ans;
do
64
Computer science practical 2015-2016
cin>>ch;
switch(ch)
case 1:
l.AddNodeLast();
break;
case 2:
l.DeleteNodeLast();
break;
case 3:
l.TraverseNode();
break;
default:
cout<<"Want to confirm"<<endl;
cin>>ans;
while(ans=='y');
getch();
65
Computer science practical 2015-2016
Node *start,*last;
void list::AddNodeLast()
Node*Nodeptr;
Nodeptr=new Node;
cout<<"Enter info"<<endl;
cin>>Nodeptr->info;
Nodeptr->next=NULL;
if(start==NULL)
start=Nodeptr;
else
last->next=Nodeptr;
last=Nodeptr;
void list::DeleteNodeLast()
Node*temp;
if(start==NULL)
66
Computer science practical 2015-2016
cout<<"Cannot delete"<<endl;
else
for(temp=start;temp->next!=last;temp=temp->next);
temp->next=NULL;
delete last;
last=temp;
void list::TraverseNode()
if(start==NULL)
cout<<"Cannot display"<<endl;
else
Node*temp;
for(temp=start;temp!=NULL;temp=temp->next)
cout<<temp->info<<endl;
67
Computer science practical 2015-2016
PROGRAM
68
Computer science practical 2015-2016
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
struct node
int info;
node *next;
};
class list
node *start;
public:
list()
start=NULL;
void addnode();
void deletenode();
void traversenode();
};
void list::addnode()
69
Computer science practical 2015-2016
node *nodeptr;
nodeptr=new node;
cout<<"Enter Info"<<endl;
cin>>nodeptr->info;
nodeptr->next=start;
start=nodeptr;
cout<<"Node Added"<<endl;
void list::deletenode()
if (start==NULL)
else
node *temp;
temp=start;
start=temp->next;
temp->next=NULL;
delete temp;
void list::traversenode()
70
Computer science practical 2015-2016
if(start==NULL)
cout<<"Cannot Display"<<endl;
else
node *temp;
for(temp=start;temp!=NULL;temp=temp->next)
cout<<temp->info<<endl;
void main()
list l;
int ch;
char ans;
do
71
Computer science practical 2015-2016
cin>>ch;
switch(ch)
case 1:
l.addnode();
break;
case 2:
l.deletenode();
break;
case 3:
l.traversenode();
break;
default:
cout<<"Wish to continue?"<<endl;
cin>>ans;
while (ans=='y');
getch();
72
Computer science practical 2015-2016
struct NODE
73
Computer science practical 2015-2016
int rn;
char name[20];
node *next;
};
______________________________________________________________________
#include<iostream.h>
#include<conio.h>
struct Node
int info;
Node *next;
};
class Stack
Node *top;
public:
Stack()
top=NULL;
void push();
void pop();
void traverse();
};
void Stack::push()
74
Computer science practical 2015-2016
Node *nodeptr;
nodeptr=new Node;
cout<<"enter info"<<endl;
cin>>nodeptr->info;
nodeptr->next=top;
top=nodeptr;
cout<<"node pushed"<<endl;
void Stack::pop()
if(top==NULL)
cout<<"can't pop"<<endl;
else
Node *temp;
temp=top;
top=top->next;
temp->next=NULL;
delete temp;
75
Computer science practical 2015-2016
void Stack::traverse()
if(top==NULL)
cout<<"can't display"<<endl;
else
for(Node *temp=top;temp!=NULL;temp=temp->next)
cout<<temp->info<<" ";
void main()
clrscr();
int i;
char c;
Stack s;
do
cin>>i;
switch(i)
76
Computer science practical 2015-2016
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.traverse();
break;
default:
cout<<"invalid choice"<<endl;
cout<<"wanna continue?"<<endl;
cin>>c;
while(c=='y');
getch(); }
77
Computer science practical 2015-2016
78
Computer science practical 2015-2016
struct node
{
int rn;
char name[20];
node *next;
};
class queue
{
node *front,*rear;
public:
queue()
{
front=rear=NULL;
}
void insert();
void deleter();
void display();
};
void queue::insert()
{
node *nodeptr;
nodeptr=new node;
cout<<"\n"<<"Enter your name";
gets(nodeptr->name);
cout<<"\n"<<"Enter your roll number";
79
Computer science practical 2015-2016
cin>>nodeptr->rn;
nodeptr->next=NULL;
if(front==NULL)
{
front=nodeptr;
}
else
{
rear->next=nodeptr;
}
rear=nodeptr;
cout<<"\n"<<"NODE INSERTED";
}
void queue::deleter()
{
if (front==NULL)
{
cout<<"\n"<<"Cannot Delete";
}
else
{
node *temp;
temp=front;
cout<<"\n"<<"Deleted info is"<<temp->rn<<" "<<temp->name<<endl;
front=front->next;
temp->next=NULL;
delete temp;
}
}
void queue::display()
{
if(front==NULL)
{
cout<<"\n"<<"Cannot display";
}
else
{
node *temp;
80
Computer science practical 2015-2016
for(temp=front;temp!=NULL;temp=temp->next)
{
cout<<"\n"<<"temp->rn"<<" "<<temp->name;
}
}
}
void main()
{
clrscr();
int choice;
char ans;
queue q1;
do
{
cout<<"\n"<<"Press 1 to enter new information";
cout<<"\n"<<"Press 2 to delete information";
cout<<"\n"<<"Press 3 to view information";
cout<<"\n"<<"What function do you want to perform";
cin>>choice;
switch(choice)
{
case 1:
q1.insert();
break;
case 2:
q1.deleter();
break;
case 3:
q1.display();
break;
default:
cout<<"\n"<<"WRONG CHOICE!!!";
}
cout<<"\n"<<"Want to continue"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
81
Computer science practical 2015-2016
#include <iostream.h>
#include <conio.h>
82
Computer science practical 2015-2016
83
Computer science practical 2015-2016
case 2:
Cout<<"Now deletion of Elements begin"<< endl;
ch1 ='y';
while (ch1== 'y'||ch1 == 'Y')
{
res=pop(stack,top);
if (res == -1)
{
cout<<"No items to delete"<<endl;
break:
}
else
{
cout<<"Elements deleted is:"<<res<<endl;
cout<<"The stack is now"<<endl;
Display(Stack,top);
}
Cout<<"Want to delete more elements"<<endl;
cin>>ch;
}
break:
case 3:
Display(Stack,top);
break:
Default:
cout<<"Invalid input. Try again"<<endl;
break:
}
getch();
}
Int Push(int Stack[ ],int & top, int ele) //Function to insert
Elements
{
if (top == size-1)
{
84
Computer science practical 2015-2016
return -1;
}
else
{
top + +;
stack[top]=ele;
}
}
Int Pop( int stack [ ], int & top) //Function to Delete
elements
{
int ret;
if(top == -1)
{
return -1;
}
else
{
ret = stack[top];
top --;
}
return ret;
}
85
Computer science practical 2015-2016
{
cout<<Stack[i]<<endl;
}
}
}
86
Computer science practical 2015-2016
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
char ans;
front=rear=-1;
do
cout<<"Press 1 to insert"<<endl;
cout<<"Press 2 to delete"<<endl;
cout<<"Press 3 to display"<<endl;
cin>>ch;
switch(ch)
case 1:
if(front==-1)
front=0;
rear=rear+1;
if(rear>9)
87
Computer science practical 2015-2016
rear=rear-1;
else
cin>>queue[rear];
break;
case 2:
if(front==-1)
else
front=front+1;
if(front>rear)
front=rear=-1;
break;
case 3:
88
Computer science practical 2015-2016
if(front==-1)
{cout<<"cannot display"<<endl;
else
for(i=front;i<=rear;i++)
cout<<queue[i]<<" "<<endl;
break;
default:
cout<<"invalid choice"<<endl;
cout<<"wanna continue?(y/n)"<<endl;
cin>>ans;
while(ans=='y');
getch();
89
Computer science practical 2015-2016
90
Computer science practical 2015-2016
2) Deletion
3) Display
#define size 5
class queue {
int data[size], f, r;
public:
queue() {
f = -1;
r = -1;
void qinsert();
void qdelete();
void qdisplay();
};
void queue::qinsert() {
} else if (r == -1) {
91
Computer science practical 2015-2016
f = 0;
r = 0;
} else if (r == size - 1) {
r = 0;
} else {
r++;
void queue::qdelete() {
if (f == -1) {
} else if (f == r) {
f = -1;
r = -1;
} else if (f == size - 1) {
f = 0;
} else {
92
Computer science practical 2015-2016
f++;
void queue::qdisplay() {
if (f == -1) {
} else {
int i;
if (f <= r) {
} else {
void main()
93
Computer science practical 2015-2016
clrscr();
char ch = 'y';
int choice;
queue q1;
do {
switch (choice) {
case 1:
q1.qinsert();
break;
case 2:
q1.qdelete();
break;
case 3:
q1.qdisplay();
break;
default:
94
Computer science practical 2015-2016
getch();
95
Computer science practical 2015-2016
public:
void input();
void output();
int getRN();
char *getName();
float getMarks();
};
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class student
{
int rollno;
char name[20];
float marks;
public:
void input();
void output();
int getRN();
char *getName();
float getMarks();
};
void student::input()
96
Computer science practical 2015-2016
{
cout<<"enter r no, name and marks of the student"<<endl;
cin>>rollno;
gets(name);
cin>>marks;
}
int student::getRN()
{
return rollno;
}
char *student::getName()
{
return name;
}
float student::getMarks()
{
return marks;
}
int r,ch,flag;
char n[20],ans,name[20];
float M;
student s1,s2;
fstream f1,f2;
void addrecord()
{
cout<<"enter new info"<<endl;
s1.input();
f1.open("stud.dat",ios::out|ios::app);
97
Computer science practical 2015-2016
f1.write((char*)&s1, sizeof(s1));
f1.close();
}
void displayrecord()
{
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
s1.output();
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
}
void searchrecord()
{
flag=0;
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
cout<<"enter the name of the student to be searched"<<endl;
gets(name);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(strcmp(s1.getName(),name)==0)
{
flag=1;
break;
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
if(flag==0)
{
cout<<"student does not exist"<<endl;
}
98
Computer science practical 2015-2016
else
{
cout<<"student exists"<<endl;
s1.output();
}
}
void deleterecord()
{
cout<<"enter name of the student to be deleted"<<endl;
gets(name);
f1.open("stud.dat",ios::in);
f2.open("temp.dat",ios::out|ios::app);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(strcmp(s1.getName(),name)!=0)
{
f2.write((char*)&s1,sizeof(s1));
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
f2.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
}
void main()
{
clrscr();
do
{
cout<<"1. add new record"<<endl;
cout<<"2.display all records"<<endl;
cout<<"3.search a specific record"<<endl;
cout<<"4.delete a records"<<endl;
cout<<"enter your choice"<<endl;
99
Computer science practical 2015-2016
cin>>ch;
switch(ch)
{
case 1:
addrecord();
break;
case 2:
displayrecord();
break;
case 3:
searchrecord();
break;
case 4:
deleterecord();
break;
default:
cout<<"invalid choice"<<endl;
}
cout<<"wanna continue"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
100
Computer science practical 2015-2016
101
Computer science practical 2015-2016
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class student
{
int RN;
char name[20];
float marks;
public:
void input();
void output();
void modify();
int getRN();
char *getname();
float getmarks();
};
void student::input()
{
cout<<"Enter the name , roll no. and marks respectively of a student \n";
gets(name);
cin>>RN>>marks;
}
void student::output()
{
cout<<"Name, Roll no. and Marks of student repectively are- \n";
cout<<name<<" "<<RN<<" "<<marks<<endl;
}
char *student::getname()
{
return name;
}
float student::getmarks()
102
Computer science practical 2015-2016
{
return marks;
}
int student::getRN()
{
return RN;
}
void addrecord();
void displayrecord();
void searchrecord();
void deleterecord();
void updaterecord();
int r,ch,flag;
char n[20],ans;
float m;
student s1,s2;
fstream f1,f2;
void student::modify()
{
cout<<"enter new details\n";
char n[20]=" ";
cout<<"New name (press '.' to retain old one)\n";
gets(n);
cout<<"New marks (press -1 to retain old one)\n";
cin>>m;
if(strcmp(n,".")!=0)
{
strcpy(s1.name,n);
}
if(m!=-1)
{
s1.marks=m;
}
}
void displayrecord()
{
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
s1.output();
103
Computer science practical 2015-2016
f1.read((char*)&s1,sizeof(s1));
}
}
void searchrecord()
{
flag=0;
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
cout<<"Enter roll no. to be searched\n";
cin>>r;
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(s1.getRN()==r)
{
flag=1;
break;
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
if(flag==0)
cout<<"no roll no. exists by that no.\n";
else
{
cout<<"Roll no. exists\n";
s1.output();
}
}
void addrecord()
{
f1.open("stud.dat",ios::app|ios::out);
ans='y';
while(ans=='y')
{
s1.input();
f1.write((char*)&s1,sizeof(s1));
cout<<"Record added to file\n";
cout<<"want to add more records? (y/n) \n";
cin>>ans;
}
f1.close();
}
104
Computer science practical 2015-2016
void deleterecord()
{
char found='f';
f1.open("stud.dat",ios::in);
f2.open("temp.dat",ios::out);
cout<<"Enter roll no. of student whose record is to be deleted\n";
cin>>r;
while(!f1.eof())
{
f1.read((char*)&s1,sizeof(s1));
if(s1.getRN()==r)
{
s1.output();
found='t';
cout<<"Are you sure you want to delete this record? (y/n)";
cin>>ans;
if(ans=='n')
f2.write((char*)&s1,sizeof(s1));
}
else
f2.write((char*)&s1,sizeof(s1));
}
if(found=='f')
cout<<"\nrecord not found..\n";
f1.close();
f2.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
f1.open("stud.dat",ios::in);
cout<<"now the file contains -\n";
while(!f1.eof())
{
f1.read((char*)&s2,sizeof(s2));
if(f1.eof())
break;
s2.output();
}
f1.close();
}
void updaterecord()
{
f1.open("stud.dat",ios::in|ios::out|ios::binary);
long pos;
cout<<"Enter roll no. of student whose record is to be modified\n";
cin>>r;
105
Computer science practical 2015-2016
while(!f1.eof())
{
pos=f1.tellg();
f1.read((char*)&s1,sizeof(s1));
if(s1.getRN()==r)
{
s1.output();
s1.modify();
f1.seekg(pos);
f1.write((char*)&s1,sizeof(s1));
ans='t';
break;
}
}
if(ans=='f')
cout<<"Record not found\n";
f1.seekg(0);
cout<<"Now the file contains\n";
while(!f1.eof())
{
f1.read((char*)&s2,sizeof(s2));
s2.output();
}
f1.close();
}
void main()
{
do
{
clrscr();
cout<<"1.Add new record\n2.Display records\n3.Search record by roll no.\n4.Delete
record by roll no.\n5.Update record by roll no.\n";
cout<<"Enter choice\n";
cin>>ch;
switch(ch)
{
case 1:addrecord();
break;
case 2:displayrecord();
break;
case 3:searchrecord();
break;
case 4:deleterecord();
break;
case 5:updaterecord();
106
Computer science practical 2015-2016
break;
default:cout<<"wrong choice\n";
}
cout<<"want to continue? (y/n)\n";
cin>>ans;
}while(ans=='y');
getch();
}
107
Computer science practical 2015-2016
Delete record
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#include<fstream.h>
class student
{
int rollno;
char name[30];
public :
void enter();
void display();
int getrollno()
{
return rollno;
}
};
void student::enter()
{
cout<<"\n Enter name of student : ";
gets(name);
cout<<"\n Enter roll number of student : ";
cin>>rollno;
}
void student::display()
{
cout<<"\n ROLL NUMBER : "<<rollno;
cout<<"\n NAME :";
puts(name);
}
void createfile()
{
char option='n';
fstream f;
do{
108
Computer science practical 2015-2016
student s1;
f.open("student.dat",ios::out|ios::binary);
s1.enter();
f.write((char*)&s1,sizeof(s1));
f.close();
cout<<" \n Want to add more ? (y/n) ";
cin>>option;
}while(option=='y');
f.close();
void displayfile()
{
fstream f;
student s1;
f.open("student.dat",ios::in|ios::binary);
while (f.read((char*)&s1,sizeof(s1)))
{
s1.display();
}
f.close();
}
void search()
{
fstream f;
student s1;
int rollno,found=0;
f.open("student.dat",ios::in|ios::binary);
cout<<"Enter roll number to be searched for:";
cin>>rollno;
while(f.read((char*)&s1,sizeof(s1)))
{
if(rollno==s1.getrollno())
{
s1.display();
found=1;
}
}
if(found==0)
{
cout<<"Data not exist"<<endl;
}
109
Computer science practical 2015-2016
}
void update()
{
fstream f;
student s,s1;
int roll;
cout<<"\n Enter roll number to be updated";
cout<<"\n ENTER NEW DATA : ";
s1.display();
cin>>roll;
f.open("student.dat",ios::in|ios::out|ios::binary);
while(f.read((char*)&s,sizeof(s)))
{
if(roll==s.getrollno())
{
f.seekp(f.tellg()-sizeof(s));
f.write((char*)&s1,sizeof(s1));
}
}
f.close();
}
void remove()
{
fstream f1,f2;
int rollno;
student s;
f1.open("student.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
cout<<"Enter rollnumber record to be deleted ";
cin>>rollno;
while(f1.read((char*)&s,sizeof(s)))
{
if(rollno==s.getrollno())
{
s.display();
cout<<"RECORD DELETED";
}
else
{
f2.write((char*)&s,sizeof(s));
}
}
110
Computer science practical 2015-2016
f1.close();
f2.close();
remove("student.dat");
rename("temp.dat","student.dat");
}
void insert()
{
int found=0;
fstream f1,f2;
student s1,s2;
cout<<"\n Enter record to be inserted";
s1.enter();
f1.open("student.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
while(f1.read((char*)&s2,sizeof(s2)))
{
if(s1.getrollno()<s2.getrollno() && found==0)
{
f2.write((char*)&s1,sizeof(s1));
found=0;
}
f2.write((char*)&s2,sizeof(s2));
}
if(found==0)
{
f2.write((char*)&s1,sizeof(s1));
}
f1.close();
f2.close();
remove("student.dat");
rename("temp.dat","student.dat");
void main()
{
clrscr();
cout<<"_______MENU BASED PROGRAM ON BINAR FILE______"<<endl;
int ch=0;
char opt='n';
111
Computer science practical 2015-2016
do{
cin>>ch;
switch(ch)
{
case 1:createfile();
break ;
case 2:displayfile();
break ;
case 3:update();
break ;
case 4:search();
break ;
case 5:remove();
break ;
case 6:insert();
break ;
}
cout<<" \n want to go back ? (y/n)";
cin>>opt;
}while(opt=='y');
getch();
112
Computer science practical 2015-2016
113
Computer science practical 2015-2016
#include<fstream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class student
{
int RN;
char name[20];
float marks;
public:
void input();
void output();
int getRN();
char *getname();
float getmarks();
};
void student::input()
{
cout<<"Enter Roll No."<<endl;
cin>>RN;
cout<<"Enter Name"<<endl;
gets(name);
cout<<"Enter Marks"<<endl;
cin>>marks;
}
114
Computer science practical 2015-2016
void student::output()
{
cout<<"Roll No. : "<<RN<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Marks : "<<marks<<endl;
}
int student::getRN()
{
return RN;
}
char* student::getname()
{
return name;
}
float student::getmarks()
{
return marks;
}
void main()
{
do
{
clrscr();
cout<<"MENU"<<endl;
cout<<"1. Add Rec"<<endl;
115
Computer science practical 2015-2016
switch(ch)
{
case 1:
addrecord();
break;
case 2:
displayrecord();
break;
case 3:
toppers();
break;
case 4:
sname();
break;
default:
cout<<"Invalid Choice"<<endl;
}
cout<<"Want to continue?"<<endl;
cin>>ans;
}
while(ans=='y');
getch();
}
void addrecord()
{
cout<<"Enter info"<<endl;
s1.input();
f1.open("abc.dat", ios::out|ios::app);
f1.write((char*)&s1, sizeof(s1));
f1.close();
}
void displayrecord()
{
f1.open("abc.dat", ios::in);
f1.seekg(0, ios::beg);
116
Computer science practical 2015-2016
f1.read((char*)&s1, sizeof(s1));
while(!f1.eof())
{
s1.output();
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
}
void toppers()
{
f1.open("abc.dat", ios::in);
f1.seekg(0, ios::beg);
f1.read((char*)&s1, sizeof(s1));
while (!f1.eof())
{
if (s1.getmarks()>80 && s1.getmarks()<90)
{
s1.output();
}
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
}
void sname()
{
char temparr[20];
f1.open("abc.dat", ios::in);
f1.seekg(0, ios::beg);
f1.read((char*)&s1, sizeof(s1));
while(!f1.eof())
{
strcpy(temparr, s1.getname());
117
Computer science practical 2015-2016
>90 A
>80 and <=90 B
>70 and <=80 C
Else D grade
118
Computer science practical 2015-2016
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class student
{
int rollno;
char name[20];
float marks;
public:
void input();
void output();
int getRN();
char*getName();
float getMarks();
};
void student::input()
{
cout<<"Enter Name"<<endl;
gets(name);
cout<<"Enter Rollnumber and Marks"<<endl;
cin>>rollno>>marks;
}
void student::output()
{
puts(name);
cout<<"Roll number="<<rollno<<endl;
cout<<"Marks="<<marks<<endl;
}
int student::getRN()
{
return rollno;
}
char*student::getName()
{
return name;
}
float student::getMarks()
{
return marks;
}
119
Computer science practical 2015-2016
void addrecord();
void displayrecord();
void countrecord();
int ch;
char ans;
student s1;
fstream f1;
void addrecord()
{
s1.input();
f1.open("stud.dat",ios::out|ios::app);
f1.write((char*)&s1,sizeof(s1));
f1.close();
}
void displayrecord()
{
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
s1.output();
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
}
void countrecord()
{
int actr=0,bctr=0,cctr=0,dctr=0; //* A-counter,B-counter etc.
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(s1.getMarks()>90)
{
actr=actr+1;
}
else if(s1.getMarks()>80)
{
bctr=bctr+1;
}
else if(s1.getMarks()>70)
120
Computer science practical 2015-2016
{
cctr=cctr+1;
}
else
{
dctr=dctr+1;
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
cout<<"The number of students having Grade:"<<endl;
cout<<" A= "<<actr<<endl;
cout<<" B= "<<bctr<<endl;
cout<<" C= "<<cctr<<endl;
cout<<" D= "<<dctr<<endl;
}
void main()
{
do
{
clrscr();
cout<<"MENU : "<<endl;
cout<<"1. Add a new record"<<endl;
cout<<"2. Display records"<<endl;
cout<<"3. Count and display the total number of students in each grade"<<endl;
cin>>ch;
switch(ch)
{
case 1:{
addrecord();
break;
}
case 2:{
displayrecord();
break;
}
case 3:{
countrecord();
break;
}
default: cout<<"wrong choice entered"<<endl;
}
cout<<"y - main menu(to do it again) "<<endl;
cout<<"x - exit"<<endl;
121
Computer science practical 2015-2016
cin>>ans;
}
while (ans=='y'||ans=='Y');
getch();
}
122
Computer science practical 2015-2016
public:
void input();
void output();
int getRN();
char *getName();
float getMarks();
};
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class student
{
int rollno;
char name[20];
float marks;
public:
123
Computer science practical 2015-2016
void input();
void output();
int getRN();
char *getName();
float getMarks();
};
void student::input()
{
cout<<"enter r no, name and marks of the student"<<endl;
cin>>rollno;
gets(name);
cin>>marks;
}
int student::getRN()
{
return rollno;
}
char *student::getName()
{
return name;
}
float student::getMarks()
124
Computer science practical 2015-2016
{
return marks;
}
int r,ch,flag;
char n[20],ans,name[20];
float M;
student s1,s2;
fstream f1,f2;
void addrecord()
{
cout<<"enter new info"<<endl;
s1.input();
f1.open("stud.dat",ios::out|ios::app);
f1.write((char*)&s1, sizeof(s1));
f1.close();
}
void displayrecord()
{
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
s1.output();
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
}
125
Computer science practical 2015-2016
void searchrecord()
{
flag=0;
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
cout<<"enter the name of the student to be searched"<<endl;
gets(name);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(strcmp(s1.getName(),name)==0)
{
flag=1;
break;
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
if(flag==0)
{
cout<<"student does not exist"<<endl;
}
else
{
cout<<"student exists"<<endl;
s1.output();
}
}
126
Computer science practical 2015-2016
void deleterecord()
{
cout<<"enter name of the student to be deleted"<<endl;
gets(name);
f1.open("stud.dat",ios::in);
f2.open("temp.dat",ios::out|ios::app);
f1.seekg(0,ios::beg);
f1.read((char*)&s1,sizeof(s1));
while(!f1.eof())
{
if(strcmp(s1.getName(),name)!=0)
{
f2.write((char*)&s1,sizeof(s1));
}
f1.read((char*)&s1,sizeof(s1));
}
f1.close();
f2.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
}
void main()
{
clrscr();
do
{
cout<<"1. add new record"<<endl;
cout<<"2.display all records"<<endl;
cout<<"3.search a specific record"<<endl;
cout<<"4.delete a records"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
127
Computer science practical 2015-2016
switch(ch)
{
case 1:
addrecord();
break;
case 2:
displayrecord();
break;
case 3:
searchrecord();
break;
case 4:
deleterecord();
break;
default:
cout<<"invalid choice"<<endl;
}
cout<<"wanna continue"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
128
Computer science practical 2015-2016
129
Computer science practical 2015-2016
public:
void input()
{ cout<<"Input Name : ";
gets(name);
cout<<"\n Input roll number : ";
cin>>rollno;
cout<<"\n Input Marks : ";
cin>>marks;
}
void output()
{cout<<"\nName : "<<name;
cout<<"\nRoll number : "<<rollno;
cout<<"\nMarks : "<<marks;
}
Int getrollno();
};
int R, flag, ch;
char ans;
student S1;
fstream f1, f2;
int student::getrollno()
{ return rollno;
}
void addrecord()
{ f1.open("stud.dat", ios::out|ios::app);
130
Computer science practical 2015-2016
void displayrecord()
{f1.open("stud.dat", ios::in);
f1.seekg(0, ios::beg);
f1.read((char*)&S1, sizeof(S1));
if(f1.eof())
{cout<<" ";
}
else
{ while(!f1.eof())
{
S1.output();
f1.read((char*)&S1, sizeof(S1));
}
}
f1.close();
}
void searchrecord()
{flag=0;
cout<<"Enter Roll number to be searched \n";
cin>>R;
f1.open("stud.dat", ios::in);
f1.seekg(0, ios::beg);
f1.read((char*)&S1, sizeof(S1));
while(!f1.eof())
{
if (S1.getrollno()==R)
{ flag = 1;
cout<<"\n Student exists \n";
S1.output();
break;
}
f1.read((char*)&S1, sizeof(S1));
}
131
Computer science practical 2015-2016
f1.close();
if(flag==0)
{
cout<<"\n Student doesn't exist \n";
}
}
void deleterecord()
{cout<<"Enter Roll number of the student to be deleted \n";
cin>>R;
f1.open("stud.dat", ios::in);
f2.open("temp.dat", ios::out|ios::app);
f1.seekg(0, ios::beg);
f1.read((char*)&S1, sizeof(S1));
while(!f1.eof())
{if(S1.getrollno()!=R)
{f2.write((char*)&S1, sizeof(S1));
}
f1.read((char*)&S1, sizeof(S1));
}
f1.close();
f2.close();
remove("stud.dat");
rename("temp.dat","stud.dat");
}
void updaterecord()
{ cout<<"\n Enter Roll number of the student ot update record : ";
cin>>R;
f1.open("stud.dat", ios::in|ios::out);
f1.seekg(0, ios::beg);
f1.read((char*)&S1, sizeof(S1));
while(!f1.eof())
{ if(S1.getrollno()==R)
{ flag=1;
cout<<"\n Student exists \n";
S1.output();
break;
}
f1.read((char*)&S1, sizeof(S1));
}
132
Computer science practical 2015-2016
if(flag==0)
{
cout<<"\n Student does not exist \n";
}
else
{
f1.seekp(f1.tellg()-sizeof(S1), ios::beg);
cout<<"\n Enter new info : ";
S1.input();
f1.write((char*)&S1, sizeof(S1));
}
cout<<"\n Updated ";
f1.close();
}
void main()
{ clrscr();
student S1;
do
{cout<<"\n ***MENU***\n";
cout<<"\n 1. Add Record";
cout<<"\n 2. Display Record";
cout<<"\n 3. Modify Record";
cout<<"\n 4. Search Record";
cout<<"\n 5. Delete Record \n";
cout<<"\n Enter your choice (y/n): ";
cin>>ch;
switch(ch)
{
case 1: addrecord();
break;
case 2: displayrecord();
break;
case 3: updaterecord();
break;
case 4: searchrecord();
break;
case 5: deleterecord();
break;
default : cout<<"\n WRONG CHOICE!! \n";
cout<<"\n Do you want to continue (y/n) : ";
133
Computer science practical 2015-2016
cin>>ans;
}
}while(ans=='y');
getch();
}
134
Computer science practical 2015-2016
class student
{ int rollno;
char name[20];
float marks;
public:
void input();
void output();
int getrollno();
char *getname();
float getmarks();
};
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class student
{
int rollno;
char name[20];
float marks;
public:
void input();
void output();
int getrollno();
char *getname();
float getmarks();
};
void student::input()
{
135
Computer science practical 2015-2016
void student::output()
{
cout<<" roll no: "<<rollno<<endl;
cout<<" name : "<<name<<endl;
cout<<" marks : "<<marks<<endl;
}
int student::getrollno()
{
return rollno;
}
char *student::getname()
{
return name;
}
float student::getmarks()
{
return marks;
}
student s1;
fstream f1,f2,f3;
int ch;
char ans,name[2];
void addrecord();
void displayrecord();
void deleterecord1();
void deleterecord2();
136
Computer science practical 2015-2016
void main()
{
do
{
clrscr();
cout<<"press 1 to add new record"<<endl;
cout<<"press 2 to display all records"<<endl;
cout<<"press 3 to delete info of all students whose name doesnt start with a and
store deleted info in backup file"<<endl;
cout<<"press 4 to delete info of all students whose name ends with a"<<endl;
cin>>ch;
switch(ch)
{
case 1: addrecord();
break;
case 2: displayrecord();
break;
case 3: deleterecord1();
break;
case 4: deleterecord2();
break;
default:cout<<"invalid choice"<<endl;
}
cout<<" do u wish to continue?"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
void addrecord()
{
f1.open("stud.dat",ios::out| ios::app);
cout<<"enter info"<<endl;
s1.input();
f1.write((char*)&s1, sizeof(s1));
cout<<"info added"<<endl;
f1.close();
137
Computer science practical 2015-2016
void displayrecord()
{
f1.open("stud.dat",ios::in);
f1.seekg(0,ios::beg);
f1.read((char*)&s1, sizeof(s1));
while(!f1.eof())
{
s1.output();
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
}
void deleterecord1()
{
f1.open("stud.dat",ios::in);
f2.open("temp.dat",ios::out | ios::app);
f3.open("backup.dat",ios::out | ios::app);
f1.seekg(0,ios::beg);
f1.read((char*)&s1, sizeof(s1));
while(!f1.eof())
{ char temp[20];
strcpy(temp,s1.getname());
if(temp[0]=='A' || temp[0]=='a')
{
f2.write((char*)&s1, sizeof(s1));
}
else
{
f3.write((char*)&s1, sizeof(s1));
cout<<"deleted info is: "<<endl;
s1.output();
}
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
f2.close();
f3.close();
remove("stud.dat");
138
Computer science practical 2015-2016
rename( "temp.dat","stud.dat");
}
void deleterecord2()
{
f1.open("stud.dat",ios::in);
f2.open("temp.dat",ios::out | ios::app);
f1.seekg(0,ios::beg);
f1.read((char*)&s1, sizeof(s1));
while(!f1.eof())
{ char temp[20];
strcpy(temp,s1.getname());
int l=strlen(temp);
if(temp[l-1]=='a')
{
cout<<"deleted info is: "<<endl;
s1.output();
}
else
{
f2.write((char*)&s1, sizeof(s1));
}
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
f2.close();
remove("stud.dat");
rename( "temp.dat","stud.dat");
139
Computer science practical 2015-2016
TEXT FILE
140
Computer science practical 2015-2016
CREATE A FILE
DISPLAY FILE DATA
ADD MORE DATA TO FILE
COUNT NUMBER OF
ALPHABETS,VOWELS,CONSONANTS,DIGIT
&SPACES
COUNT NUMBER OF WORDS AND NUMBER
OF THIS
COUNT NUMBER OF LINES & NUMBER OF
LINE BEGINNING WITH T
REPLACE ALL THE SPACE BY HYPHEN(-)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
#include<fstream.h>
void create()
{
fstream f;
char str[80];
f.open("story.TXT",ios::out);
cout<<"Enter the data to input in file";
gets(str);
f<<str<<endl;
f.close();
}
void addmore()
141
Computer science practical 2015-2016
{
fstream f;
char str[80];
f.open("story.TXT",ios::app);
cout<<"Enter Data :";
gets(str);
f<<str<<endl;
f.close();
}
void display()
{
fstream f;
char ch;
char str[80];
f.open("story.TXT",ios::in);
while(f.get(ch)) //read character by character also check eof()
{
cout<<ch;
}
f.close();
}
void count()
{
fstream f;
char ch;
int a=0,d=0,s=0,c=0,v=0;
f.open ("story.txt",ios::in);
while(f.get(ch))
{
if(isalpha(ch))
{
a++;
switch(tolower(ch))
{
case'a':case'e':case'i':case'o':case'u':
v++;
142
Computer science practical 2015-2016
break;
default: c++;
}
}
else if(isdigit(ch))
d++;
else if(ch==' ')
s++;
}
f.close();
cout<<"_________________FILE SUMMARY
_________________ "<<endl;
cout<<"Number of Alphabets :"<<a<<endl;
cout<<"Number of vowels :"<<v<<endl;
cout<<"Number of consonants:"<<c<<endl;
cout<<"Number of Digits :"<<d<<endl;
cout<<"Number of Spaces :"<<s<<endl;
void countthis()
{
fstream f;
char str[80];
int n=0,t=0;
f.open("story.TXT",ios::in);
while(!f.eof())
{
f>>str;
if(f.eof())
break;
n++;
if(strcmpi(str,"this")==0)
t++;
143
Computer science practical 2015-2016
}
cout<<"TOTAL NUMBER OF WORDS ARE "<<n<<" AND TOTAL
NUMBER OF (this) are "<<t<<endl;
}
void countline()
{
fstream f;
char str[80];
int n=0,t=0;
f.open("story.TXT",ios::in);
while(f.getline(str,80)) //read line by line
{
n++;
if(str[0]=='t'||str[0]=='T')
t++;
}
f.close();
cout<<"\n Number of line "<<n;
cout<<"\n Number of lines begining with t or T "<<t;
cout<<endl;
void misc()
{
fstream f;
char ch;
f.open("story.TXT",ios::in|ios::out);
while(f.get(ch))
{
if(ch==' ')
{
f.seekp(f.tellg()-1);
f.put('-');
}
144
Computer science practical 2015-2016
}
f.close();
}
void main()
{
clrscr();
cout<<"_______MENU BASED PROGRAM ON TEXT
FILE______"<<endl;
int ch=0;
char opt='n';
do{
145
Computer science practical 2015-2016
break ;
case 7:misc();
break ;
}
cout<<" \n want to go back ? (y/n)";
cin>>opt;
}while(opt=='y');
getch();
146
Computer science practical 2015-2016
2 (
3 FL_NO char(30),
4 STARTING char(30),
5 ENDING char(30),
6 NO_FLIGHTS number,
7 NO_STOPS number
8* )
9 /
table created.
1 row created.
147
Computer science practical 2015-2016
1 row created.
1 row created.
148
Computer science practical 2015-2016
1 row created.
1 row created.
1 row created.
149
Computer science practical 2015-2016
1 row created.
1 row created.
150
Computer science practical 2015-2016
1 row created.
2 /
1 row created.
1 row created.
151
Computer science practical 2015-2016
1 row created.
1 row created.
152
Computer science practical 2015-2016
1 row created.
1 row created.
1 row created.
153
Computer science practical 2015-2016
1 row created.
2 /
154
Computer science practical 2015-2016
1 row created.
SQL> /
1 row created.
SQL> AM501
SQL> /
1 row created.
SQL> /
155
Computer science practical 2015-2016
1 row created.
1 row created.
SQL> /
1 row created.
SQL> /
156
Computer science practical 2015-2016
1 row created.
2 /
FL_NO STARTING
------------------------------ ------------------------------
IC301 MUMBAI
DELHI 8 0
IC799 BANGLORE
DELHI 2 1
MC101 INDORE
MUMBAI 3 0
FL_NO STARTING
------------------------------ ------------------------------
157
Computer science practical 2015-2016
IC302 DELHI
MUMBAI 8 0
AM812 KANPUR
BANGLORE 3 1
IC899 MUMBAI
KOCHI 1 4
FL_NO STARTING
------------------------------ ------------------------------
AM501 DELHI
TRIVANDRUM 1 5
MU499 MUMBAI
MADRAS 3 3
IC701 DELHI
AHMEDABAD 4 0
2 /
158
Computer science practical 2015-2016
FL_NO NO_FLIGHTS
------------------------------ ----------
AM812 3
2)
2 /
FL_NO NO_FLIGHTS
------------------------------ ----------
AM812 3
2 order by FL_NO;
FL_NO STARTING
------------------------------ ------------------------------
AM501 DELHI
TRIVANDRUM 1 5
AM812 KANPUR
159
Computer science practical 2015-2016
BANGLORE 3 1
IC301 MUMBAI
DELHI 8 0
FL_NO STARTING
------------------------------ ------------------------------
IC302 DELHI
MUMBAI 8 0
IC701 DELHI
AHMEDABAD 4 0
IC799 BANGLORE
DELHI 2 1
FL_NO STARTING
------------------------------ ------------------------------
IC899 MUMBAI
KOCHI 1 4
160
Computer science practical 2015-2016
MC101 INDORE
MUMBAI 3 0
MU499 MUMBAI
MADRAS 3 3
3)
3 and
4* FLIGHTS.FL_NO=FARES.FL_NO
SQL> /
FL_NO FARE+TAX
------------------------------ ----------
IC302 4310
MIN(FARE)
----------
4300
161
Computer science practical 2015-2016
5)
3 /
FL_NO NO_FLIGHTS
------------------------------ ----------
AIRLINES
----------------------------------------
AM501 1
JET AIRWAYS
IC302 8
INDIAN AIRLINES
IC701 4
INDIAN AIRLINES
6)
2 /
COUNT(DISTINCTENDING)
---------------------
162
Computer science practical 2015-2016
____________________________________________________
2
1 create table workers
2 (
3 w_id number(3),
4 firstname char(20),
5 lastname char(20),
6 address char(30),
7 city char(20)
8* )
SQL> /
Table created.
SQL>desc workers
Name Null? Type
----------------------------------------- -------- ----------------------------
W_ID NUMBER(3)
FIRSTNAME CHAR(20)
LASTNAME CHAR(20)
ADDRESS CHAR(30)
CITY CHAR(20)
1 row created.
1 row created.
SQL> /
163
Computer science practical 2015-2016
1 row created.
SQL> /
Enter value for w_id: 144
Enter value for firstname: manila
Enter value for lastname: sengupta
Enter value for address: 24 friends street
Enter value for city: new delhi
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(144,'manila','sengupta','24 friends street','newdelhi')
1 row created.
SQL> /
Enter value for w_id: 210
Enter value for firstname: george
Enter value for lastname: smith
Enter value for address: 83 first street
Enter value for city: howard
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(210,'george','smith','83 first street','howard')
1 row created.
SQL> /
Enter value for w_id: 255
Enter value for firstname: mary
Enter value for lastname: jones
Enter value for address: 842 vine ave
Enter value for city: losantiville
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(255,'mary','jones','842 vine ave','losantiville')
164
Computer science practical 2015-2016
1 row created.
SQL> /
Enter value for w_id: 300
Enter value for firstname: robert
Enter value for lastname: samuel
Enter value for address: 9 fifth cross
Enter value for city: washington
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(300,'robert','samuel','9 fifth cross','washington')
1 row created.
SQL> /
Enter value for w_id: 335
Enter value for firstname: henry
Enter value for lastname: williams
Enter value for address: 12 moore street
Enter value for city: boston
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(335,'henry','williams','12 moore street','boston')
1 row created.
SQL> /
Enter value for w_id: 403
Enter value for firstname: ronny
Enter value for lastname: lee
Enter value for address: 121 harrisonst
Enter value for city: new york
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(403,'ronny','lee','121 harrisonst','new york')
1 row created.
SQL> /
Enter value for w_id: 451
Enter value for firstname: pat
Enter value for lastname: thompson
Enter value for address: 11 red road
Enter value for city: paris
165
Computer science practical 2015-2016
1 row created.
9 rows selected.
Table created.
SQL>descdesig
Name Null? Type
----------------------------------------- -------- ----------------------------
W_ID NUMBER(3)
SALARY NUMBER(5)
BENIFITS NUMBER(5)
DESIGNATION CHAR(20)
166
Computer science practical 2015-2016
2 /
Enter value for w_id: 102
Enter value for salary: 75000
Enter value for benifits: 15000
Enter value for designation: manager
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(102,75000,15000,'manager')
1 row created.
SQL> /
Enter value for w_id: 105
Enter value for salary: 85000
Enter value for benifits: 25000
Enter value for designation: director
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(105,85000,25000,'director')
1 row created.
SQL> /
Enter value for w_id: 144
Enter value for salary: 70000
Enter value for benifits: 15000
Enter value for designation: manager
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(144,70000,15000,'manager')
1 row created.
SQL> /
Enter value for w_id: 210
Enter value for salary: 75000
Enter value for benifits: 12500
Enter value for designation: manager
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(210,75000,12500,'manager')
1 row created.
167
Computer science practical 2015-2016
SQL> /
Enter value for w_id: 255
Enter value for salary: 50000
Enter value for benifits: 12000
Enter value for designation: clerk
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(255,50000,12000,'clerk')
1 row created.
SQL> /
Enter value for w_id: 300
Enter value for salary: 45000
Enter value for benifits: 10000
Enter value for designation: clerk
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(300,45000,10000,'clerk')
1 row created.
SQL> /
Enter value for w_id: 335
Enter value for salary: 40000
Enter value for benifits: 10000
Enter value for designation: clerk
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(335,40000,10000,'clerk')
1 row created.
SQL> /
Enter value for w_id: 400
Enter value for salary: 32000
Enter value for benifits: 75000
Enter value for designation: salesman
old 1: insert into desig values(&w_id,&salary,&benifits,'&designation')
new 1: insert into desig values(400,32000,75000,'salesman')
1 row created.
SQL> /
168
Computer science practical 2015-2016
1 row created.
9 rows selected.
Ques 1
SQL> select w_id,firstname,address,city from workers
2 where
3 city='new york'
4 /
Ques2)
169
Computer science practical 2015-2016
9 rows selected.
Ques6)
1* select count(distinct designation) from desig
SQL> /
COUNT(DISTINCTDESIGNATION)
--------------------------
4
Ques7)
DESIGNATION SUM(SALARY)
-------------------- -----------
director 85000
salesman 60000
Ques8)
1 select sum(benifits)
2 fromdesig
3* where designation='salesman'
SQL> /
170
Computer science practical 2015-2016
SUM(BENIFITS)
-------------
150000
Ques5)
SQL> select firstname,salary
2 fromworkers,desig
3 where designation='manager'
4 andworkers.w_id=desig.w_id
5 /
FIRSTNAME SALARY
-------------------- ----------
sam75000
manila70000
george75000
Ques3)
Ques4)
MIN(SALARY)
171
Computer science practical 2015-2016
-----------
40000
70000
3)
Question 3
SQL> create table GARMENT
2 (
3 GCODE integer,
4 Description char(30),
5 Price integer,
6 FCODE char(10),
7 READYDATE date
8 );
Table created.
1 row created.
SQL> /
Enter value for gcode: 10001
Enter value for description: FORMAL
172
Computer science practical 2015-2016
1 row created.
SQL> /
Enter value for gcode: 10012
Enter value for description: INFORMAL
Enter value for price: 1550
Enter value for fcode: F02
Enter value for readydate: 06-JUN-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10012,'INFORMAL',1550,'F02','06-JUN-08')
1 row created.
SQL> /
Enter value for gcode: 10024
Enter value for description: BABY TOP
Enter value for price: 750
Enter value for fcode: F03
Enter value for readydate: 07-APR-07
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10024,'BABY TOP',750,'F03','07-APR-07')
1 row created.
SQL> /
Enter value for gcode: 10090
Enter value for description: TULIP SKIRT
Enter value for price: 850
Enter value for fcode: F02
Enter value for readydate: 31-MAR-07
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10090,'TULIP SKIRT',850,'F02','31-MAR-07')
1 row created.
SQL> /
173
Computer science practical 2015-2016
1 row created.
SQL> /
Enter value for gcode: 10009
Enter value for description: INFORMAL PANT
Enter value for price: 1500
Enter value for fcode: F02
Enter value for readydate: 20-OCT-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10009,'INFORMAL PANT',1500,'F02','20-OCT-
08')
1 row created.
1 row created.
SQL> /
Enter value for gcode: 10020
Enter value for description: FROCK
Enter value for price: 850
Enter value for fcode: F04
Enter value for readydate: 09-SEP-07
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10020,'FROCK',850,'F04','09-SEP-07')
1 row created.
174
Computer science practical 2015-2016
SQL> /
Enter value for gcode: 10089
Enter value for description: SLACKS
Enter value for price: 750
Enter value for fcode: F03
Enter value for readydate: 31-OCT-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10089,'SLACKS',750,'F03','31-OCT-08')
1 row created.
10 rows selected.
Table created.
1 row created.
175
Computer science practical 2015-2016
SQL> /
Enter value for fcode: F02
Enter value for type: COTTON
old 1: insert into FABRIC values('&FCODE','&TYPE')
new 1: insert into FABRIC values('F02','COTTON')
1 row created.
SQL> /
Enter value for fcode: F03
Enter value for type: SILK
old 1: insert into FABRIC values('&FCODE','&TYPE')
new 1: insert into FABRIC values('F03','SILK')
1 row created.
SQL> /
Enter value for fcode: F01
Enter value for type: TERELENE
old 1: insert into FABRIC values('&FCODE','&TYPE')
new 1: insert into FABRIC values('F01','TERELENE')
1 row created.
FCODE TYPE
---------- ------------------------------
F04 POLYSTER
F02 COTTON
F03 SILK
F01 TERELENE
Q1
SQL> select GCODE,DESCRIPTION
2 from GARMENT
3 order by GCODE DESC;
GCODE DESCRIPTION
---------- ------------------------------
10090 TULIP SKIRT
10089 SLACKS
10024 BABY TOP
10023 PENCIL SKIRT
10020 FROCK
10019 EVENING GOWN
176
Computer science practical 2015-2016
10012 INFORMAL
10009 INFORMAL PANT
10007 FORMAL
10001 FORMAL
10 rows selected.
Q2
1 SELECT*
2 from GARMENT
3* where READYDATE BETWEEN '08-DEC-07' AND '16-JUN-08'
4 ;
Q3
1 select avg(PRICE)
2 from GARMENT,FABRIC
3* where FABRIC.FCODE='F03'
4 ;
AVG(PRICE)
----------
1085
Q4
SQL> SELECT MAX(PRICE),MIN(PRICE)
2 FROM GARMENT;
MAX(PRICE) MIN(PRICE)
---------- ----------
1550 750
Q5
SQL> select sum(PRICE)
2 FROM GARMENT
3 WHERE FCODE='F01';
SUM(PRICE)
----------
2600
177
Computer science practical 2015-2016
Q6 1 SELECT DESCRIPTION,TYPE
2 FROM GARMENT,FABRIC
3* WHERE GARMENT.FCODE=FABRIC.FCODE AND GARMENT.PRICE>=1260
4 ;
DESCRIPTION TYPE
------------------------------ ------------------------------
FORMAL TERELENE
INFORMAL COTTON
INFORMAL PANT COTTON
Q7
SQL> SELECT
2 MAX(FCODE)
3 FROM FABRIC;
MAX(FCODE)
----------
F04
Q8
SQL> SELECT
2 COUNT(DISTINCT PRICE)
3 FROM GARMENT;
COUNT(DISTINCTPRICE)
--------------------
7
4)
Q-4 SQL
------------------------------------
178
Computer science practical 2015-2016
Table Created.
------------------------------------
create table material
(
mcode char(4),
type char(10)
)/
Table Created.
-------------------------------------
insert into dress values (&dcode,'&description',&price,'&mcode','&launchdate');
Enter value for dcode: 10001
Enter value for description: Formal Shirt
Enter value for price: 1250
Enter value for mcode: m001
Enter value for launchdate: 12-jan-08
1 row created.
/
Enter value for dcode: 10020
Enter value for description: Frock
Enter value for price: 750
Enter value for mcode: m004
Enter value for launchdate: 09-sep-07
1 row created.
/
Enter value for dcode: 10012
Enter value for description: informal shirt
Enter value for price: 1450
Enter value for mcode: m002
Enter value for launchdate: 06-jun-08
1 row created.
/
Enter value for dcode: 10019
Enter value for description: Evening Gown
Enter value for price: 850
Enter value for mcode: m003
Enter value for launchdate: 06-jun-08
1 row created.
/
Enter value for dcode: 10090
Enter value for description: Tulip Skirt
179
Computer science practical 2015-2016
/
Enter value for dcode: 10023
Enter value for description: Pencil Skirt
Enter value for price: 1250
Enter value for mcode: m003
Enter value for launchdate: 19-dec-08
1 row created.
/
Enter value for dcode: 10089
Enter value for description: Slacks
Enter value for price: 850
Enter value for mcode: m003
Enter value for launchdate: 20-oct-08
1 row created.
/
Enter value for dcode: 10007
Enter value for description: Formal Skirt
Enter value for price: 1450
Enter value for mcode: m001
Enter value for launchdate: 09-mar-08
1 row created.
/
Enter value for dcode: 10009
Enter value for description: Informal Pant
Enter value for price: 1400
Enter value for mcode: m002
Enter value for launchdate: 20-oct-08
1 row created.
/
Enter value for dcode: 10024
Enter value for description: Baby Top
Enter value for price: 650
Enter value for mcode: m003
Enter value for launchdate: 07-apr-07
1 row created.
---------------------------------------------------
180
Computer science practical 2015-2016
---------------------------------------------------------
/
Enter value for mcode: m002
Enter value for type: Cotton
1 row created.
/
Enter value for mcode: m004
Enter value for type: Polyester
1 row created.
/
Enter value for mcode: m003
Enter value for type: Silk
1 row created.
-----------------------------------
181
Computer science practical 2015-2016
m004 Polyester
m003 Silk
-----------------------------------------------------
Questions and Answers
Q1----------------------------------------------------
DCODE DESCRIPTON
---------- --------------------
10001 Formal Shirt
10007 Formal Skirt
10009 Informal Pant
10012 informal shirt
10019 Evening Gown
10020 Frock
10023 Pencil Skirt
10024 Baby Top
10089 Slacks
10090 Tulip Skirt
----------------------------------
Q2
select * from dress where launchdate between '05-dec-07' and '20-jun-08'
/
--------------------------------------------------------------
Q3
AVG(PRICE)
----------
1075
-------------------------------------------------
182
Computer science practical 2015-2016
Q4
MAX(PRICE) MIN(PRICE)
---------- ----------
1450 650
-----------------------------------------------
Q5
SUM(PRICE)
----------
2700
-----------------------------------------------
Q6
DESCRIPTON TYPE
-------------------- ----------
Formal Shirt Terelene
Formal Skirt Terelene
informal shirt Cotton
Informal Pant Cotton
Pencil Skirt Silk
-------------------------------------------------------
Q7
MAX(
----
m004
--------------------------------------------------
Q8
select count(DISTINCT Price) from dress;
COUNT(DISTINCTPRICE)
183
Computer science practical 2015-2016
--------------------
6
184