Professional Documents
Culture Documents
PRACTICAL FILE
BY :
SIDDHANT BANYAL
Roll no:
Session: 2015-2016
-2016
INDEX
Programme
S.NO.
6
7
8
9
-2016
13
14
15
16
17
18
19
20
-2016
-2016
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
24
25
26
27
28
-2016
SQL ques no 1
32 SQL ques no 2
33 SQL ques no 3
34 SQL ques no 4
Signature :_______________________________
-2016
GRADE
F
D
C
B
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;
cout << "Enter name" << endl;
gets(s1[i].name);
cout << "Enter marks in 3 subjects" << endl;
cin >> s1[i].marks1 >> s1[i].marks2 >> s1[i].marks3;
-2016
-2016
-2016
10
-2016
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#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;
11
-2016
}
else if(Kilometers<1000)
{
TotalFare=NoofAdults*300+NoofKids*150;
}
else
{
TotalFare=NoofAdults*500+NoofKids*250;
}
}
void Enter_Tour()
{
cout<<"Enter TCode,No of Adults,No of Kids and Kilometers\n";
gets(TCode);
cin>>NoofAdults>>NoofKids>>Kilometers;
AssignFare();
}
void Show_Tour()
{
cout<<"TCode - "<<TCode<<endl;
cout<<"No of Adults - "<<NoofAdults<<endl;
cout<<"No of Kids - "<<NoofKids<<endl;
cout<<"Kilometers - "<<Kilometers<<endl;
cout<<"Total Fare - "<<TotalFare<<endl;
}
12
-2016
};
void main()
{
clrscr();
Tour T;
T.Show_Tour();
T.Enter_Tour();
T.Show_Tour();
getch();
}
13
-2016
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class outfit {
int OSize;
14
-2016
char OCode[20];
char OType[30];
char OFabric[20];
float OPrice;
voidInitPrice() {
if (OFabric == "DENIM") {
if (strcmp(OType,"TROUSER")) {
OPrice = 1500;
} else if (strcmp(OType,"TROUSER")) {
OPrice = 2500;
}
} else {
if (strcmp(OType,"TROUSER")) {
OPrice = 1500 * (.75);
} else if (strcmp(OType,"JACKET")) {
OPrice = 2500 * (.75);
}
}
}
public:
outfit() {
strcpy(OCode, "NOT INITIALISED");
strcpy(OType, "NOT INITIALISED");
strcpy(OFabric, "NOT INITIALISED");
15
-2016
OSize = 0;
OPrice = 0;
}
void input() {
cout<< "Enter the type of Fabric" <<endl;
cin>>OFabric;
cout<< "Enter the Type of cloth" <<endl;
cin>>OType;
cout<< "Enter the code of the cloth" <<endl;
cin>>OCode;
cout<< "Enter the Size of the cloth" <<endl;
cin>>OSize;
InitPrice();
}
void display() {
cout<< "Code for the cloth is " << " " <<OCode<<endl;
cout<< "Ypur Fabric Type is " << " " <<OFabric<<endl;
cout<< "Your Cloth Type is " << " " <<OType<<endl;
cout<< "Size of the cloth is " << " " <<OSize<<endl;
cout<< "Price of the chosen cloth is" << " " <<OPrice<<endl;
}
};
void main()
16
-2016
{
outfit o1;
cout<<"Enter all the details of the outfit you want to buy"<<endl;
o1.input();
o1.display();
getch();
}
17
-2016
#include<iostream.h>
#include<conio.h>
//liner search
void lsearch(int a[],int n,int item)
{
int i,found=0;
for(i=0;i<n;i++)
18
-2016
{
if (a[i]==item)
{
found=1;
break;
}
}
if(found==1)
{
cout<<"\nFound at "<<++i;
}
else
cout<<"\nNOT FOUND";
}
19
-2016
{
found=1;
break;
}
else if(a[mid]<item)
{
beg=mid+1;
}
else if (a[mid]>item)
{
last=mid-1;
}
}
if (found==1)
cout<<"\n Found at"<<mid+1;
else
cout<<" \n NOT FOUND"<<endl;
}
void main()
{
int a[50],i,size,item;
int ch=0;
clrscr();
cout<<" Enter size : "<<endl;
20
-2016
cin>>size;
cout<<"Enter array of numbers : ";
for(i=0;i<size;i++)
{
cin>>a[i];
}
cout<<"\nEnter item to be searched for :";
cin>>item;
cout<<" PRESS 1 : VIA LINEAR SEARCH
";
";
cin>>ch;
if(ch==1)
{
lsearch(a,size,item);
}
if(ch==2)
{
bsearch(a,size,item);
}
getch();
}
21
-2016
22
-2016
do
{
cout<<"press 1 to insert element in a specific position"<<endl;
cout<<"press 2 to delete an element from a specific position"<<endl;
cout<<"press 3 to assign 0 to all duplicate elements in an array"<<endl;
cout<<"enter ur choice"<<endl;
23
-2016
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
deleteelement();
break;
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;
24
-2016
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"enter position and element"<<endl;
cin>>pos;
cin>>num;
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=num;
cout<<"After insertion,the elements are"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
25
-2016
}
void deleteelement()
{
int a[20],i,n,pos;
cout<<"enter the size of array"<<endl;
cin>>n;
cout<<"enter the elements u want "<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"enter position from where alloted number is to be deleted"<<endl;
cin>>pos;
for(i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
for(i=0;i<n-1;i++)
{
26
-2016
cout<<a[i]<<endl;
}
}
void
duplicate()
{
int a[20],i,j,n,flag;
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter the elements u want"<<endl;
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])
{
27
-2016
a[j]=0;
flag=1;
}
}
if(flag==1)
{
a[i]=0;
}
}
cout<<" after removing duplicate elements :"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
28
-2016
-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>
void rearrange1(int arr1[],int size)
{
int i;
for(i=0;i<size;i++)
{
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;
}
void rearrange2(int arr1[],int size)
30
-2016
{
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++;
}
}
for(i=0;i<size;i++)
cout<<arr2[i]<<" ";
cout<<endl;
}
void rearrange3(int arr1[],int size)
{
int i,j,arr2[10];
for(i=0,j=0;i<size;i++)
{
if(arr1[i]%2==0)
31
-2016
{
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]<<" ";
cout<<endl;
}
void main()
{
int arr1[10],s,ch;
char ans;
do
{
clrscr();
X:
32
-2016
33
-2016
rearrange3(arr1,s);
break;
default:
cout<<"invalid entry"<<endl;
}
cout<<"do you want to continue(y/n)"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
34
-2016
#include<iostream.h>
#include<conio.h>
#include<math.h>
void TwoDtoOneD(int [][100],int);
void OneDtoTwoD(int [],int);
void main()
{
int size,ch,j,i,k=0,A[100][100],B[100];
clrscr();
cout<<"\n1. Conversion of 2D Array to 1D\n";
cout<<"\n2. Conversion of 1D Array to 2D\n";
cout<<"\nEnter your Choice: \n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the size of 2D array"<<endl;
cin>>size;
cout<<"\nEnter the 2D array:\n";
for(i=0;i<size;i++)
{
35
-2016
for(j=0;j<size;j++)
{
cin>>A[i][j];
}
}
TwoDtoOneD(A,size);
break;
case 2:
cout<<"Enter the size of 1D array"<<endl;
cin>>size;
cout<<"\nEnter One D Array\n";
for(i=0;i<size;i++)
{
cin>>B[i];
}
OneDtoTwoD(B,size);
break;
default:
cout<<"Wrong Input";
}
getch();
36
-2016
37
-2016
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
{
A[i][j]=B[k];
k++;
}
}
cout<<"\nContents of converted 2D Array\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}
else
{
cout<<"conversion is not possible"<<endl;
}
}
38
-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 :
If input array contains :
4 27 5 9 10 3 14 7 1 2
then output array should be :
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>
void arr1(int arr[],int size)
{
int i,temp;
39
-2016
40
-2016
temp = arr[i];
arr[i] = arr[i+size/2];
arr[i+size/2] = temp;
}
cout<<"\nNew Array Is";
for(i=0;i<size;i++)
cout<<" "<<arr[i];
}
void arr3(int arr[],int size)
{
int i,j,temp;
cout<<"\nOriginal Array Is";
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;
}
cout<<"\nNew Array Is";
for(i=0;i<size;i++)
cout<<" "<<arr[i];
41
-2016
}
void main()
{
clrscr();
int a[100];
char ans;
int i,size,ch;
cout<<"Enter Size Of The Array"<<endl;
cin>>size;
cout<<"Enter Elements Of The Array"<<endl;
for(i=0;i<size;i++)
cin>>a[i];
do
{
clrscr();
cout<<"Press 1 For First Rearrangement"<<endl;
cout<<"Press 2 For Second Rearrangement"<<endl;
cout<<"Press 3 For Third Rearrangement"<<endl;
cout<<"\nEnter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1 : arr1(a,size);
break;
case 2 : arr2(a,size);
42
-2016
break;
case 3 : arr3(a,size);
break;
default : cout<<"Invalid Case";
break;
}
cout<<"\n\nDo you want to continue(Y/N) : ";
cin>>ans;
}
while(ans=='Y'||ans=='y');
getch();
}
43
-2016
44
-2016
45
-2016
46
-2016
{
clrscr();
int a[100][100];
char ans;
int i,j,size,ch;
cout<<"Enter Size Of The Array"<<endl;
cin>>size;
cout<<"Enter Elements Of The Array"<<endl;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
cin>>a[i][j];
}
}
do
{
clrscr();
cout<<"Press 1 For First Rearrangement"<<endl;
cout<<"Press 2 For Second Rearrangement"<<endl;
cout<<"Press 3 For Third Rearrangement"<<endl;
cout<<"Press 4 For Fourth Rearrangement"<<endl;
cout<<"\nEnter Your Choice"<<endl;
cin>>ch;
switch(ch)
47
-2016
{
case 1 :
arr1(a,size);
break;
case 2 : arr2(a,size);
break;
case 3 :
arr3(a,size);
break;
case 4 :
arr4(a,size);
break;
default : cout<<"Invalid Case";
break;
}
cout<<"\n\nDo you want to continue(Y/N) : ";
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
48
-2016
49
-2016
#include<iostream.h>
#include<conio.h>
void upper1(int arr[][10],int s)
{
int i,j;
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
if(i<=j)
cout<<arr[i][j]<<" ";
else
cout<<" ";
}
cout<<endl;
}
}
void upper2(int arr[][10],int s)
{ int i,j;
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
if(i+j<s)
50
-2016
cout<<arr[i][j]<<" ";
else
cout<<" ";
}
cout<<endl;
}
}
void lower1(int arr[][10],int s)
{ int i,j;
for(i=0;i<s;i++)
{
for(j=0;j<s;j++)
{
if(i>=j)
cout<<arr[i][j]<<" ";
else
cout<<" ";
}
cout<<endl;
}
}
void lower2(int arr[][10],int s)
{ int i,j;
for(i=0;i<s;i++)
{
51
-2016
for(j=0;j<s;j++)
{
if(i+j>=s-1)
cout<<arr[i][j]<<" ";
else
cout<<" ";
}
cout<<endl;
}
}
void main()
{
int i,j,ch,size,arr[10][10];
char ans;
cout<<"Enter the size of 2D array\n";
cin>>size;
cout<<"Enter elements for the 2D array\n";
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
cin>>arr[i][j];
}
}
do
52
-2016
{
clrscr();
cout<<"Press\n";
cout<<"1-Display 1st upper half diagnol elements\n";
cout<<"2-Display 2nd upper half diagnol elements\n";
cout<<"3-Display 1st lower half diagnol elements\n";
cout<<"4-Display 2nd lower half diagnol elements\n";
cin>>ch;
switch(ch)
{
case 1:
{
upper1(arr,size);
break;
}
case 2:
{
upper2(arr,size);
break;
}
case 3:
{
lower1(arr,size);
break;
}
53
-2016
case 4:
{
lower2(arr,size);
break;
}
default:
cout<<"You have entered a wrong choice\n";
}
cout<<"Do you want to continue(y/n)\n";
cin>>ans;
}while(ans=='y');
getch();
}
54
-2016
void main()
{
clrscr();
int a[50][50];
char ans;
int i,j,row,col,ch;
cout<<"Enter no of rows"<<endl;
cin>>row;
55
-2016
cout<<"Enter cols"<<endl;
cin>>col;
cout<<"Enter Elements Of The Array"<<endl;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>a[i][j];
}
}
do
{
clrscr();
cout<<"Press 1 To swap the first and last row"<<endl;
cout<<"Press 2 To swap the first and last column"<<endl;
cout<<"Press 3 To display alternating elements"<<endl;
cout<<"Press 4 To display the elements residing at the edge"<<endl;
cout<<"\nEnter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1 : arr1(a,row,col);
break;
case 2 : arr2(a,row,col);
56
-2016
break;
case 3 : arr3(a,row,col);
break;
case 4 : arr4(a,row,col);
break;
default : cout<<"Invalid Case";
break;
}
cout<<"\n\nDo you want to continue(Y/N) : ";
cin>>ans;
}
while(ans=='Y'||ans=='y');
getch();
}
57
-2016
for(j=0;j<c;j++)
{
temp = arr[0][j];
arr[0][j] = arr[r-1][j];
arr[r-1][j] = temp;
}
cout<<"\nNew Array Is"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
}
void arr2(int arr[][50],int r,int c)
{
int i,j,temp;
cout<<"\nOriginal Array Is"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
58
-2016
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;
}
cout<<"\nNew Array Is"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
}
void arr3(int arr[][50],int r,int c)
{
int i,j,k,temp;
cout<<"\nOriginal Array Is"<<endl;
for(i=0;i<r;i++)
{
59
-2016
for(j=0;j<c;j++)
cout<<" "<<arr[i][j];
cout<<endl;
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(k%2==0)
{
cout<<" "<<arr[i][j];
}
else cout<<" ";
k++;
}
cout<<endl;
60
-2016
}
}
cout<<endl;
}
}
void arr4(int arr[][50],int r,int c)
{
int i,j,temp;
cout<<"\nOriginal Array Is"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<" "<<arr[i][j];
} cout<<endl;
}
cout<<"\nNew Array Is"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==0||j==0||i==r-1||j==c-1)
{
61
-2016
cout<<" "<<arr[i][j];
}
else
{
cout<<" ";
}
cout<<endl;
}
62
-2016
63
-2016
void DeleteNodeLast();
void TraverseNode();
};
void main()
{
clrscr();
list l;
int ch;
char ans;
do
{
cout<<"Press 1 to Add Node TO The Last"<<endl;
cout<<"Press 2 to Delete Node From The Last"<<endl;
cout<<"Press 3 to Display All The Nodes"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
l.AddNodeLast();
break;
case 2:
l.DeleteNodeLast();
break;
case 3:
64
-2016
l.TraverseNode();
break;
default:
cout<<"Invalid choice entered"<<endl;
}
cout<<"Want to confirm"<<endl;
cin>>ans;
}
while(ans=='y');
getch();
}
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
{
65
-2016
last->next=Nodeptr;
}
last=Nodeptr;
cout<<"Node Added To The Last"<<endl;
}
void list::DeleteNodeLast()
{
Node*temp;
if(start==NULL)
{
cout<<"Cannot delete"<<endl;
}
else
for(temp=start;temp->next!=last;temp=temp->next);
temp->next=NULL;
cout<<"Deleted info is"<<last->info<<endl;
delete last;
last=temp;
}
void list::TraverseNode()
{
if(start==NULL)
{
cout<<"Cannot display"<<endl;
}
66
-2016
else
{
Node*temp;
for(temp=start;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}
67
-2016
PROGRAM
#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;
68
-2016
public:
list()
{
start=NULL;
}
void addnode();
void deletenode();
void traversenode();
};
void list::addnode()
{
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)
{
cout<<"Cannot Delete Node"<<endl;
}
69
-2016
else
{
node *temp;
temp=start;
cout<<"Deleted Info is"<<temp->info<<endl;
start=temp->next;
temp->next=NULL;
delete 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;
}
}
}
70
-2016
void main()
{
list l;
int ch;
char ans;
do
{
cout<<"Press 1 to Add Node"<<endl;
cout<<"Press 2 to Delete Node"<<endl;
cout<<"Press 3 to Display Node"<<endl;
cout<<"Enter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
l.addnode();
break;
case 2:
l.deletenode();
break;
case 3:
l.traversenode();
break;
default:
cout<<"Invalid choice entered"<<endl;
71
-2016
}
cout<<"Wish to continue?"<<endl;
cin>>ans;
}
while (ans=='y');
getch();
}
72
-2016
73
-2016
public:
Stack()
{
top=NULL;
}
void push();
void pop();
void traverse();
};
void Stack::push()
{
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;
}
74
-2016
else
{
Node *temp;
temp=top;
cout<<"popped info is "<<temp->info<<endl;
top=top->next;
temp->next=NULL;
delete temp;
}
}
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()
75
-2016
{
clrscr();
int i;
char c;
Stack s;
Cout<<1-Push
2-Pop
3-Display;
do
{
cin>>i;
switch(i)
{
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;
76
-2016
}
while(c=='y');
getch();
77
-2016
78
-2016
}
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";
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;
79
-2016
delete temp;
}
}
void queue::display()
{
if(front==NULL)
{
cout<<"\n"<<"Cannot display";
}
else
{
node *temp;
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;
80
-2016
case 3:
q1.display();
break;
default:
cout<<"\n"<<"WRONG CHOICE!!!";
}
cout<<"\n"<<"Want to continue"<<endl;
cin>>ans;
}while(ans=='y');
getch();
}
81
-2016
82
-2016
res=Push(stack,top,Item);
if(res == -1)
{
cout<<"Overloading...Aborting Sequence!!!"<<endl;
break:
}
cout<<"The Stack now is:"<<endl;
Display(stack,top);
}
cout<<"Enter More Elements? (y/n)<<endl;
cin>>ch;
}
break:
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:
83
Display(Stack,top);
break:
Default:
cout<<"Invalid input. Try again"<<endl;
break:
}
getch();
}
Int Push(int Stack[ ],int & top, int ele)
Elements
{
if (top == size-1)
{
return -1;
}
else
{
top + +;
stack[top]=ele;
}
}
Int Pop( int stack [ ], int & top)
elements
{
int ret;
if(top == -1)
{
return -1;
}
else
{
ret = stack[top];
top --;
}
return ret;
-2016
//Function to insert
//Function to Delete
84
-2016
}
Void display (int Stack[ ],int top)
{
if (top == -1)
{
return;
}
else
{
cout<<Stack[top]<<"<--"<<endl;
for(int i = top - 1;i > = 0; i--)
{
cout<<Stack[i]<<endl;
}
}
}
85
-2016
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int queue[10], front, rear, ch, i;
char ans;
front=rear=-1;
do
{
cout<<"Press 1 to insert"<<endl;
cout<<"Press 2 to delete"<<endl;
cout<<"Press 3 to display"<<endl;
cout<<"ENTER YOUR CHOICE"<<endl;
cin>>ch;
switch(ch)
86
-2016
{
case 1:
if(front==-1)
{
front=0;
}
rear=rear+1;
if(rear>9)
{
cout<<"can not insert"<<endl;
rear=rear-1;
}
else
{
cout<<"enter the element"<<endl;
cin>>queue[rear];
}
break;
case 2:
if(front==-1)
{
cout<<"can not delete"<<endl;
}
else
{
87
-2016
88
-2016
}
while(ans=='y');
getch();
}
89
-2016
90
-2016
};
void queue::qinsert() {
if ((f == 0 && r == size - 1) || (f == r + 1)) {
cout << "queue is full";
} else if (r == -1) {
f = 0;
r = 0;
cout << "enter the data:";
cin >> data[r];
} else if (r == size - 1) {
r = 0;
cout << "enter the data";
cin >> data[r];
} else {
r++;
cout << "enter the data";
cin >> data[r];
}
}
void queue::qdelete() {
if (f == -1) {
cout << "queue is empty";
} else if (f == r) {
cout << data[f] << "deleted";
f = -1;
91
-2016
r = -1;
} else if (f == size - 1) {
cout << data[f] << "deleted";
f = 0;
} else {
cout << data[f] << "deleted";
f++;
}
}
void queue::qdisplay() {
if (f == -1) {
cout << "queue is empty";
} else {
int i;
if (f <= r) {
for (i = f; i <= r; i++) {
cout << data[i] << endl;
}
} else {
for (i = 0; i < size; i++) {
cout << data[i] << endl;
}
}
}
92
-2016
void main()
{
clrscr();
char ch = 'y';
int choice;
queue q1;
do {
cout << "press 1 for enter" << endl;
cout << "press 2 for delete" << endl;
cout << "press 3 for display" << endl;
cin >> choice;
switch (choice) {
case 1:
q1.qinsert();
break;
case 2:
q1.qdelete();
break;
93
-2016
case 3:
q1.qdisplay();
break;
default:
cout << "Invalid choice" << endl;
}
cout << "want to continue(y/n)" << endl;
cin >> ch;
} while (ch == 'y');
getch();
}
94
-2016
95
-2016
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;
}
void student:: output()
{
cout<<"R no, name and marks of the given student"<<endl;
cout<<"roll no is"<<rollno<<", "<<"name is "<<name<<" and "<<" marks obtained is
"<<marks<<endl;
}
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;
96
-2016
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();
}
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));
97
-2016
}
f1.close();
if(flag==0)
{
cout<<"student does not exist"<<endl;
}
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();
98
-2016
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;
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();
}
99
-2016
100
1)
2)
3)
4)
-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()
101
-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();
102
-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();
}
103
-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;
104
-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();
105
-2016
break;
default:cout<<"wrong choice\n";
}
cout<<"want to continue? (y/n)\n";
cin>>ans;
}while(ans=='y');
getch();
}
106
-2016
Search record
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{
107
-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;
}
}
108
-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));
}
}
f1.close();
f2.close();
remove("student.dat");
rename("temp.dat","student.dat");
109
-2016
}
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';
do{
cout<<"\n PRESS 1 : CREATE A NEW FILE";
cout<<"\n PRESS 2 : DISPLAY FILE ";
110
cout<<"\n PRESS
cout<<"\n PRESS
cout<<"\n PRESS
cout<<"\n PRESS
cout<<endl;
3
4
5
6
:
:
:
:
-2016
UPDATE DATA";
SEARCH RECORD";
REMOVE RECORD ";
INSERT RECORD ";
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();
}
111
-2016
112
-2016
113
-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;
}
int R, flag, ch;
char ans, name[20];
student s1;
fstream f1;
void addrecord();
void deleterecord();
void toppers();
void sname();
void displayrecord();
void main()
{
do
{
clrscr();
cout<<"MENU"<<endl;
cout<<"1. Add Rec"<<endl;
114
-2016
115
-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());
if(temparr[0] == 'a' || temparr[0] == 'A')
{
s1.output();
}
f1.read((char*)&s1, sizeof(s1));
}
f1.close();
}
116
-2016
Else
D grade
117
-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;
}
118
-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;
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)
119
-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;
120
-2016
cin>>ans;
}
while (ans=='y'||ans=='Y');
getch();
}
121
1)
2)
3)
4)
-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();
122
-2016
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;
}
void student:: output()
{
cout<<"R no, name and marks of the given student"<<endl;
cout<<"roll no is"<<rollno<<", "<<"name is "<<name<<" and
"<<" marks obtained is "<<marks<<endl;
}
int student::getRN()
{
return rollno;
}
char *student::getName()
{
return name;
}
float student::getMarks()
{
return marks;
123
-2016
}
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();
}
void searchrecord()
{
flag=0;
124
-2016
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();
}
}
void deleterecord()
{
cout<<"enter name of the student to be deleted"<<endl;
gets(name);
125
-2016
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;
switch(ch)
{
case 1:
addrecord();
break;
126
-2016
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();
}
127
-2016
-2016
129
-2016
130
-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));
}
131
-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) : ";
132
-2016
cin>>ans;
}
}while(ans=='y');
getch();
}
133
-2016
#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()
{
134
-2016
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();
135
-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();
136
-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");
137
-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");
}
138
-2016
TEXT FILE
139
-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()
140
-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++;
141
-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++;
}
142
-2016
143
-2016
}
void main()
{
clrscr();
cout<<"_______MENU BASED PROGRAM ON TEXT
FILE______"<<endl;
int ch=0;
char opt='n';
do{
cout<<"\n PRESS 1: CREATE A NEW FILE";
cout<<"\n PRESS 2 : DISPLAY FILE ";
cout<<"\n PRESS 3 : ADD MORE DATA";
cout<<"\n PRESS 4 : FILE ANALYSIS ( count no of
aphabets vowels, consonants ,digits,space)";
cout<<"\n PRESS 5 : COUNT NUMBER OF WORDS AND
this ";
cout<<"\n PRESS 6 : COUNT NUMBER OF LINES AND
LINES STARTING WITH t ot T ";
cout<<"\n PRESS 7 : REPLACE ALL SPACES BY HYPHENS
- ";
cin>>ch;
switch(ch)
{
case 1:create();
break ;
case 2:display();
break ;
case 3:addmore();
break ;
case 4:count();
break ;
case 5:countthis();
break ;
case 6:countthis();
break ;
case 7:misc();
144
-2016
break ;
}
cout<<" \n want to go back ? (y/n)";
cin>>opt;
}while(opt=='y');
getch();
}
145
-2016
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.
146
-2016
1 row created.
1 row created.
147
-2016
1 row created.
148
-2016
1 row created.
149
-2016
1 row created.
1 row created.
Enter value for fl_no: IC799
Enter value for starting: BANGLORE
Enter value for ending: DELHI
Enter value for no_flights: 2
Enter value for no_stops: 1
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('IC799','BANGLORE','DELHI',2,1)
1 row created.
150
-2016
151
-2016
152
-2016
153
-2016
SQL> /
Enter value for fl_no: MU499
Enter value for airlines: Sahara
Enter value for fare: 9400
Enter value for tax: 5
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('MU499','Sahara',9400,5)
1 row created.
SQL> AM501
SP2-0042: unknown command "AM501" - rest of line ignored.
SQL> /
Enter value for fl_no: AM501
Enter value for airlines: Jet Airways
Enter value for fare: 13450
Enter value for tax: 8
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('AM501','Jet Airways',13450,8)
1 row created.
SQL> /
Enter value for fl_no: IC899
Enter value for airlines: Indian Airlines
Enter value for fare: 8300
Enter value for tax: 4
old 1: insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
new 1: insert into FARES values('IC899','Indian Airlines',8300,4)
154
-2016
1 row created.
155
-2016
1 row created.
SQL> select*from FLIGHTS
2 /
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
MUMBAI
DELHI
IC799
BANGLORE
DELHI
MC101
INDORE
MUMBAI
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
DELHI
8
156
AM812
KANPUR
BANGLORE
IC899
-2016
MUMBAI
KOCHI
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
DELHI
TRIVANDRUM
MU499
MUMBAI
MADRAS
IC701
AHMEDABAD
DELHI
4
FL_NO
NO_FLIGHTS
157
-2016
------------------------------ ---------AM812
2)
1* select FL_NO , NO_FLIGHTS from FLIGHTS where STARTING ='KANPUR'and
ENDING='BANGLORE'
2 /
FL_NO
NO_FLIGHTS
------------------------------ ---------AM812
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
DELHI
TRIVANDRUM
AM812
BANGLORE
KANPUR
3
158
IC301
-2016
MUMBAI
DELHI
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
DELHI
MUMBAI
IC701
DELHI
AHMEDABAD
IC799
BANGLORE
DELHI
FL_NO
STARTING
------------------------------ -----------------------------ENDING
NO_FLIGHTS NO_STOPS
MC101
MUMBAI
1
INDORE
159
MUMBAI
MU499
-2016
MUMBAI
MADRAS
3)
1 select FLIGHTS.FL_NO , FARE+TAX from FARES , FLIGHTS
2 where STARTING='DELHI' and ENDING='MUMBAI'
3 and
4* FLIGHTS.FL_NO=FARES.FL_NO
SQL> /
FL_NO
FARE+TAX
------------------------------ ---------IC302
4310
MIN(FARE)
---------4300
5)
1 select FLIGHTS.FL_NO, NO_FLIGHTS, AIRLINES from FLIGHTS, FARES where
160
-2016
FL_NO
NO_FLIGHTS
------------------------------ ---------AIRLINES
---------------------------------------AM501
JET AIRWAYS
IC302
INDIAN AIRLINES
IC701
INDIAN AIRLINES
6)
SQL> select count(distinct ENDING) from FLIGHTS
2 /
COUNT(DISTINCTENDING)
--------------------7
____________________________________________________
161
-2016
2
1
2
3
4
5
6
7
162
-2016
163
-2016
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
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(451,'pat','thompson','11 red road','paris')
164
-2016
1 row created.
W_ID FIRSTNAME
LASTNAME ADDRESS
CITY
---------- -------------------- ---------------------------- ------------ ------------102 sam
tones33 elm stparis
105sarahackerman
440 U.S.110
new delhi
144 manila
sengupta24 friends street
new delhi
210 george
smith83 first street
howard
255 mary jones842 vine avelosantiville
300 robertSamuel
9 fifth cross
washington
335 henry
Williams
12 moore street
boston
403ronny
lee121 harrisonst
new york
451pat
Thompson
11 red road
paris
9 rows selected.
1
2
3
4
5
6
165
-2016
SQL> /
Enter value for w_id: 255
166
-2016
167
-2016
LASTNAMEADDRESS
CITY
168
-2016
1 select sum(benifits)
2 fromdesig
3* where designation='salesman'
SQL> /
SUM(BENIFITS)
169
-2016
------------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)
170
-2016
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.
SQL> desc GARMENT;
Name
Null? Type
----------------------------------------- -------- -------------GCODE
NUMBER(38)
DESCRIPTION
CHAR(30)
PRICE
NUMBER(38)
FCODE
CHAR(10)
READYDATE
DATE
Enter value for gcode: 10023
Enter value for description: PENCIL SKIRT
Enter value for price: 1150
Enter value for fcode: F03
Enter value for readydate: 19-DEC-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10023,'PENCIL SKIRT',1150,'F03','19-DEC-08')
1 row created.
SQL> /
Enter value for gcode: 10001
Enter value for description: FORMAL
Enter value for price: 1250
Enter value for fcode: F01
Enter value for readydate: 12-JAN-08
171
-2016
172
-2016
173
-2016
174
-2016
175
-2016
10001 FORMAL
10 rows selected.
Q2
1 SELECT*
2 from GARMENT
3* where READYDATE BETWEEN '08-DEC-07' AND '16-JUN-08'
4 ;
GCODE DESCRIPTION
PRICE FCODE
READYDATE
---------- ------------------------------ ---------- ---------- --10001 FORMAL
1250 F01
12-JAN-08
10012 INFORMAL
1550 F02
06-JUN-08
10019 EVENING GOWN
850 F03
06-JUN-08
10007 FORMAL
1350 F01
09-MAR-08
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
Q6 1 SELECT DESCRIPTION,TYPE
2 FROM GARMENT,FABRIC
176
-2016
4)
Q-4 SQL
-----------------------------------create table dress
(
dcode number(5),
descripton char(20),
price number(5),
mcode char(4),
launchdate date
);
/
Table Created.
------------------------------------
177
-2016
178
-2016
1 row created.
/
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.
--------------------------------------------------select * from dress
;
179
-2016
DCODE DESCRIPTON
PRICE MCOD LAUNCHDAT
----- -------------------- ---------- ---- --------10001 Formal Shirt
1250 m001 12-JAN-08
10020 Frock
750 m004 09-SEP-07
10012 informal shirt
1450 m002 06-JUN-08
10019 Evening Gown
850 m003 06-JUN-08
10090 Tulip Skirt
850 m002 31-MAR-07
10023 Pencil Skirt
1250 m003 19-DEC-08
10089 Slacks
850 m003 20-OCT-08
10007 Formal Skirt
1450 m001 09-MAR-08
10009 Informal Pant
1400 m002 20-OCT-08
10024 Baby Top
650 m003 07-APR-07
--------------------------------------------------------insert into material values ('&mcode','&type')
Enter value for mcode: m001
Enter value for type: Terelene
1 row created.
/
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.
----------------------------------select * from material;
MCOD TYPE
---- ---------m001 Terelene
m002 Cotton
m004 Polyester
m003 Silk
-----------------------------------------------------
180
-2016
181
-2016
MAX(PRICE) MIN(PRICE)
---------- ---------1450
650
----------------------------------------------Q5
select sum(price) from dress where
mcode='m001';
SUM(PRICE)
---------2700
----------------------------------------------Q6
select descripton,type from dress,material
where dress.mcode=material.mcode and dress.price>=1250;
DESCRIPTON
TYPE
-------------------- ---------Formal Shirt
Terelene
Formal Skirt
Terelene
informal shirt
Cotton
Informal Pant
Cotton
Pencil Skirt
Silk
------------------------------------------------------Q7
select max(mcode) from material;
MAX(
---m004
-------------------------------------------------Q8
select count(DISTINCT Price) from dress;
COUNT(DISTINCTPRICE)
-------------------6
182
-2016
183