You are on page 1of 183

COMPUTER

PRACTICAL FILE

BY :
SIDDHANT BANYAL
Roll no:
Session: 2015-2016

Computer science practical

-2016

INDEX
Programme

S.NO.

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
5

6
7

8
9

Write a menu based program using function to perform the following


(i) Sort an integer type of array using bubble sort.
(ii) Sort an integer type of array using selection sort.
(iii) Sort an integer type of array using insertion sort.
Write a menu driven programme using function to perform the following
(i) Insert element in specific position
(ii) Delete an element from a specific position
(iii) Assign 0 to all duplicate nos.
Write a menu based program using function to do the following.
if input array contains
3, 7, 9, 6, 10, 8, 14, 5, 4, 12
Then output array should contain the following
6,14,18, 18, 30, 24, 42, 10, 12, 36
3, 7, 9, 5, 12, 4, 14, 8, 10, 6
3, 7, 9, 5, 6, 10, 8, 14, 4, 12
Write a menu based program using function to perform the following
(i) convert 1D to 2D array
(ii) convert 2D to 1D Array
Write a menu based program using function to perform the following

If 1-D array contains the elements


1, 2,3

Computer science practical

-2016

Then output array 2 D should contain the following


(i)
123
120
100
(ii)
100
120
123
(iii)
123
023
003
Write a menu based program system function to do the following for
example if input is 1 2 3 4 5 6 7 8 9
Then output should be
(i)
125
450
700
(ii)
123
056
007
(iii)
100
450
10 7 8 9
Write a menu based program using function to do the following. if input array
contains
4, 27, 5 ,9, 10, 3, 14, 7, 1, 2
Then output array should contain the following
27, 4, 9, 5, 3, 10, 7, 14, 2, 1
3, 14, 7, 1, 2, 4, 27, 5, 9, 10
11 2,1, 7, 14, 3, 10, 9, 5, 27, 4
12
Write a menu based program to do the following.
1) sum of both the diagonals

Computer science practical

13

14

15

16

17

18

19

20

-2016

2 ) sum of each row


3) sum of each column
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
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
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
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
3) display all nodes
Write a menu based program to do the following functions within
stack as a linked list
1) Push
2) Pop
3) Display
Write a menu based program to do the following in linked queue
1) Insertion of a node
2) Deletion of a node
3) Display all the nodes
Write a menu based program to do the following in stack as an array
1) Push
2) Pop
3) Traverse
Write a menu based program to do the following in a queue as an array
1) Insertion
2) Deletion
3) Display
Write a menu based program to do the following in a circular queue as an array

Computer science practical

-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

2) display all the records


3) search a record
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
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
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
4) display the info of all the students whose 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) count and display the total no of students in each grade

Computer science practical

-2016

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) delete info of all the students whose name doesn't starts with a and
store
info in a backup file
29
4) delete info of all the students whose name ends with a
Write a menu based program to read a text file and perform the following
1) count the total no of characters
2) count the total no of alphabets, spaces ,words and other characters
3) count the total no of words do" present
4) count the total no of lines starting with T
30
5) Replace all the spaces with hyphen (-)
31

SQL ques no 1
32 SQL ques no 2
33 SQL ques no 3
34 SQL ques no 4

Signature :_______________________________

Computer science practical

-2016

WAP using classes to stimulate result


preparation system for 10 students
PERCENTAGE MARKS
<50
>=50<60
>=60<75
>=75<90
>=90<100

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;

Computer science practical

-2016

cout << endl;


}
int student::getpercent()
{
int per;
per = (s1[i].marks1 + s1[i].marks2 + s1[i].marks3) / 3;
return per;
}
char student::getgrade()
{
int p;
p = getpercent();
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()
{
cout << endl;
cout << "-----------------------------------" << endl;
cout << "
RESULT " << endl;

Computer science practical

-2016

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();
}

Computer science practical

-2016

Define a class tour in C++

10

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

};
void main()
{
clrscr();
Tour T;
T.Show_Tour();
T.Enter_Tour();
T.Show_Tour();
getch();
}

13

Computer science practical

-2016

Define a class outfit in C++

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class outfit {
int OSize;

14

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

{
outfit o1;
cout<<"Enter all the details of the outfit you want to buy"<<endl;
o1.input();
o1.display();
getch();
}

17

Computer science practical

-2016

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

#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

Computer science practical

-2016

{
if (a[i]==item)
{
found=1;
break;
}
}
if(found==1)
{
cout<<"\nFound at "<<++i;
}
else
cout<<"\nNOT FOUND";
}

//Assuming array in ascending order


void bsearch(int a[],int size,int item)
{
int beg,last,mid,found=0;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(a[mid]==item)

19

Computer science practical

-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

Computer science practical

-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

";

cout<<" PRESS 2 : VIA BINARY SEARCH

";

cin>>ch;
if(ch==1)
{
lsearch(a,size,item);
}
if(ch==2)
{
bsearch(a,size,item);

}
getch();
}

21

Computer science practical

-2016

22

Computer science practical

-2016

Write a menu driven programme using


function to perform the following
(i)
Insert element in specific position
(ii) Delete an element from a specific
position
(iii) Assign 0 to all duplicate nos.
#include<iostream.h>
#include<conio.h>
void insert();
void deleteelement();
void duplicate();
int a[20],ch ;
char ans;
void main()
{
clrscr();

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

Computer science practical

-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

Computer science practical

-2016

cout<<"enter the size of the 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 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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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:
3 7 9 6 10 8 14 5 4 12
then output array should be:
1) 6 14 18 18 30 24 42 10 12 36
29

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

cout<<"enter size of the arr(1-10)-";


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];
}
cout<<"press 1-1st rearrangement\n"<<endl;
cout<<"press 2-2nd rearrangement\n"<<endl;
cout<<"press 3-3rd rearrangement\n"<<endl;
cin>>ch;
switch(ch)
{
case 1:
rearrange1(arr1,s);
break;
case 2:
rearrange2(arr1,s);
break;
case 3:

33

Computer science practical

-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();
}

Write a Menu Driven Program using


function to:
1. Convert One D Array to Two D
Array

34

Computer science practical

-2016

2. Convert Two D Array to One


D Array

#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

Computer science practical

-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

Computer science practical

-2016

void TwoDtoOneD(int B[][100],int size)


{
int A[100],i,j,k=0;
for (i=0; i<size;i++)
{
for (j=0; j<size;j++)
{
A[k]=B[i][j];
k++;
}
}
cout<<"\nNew 1D Array: \n";
for(k=0; k<size*size; k++)
cout <<A[k]<<"\n";
}
void OneDtoTwoD(int B[],int size)
{
int A[100][100],j,k=0;
int s=sqrt(size);
if(pow(s,2)==size)
{
cout<<"Conversion possible"<<endl;

37

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

cout<<"\nOriginal Array Is";


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];
arr[i+1] = temp;
}
cout<<"\nNew Array Is";
for(i=0;i<size;i++)
cout<<" "<<arr[i];
}
void arr2(int arr[],int size)
{
int i,temp;
cout<<"\nOriginal Array Is";
for(i=0;i<size;i++)
{
cout<<" "<<arr[i];
}
for(i=0;i<size/2;i++)
{

40

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

a menu driven program to do the following 2d manipulations


1)sum of both the diagonals
2)sum of each row
3)sum of each column
4)display the middle row and middle column elements
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void arr1(int arr[][100],int size)
{
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];
}
}
}
cout<<"sum of diagonal is"<<ds<< endl;
}

44

Computer science practical

-2016

void arr2(int arr[][100],int size)


{
int i,j;
int rs=0;
for(i=0;i<size;i++)
{
rs=0;
for(j=0;j<size;j++)
{
rs=rs+arr[i][j];
}
cout<<"sum of row "<<i+1<<"is"<<rs<<endl;
}
}

void arr3(int arr[][100],int size)


{
int i,j;
for(i=0;i<size;i++)
{
int cs=0;
for(j=0;j<size;j++)
{
cs=cs+arr[j][i];
}

45

Computer science practical

-2016

cout<<"sum of column "<<i+1<<"is"<<cs<<endl;


}
}
void arr4(int arr[][100],int size)
{
if(size%2==0)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i==size/2||j==size/2)
{
cout<<arr[i][j];
}
}
cout<<endl;
}
}
else
{
cout<<"Can not display middle row and middle column"<<endl;
}
}
void main()

46

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

a menu driven program to pass a 2D


array and its size as a parameter
and perform the following operations
1-Display 1st upper half diagnol
elements
2-Display 2nd upper half diagnol
elements
3-Display 1st lower half diagnol
elements
4-Display 2nd lower half diagnol
elements

49

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

menu based program to pass a 2-D array and its


size as a parameter to a function and perform the
following operations:
1) Swap the first and last row
2) Swap the first and last column
3) Display alternating elements
4) Display the elements residing at the edge of
the array
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void arr1(int arr[][50],int r,int c);
void arr2(int arr[][50],int r,int c);
void arr3(int arr[][50],int r,int c);
void arr4(int arr[][50],int r,int c);

void main()
{
clrscr();
int a[50][50];
char ans;
int i,j,row,col,ch;
cout<<"Enter no of rows"<<endl;
cin>>row;

55

Computer science practical

-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

Computer science practical

-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();
}

void arr1(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;

57

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

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++)
{
k=0;

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

Computer science practical

-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

Computer science practical

-2016

cout<<" "<<arr[i][j];
}
else
{
cout<<" ";
}

cout<<endl;
}

62

Computer science practical

-2016

A MENU BASED PROGRAM TO PERFORM THE


FOLLOWING OPERATIONS IN THE LINKED LIST
1)ADD NODE TO THE LAST
2)DELETE NODE FROM THE LAST
3)DISPLAY ALL THE NODES
#include<iostream.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
};
class list
{
Node*start;
public:
list()
{
start=NULL;
}
void AddNodeLast();

63

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

else
{
Node*temp;
for(temp=start;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}

Write a menu driven program to perform the


following operation in the linked list

67

Computer science practical

-2016

1)add node to the beginning


2)delete node from the beginning
3)Display all the nodes

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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

}
cout<<"Wish to continue?"<<endl;
cin>>ans;
}
while (ans=='y');
getch();
}

72

Computer science practical

-2016

FUNCTIONS WITHIN A STACK AS A LINKED LIST:


1. PUSH
2. POP
3. TRAVERSE
WHERE EACH NODE CONTAINS THE FOLLOWING INFORMATION
struct NODE
{
int rn;
char name[20];
node *next;
};
______________________________________________________________________
#include<iostream.h>
#include<conio.h>
struct Node
{
int info;
Node *next;
};
class Stack
{
Node *top;

73

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

}
while(c=='y');
getch();

77

Computer science practical

-2016

Write a menu based program to do the following in


linked queue
1) Insertion of a node
2) Deletion of a node
3) Display all the nodes

Where each node contains the following information


struct Node
{
int rn;
char name[20];
node*next;
};
*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int rn;
char name[20];
node *next;
};
class queue
{
node *front,*rear;
public:
queue()
{
front=rear=NULL;

78

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

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

-2016

Write a menu based program to do the following in


stack as an array
1) Push
2) Pop
3) Traverse
#include <iostream.h>
#include <conio.h>
#include<process.h>
//for exit function exit()
int pop(int[ ],int&)
int push(int[ ],int&,int)
void display(int[],int&,int)
const int size=50;
void main()
{
int stack[size],Item,top=-1,res;
int ans;
char ch = 'y' ;
cout<<"Enter Your choice"<<endl;
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Traverse"<<endl;
cin>>ans;
switch (ans)
{
case 1:
while( ch == 'y'||ch == 'Y')
{
cout<<"Enter ITEM for discussion<<endl;
cin>>Item;

82

Computer science practical

-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

Computer science practical

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

Computer science practical

-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;
}
}
}

//Function to display Elements

85

Computer science practical

-2016

Write a menu based program to do the following in


a queue as an array
1) Insertion
2) Deletion
3) Display

#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

Computer science practical

-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

Computer science practical

-2016

cout<<"deleted information is"<<queue[front]<<endl;


front=front+1;
if(front>rear)
{
front=rear=-1;
}
}
break;
case 3:
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;

88

Computer science practical

-2016

}
while(ans=='y');
getch();
}

89

Computer science practical

-2016

Write a menu based program to do the following in


a circular queue as an array
1) Insertion
2) Deletion
3) Display

#include < iostream.h >


#include < conio.h >
#include < stdio.h >
#define size 5
class queue {
int data[size], f, r;
public:
queue() {
f = -1;
r = -1;
}
void qinsert();
void qdelete();
void qdisplay();

90

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

Write a menu based program to perform the following


operations on a binary file "stud.dat" :
1) Add a new record
2) Display all the record
3) Search record based on name
4) Delete record based on name
The definition of the class is as following :
class student
{
int rollno;
char name[20];
float marks;
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();

95

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

Write a menu based program to perform the following


operation on a binary file stud.dat

100

Computer science practical

1)
2)
3)
4)

-2016

Add new record


Display all the record
Search record based on marks
Delete record based on marks

#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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

break;
default:cout<<"wrong choice\n";
}
cout<<"want to continue? (y/n)\n";
cin>>ans;
}while(ans=='y');
getch();
}

MENU BASED PROGRAM:


STUDENTS NAME AND ROLLNUMBER
Create a new binary file
Display data in the file
Update data in file
Insert data in file at correct position

106

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

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

Computer science practical

-2016

112

Computer science practical

-2016

Write a menu based program to perform the


following operation on a binary file stud.dat
1) Add new record
2) Display all the record
3) Display the information of all the students
whose percentage is between 80 to 90
4) Display the information of all the students
whose name starts with A
#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;
}

113

Computer science practical

-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

Computer science practical

-2016

cout<<"2. Display Rec"<<endl;


cout<<"3. Display rec of students with 80 - 90 % marks"<<endl;
cout<<"4. Show rec of students with name starting with A"<<endl;
cin>>ch;
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);

115

Computer science practical

-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

Computer science practical

-2016

Write a menu based program to perform the following


operation on a binary file " STUD.dat"
1) Add new record
2) Display all the record
3) Count and display the total number of students in each
grade
Criteria for the grade is as follows
>90

>80 and <=90

>70 and <=80

Else

D grade

117

Computer science practical

-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

Computer science practical

-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)

//* A-counter,B-counter etc.

119

Computer science practical

-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

Computer science practical

-2016

cin>>ans;
}
while (ans=='y'||ans=='Y');
getch();
}

Write a menu based program to perform the


following operations on a binary file "stud.dat" :

121

Computer science practical

1)
2)
3)
4)

-2016

Add a new record


Display all the record
Search record based on name
Delete record based on name

The definition of the class is as following :


class student
{
int rollno;
char name[20];
float marks;
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();

122

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

Write a menu based program to perform the


following operatio on a binary file
"STUD.dat".
1) Add new record
128

Computer science practical

-2016

2) Display all the record


3) Search record
4) Update record
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
class student
{
char name[20];
introllno;
float marks;
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);

129

Computer science practical

-2016

cout<<" Enter Information \n";


S1.input();
f1.write((char*)&S1, sizeof(S1));
cout<<" **Info added** \n";
f1.close();
}
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));
}

130

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

cin>>ans;
}
}while(ans=='y');
getch();
}

Write a menu based program to perform the following


operation on a binary file "stud.dat"
1. add new record.
2. display all the record
3. delete info of all students whose name doesnt
start with a and store deleted info in backup file

133

Computer science practical

-2016

4. delete info of all students whose name ends with a

definition of class is as followsclass 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()
{

134

Computer science practical

-2016

cout<<" Enter roll number"<<endl;


cin>>rollno;
cout<<"enter name"<<endl;
gets(name);
cout<<"enter marks"<<endl;
cin>>marks;
}
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();

135

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

TEXT FILE

139

Computer science practical

-2016

MENU BASED PROGRAM:

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

Computer science practical

-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

Computer science practical

-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

Computer science practical

-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('-');
}
}
f.close();

143

Computer science practical

-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

Computer science practical

-2016

break ;
}
cout<<" \n want to go back ? (y/n)";
cin>>opt;
}while(opt=='y');
getch();
}

SQL BASED DATABASE


1
1 create table FLIGHTS
2 (

145

Computer science practical

-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* insert into FLIGHTS


values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)

Enter value for fl_no: IC301


Enter value for starting: MUMBAI
Enter value for ending: DELHI
Enter value for no_flights: 8
Enter value for no_stops: 0
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
new 1: insert into FLIGHTS values('IC301','MUMBAI','DELHI',8,0)

1 row created.

Enter value for fl_no: IC799


Enter value for starting: BANGLORE
Enter value for ending: DELHI

146

Computer science practical

-2016

Enter value for no_flights: 2


Enter value for no_stops: 1
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('IC799','BANGLORE','DELHI',2,1)

1 row created.

Enter value for fl_no: MC101


Enter value for starting: INDORE
Enter value for ending: MUMBAI
Enter value for no_flights: 3
Enter value for no_stops: 0
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('MC101','INDORE','MUMBAI',3,0)

1 row created.

Enter value for fl_no: IC302


Enter value for starting: DELHI
Enter value for ending: MUMBAI
Enter value for no_flights: 8
Enter value for no_stops: 0
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)

147

Computer science practical

-2016

insert into FLIGHTS values('IC302','DELHI','MUMBAI',8,0)

1 row created.

Enter value for fl_no: AM812


Enter value for starting: KANPUR
Enter value for ending: BANGLORE
Enter value for no_flights: 3
Enter value for no_stops: 1
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('AM812','KANPUR','BANGLORE',3,1)
1 row created.

Enter value for fl_no: IC899


Enter value for starting: MUMBAI
Enter value for ending: KOCHI
Enter value for no_flights: 1
Enter value for no_stops: 4
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('IC899','MUMBAI','KOCHI',1,4)
1 row created.

Enter value for fl_no: AM501


Enter value for starting: DELHI

148

Computer science practical

-2016

Enter value for ending: TRIVANDRUM


Enter value for no_flights: 1
Enter value for no_stops: 5
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('AM501','DELHI','TRIVANDRUM',1,5)

1 row created.

Enter value for fl_no: MU499


Enter value for starting: MUMBAI
Enter value for ending: MADRAS
Enter value for no_flights: 3
Enter value for no_stops: 3
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('MU499','MUMBAI','MADRAS',3,3)
1 row created.

Enter value for fl_no: IC701


Enter value for starting: DELHI
Enter value for ending: AHMEDABAD
Enter value for no_flights: 4
Enter value for no_stops: 0
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)

149

Computer science practical

-2016

insert into FLIGHTS values('IC701','DELHI','AHMEDABAD',4,0)

1 row created.

insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS


2 /
Enter value for fl_no: IC301
Enter value for starting: MUMBAI
Enter value for ending: DELHI
Enter value for no_flights: 8
Enter value for no_stops: 0
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('IC301','MUMBAI','DELHI',8,0)

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.

Enter value for fl_no: MC101

150

Computer science practical

-2016

Enter value for starting: INDORE


Enter value for ending: MUMBAI
Enter value for no_flights: 3
Enter value for no_stops: 0
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('MC101','INDORE','MUMBAI',3,0)
1 row created.

Enter value for fl_no: IC302


Enter value for starting: DELHI
Enter value for ending: MUMBAI
Enter value for no_flights: 8
Enter value for no_stops: 0
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('IC302','DELHI','MUMBAI',8,0)
1 row created.

Enter value for fl_no: AM812


Enter value for starting: KANPUR
Enter value for ending: BANGLORE
Enter value for no_flights: 3
Enter value for no_stops: 1
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('AM812','KANPUR','BANGLORE',3,1)
1 row created.

151

Computer science practical

-2016

Enter value for fl_no: IC899


Enter value for starting: MUMBAI
Enter value for ending: KOCHI
Enter value for no_flights: 1
Enter value for no_stops: 4
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('IC899','MUMBAI','KOCHI',1,4)
1 row created.

Enter value for fl_no: AM501


Enter value for starting: DELHI
Enter value for ending: TRIVANDRUM
Enter value for no_flights: 1
Enter value for no_stops: 5
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('AM501','DELHI','TRIVANDRUM',1,5)
1 row created.

Enter value for fl_no: MU499


Enter value for starting: MUMBAI
Enter value for ending: MADRAS
Enter value for no_flights: 3
Enter value for no_stops: 3
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI

152

Computer science practical

-2016

insert into FLIGHTS values('MU499','MUMBAI','MADRAS',3,3)


1 row created.

Enter value for fl_no: IC701


Enter value for starting: DELHI
Enter value for ending: AHMEDABAD
Enter value for no_flights: 4
Enter value for no_stops: 0
insert into FLIGHTS values('&FL_NO','&STARTING','&ENDING',&NO_FLI
insert into FLIGHTS values('IC701','DELHI','AHMEDABAD',4,0)
SQL> Enter value for starting: MUMBAI
Enter value for ending: DELHI
Enter value for no_flights: 8
Enter value for no_stops: 0
insert into FLIGHTS
values('&FL_NO','&STARTING','&ENDING',&NO_FLIGHTS,&NO_STOPS)
insert into FLIGHTS values('IC301','MUMBAI','DELHI',8,0)
2 /
Enter value for fl_no: IC701
Enter value for airlines: Indian Airlines
Enter value for fare: 6500
Enter value for tax: 10
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('IC701','Indian Airlines',6500,10)
1 row created.

153

Computer science practical

-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

Computer science practical

-2016

1 row created.

Enter value for fl_no: IC302


Enter value for airlines: Indian Airlines
Enter value for fare: 4300
Enter value for tax: 10
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('IC302','Indian Airlines',4300,10)
1 row created.
SQL> /
Enter value for fl_no: IC799
Enter value for airlines: Indian Airlines
Enter value for fare: 10500
Enter value for tax: 10
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('IC799','Indian Airlines',10500,10)
1 row created.
SQL> /
Enter value for fl_no: MC701
Enter value for airlines: Deccan Airlines
Enter value for fare: 3500
Enter value for tax: 4
insert into FARES values('&FL_NO','&AIRLINES',&FARE,&TAX)
insert into FARES values('MC701','Deccan Airlines',3500,4)

155

Computer science practical

-2016

1 row created.
SQL> select*from FLIGHTS
2 /

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------IC301

MUMBAI

DELHI

IC799

BANGLORE

DELHI

MC101

INDORE

MUMBAI

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------IC302


MUMBAI

DELHI
8

156

Computer science practical

AM812

KANPUR

BANGLORE

IC899

-2016

MUMBAI

KOCHI

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------AM501

DELHI

TRIVANDRUM

MU499

MUMBAI

MADRAS

IC701
AHMEDABAD

DELHI
4

1) select FL_NO , NO_FLIGHTS from FLIGHTS where STARTING ='KANPUR'and


ENDING='BANGLORE'
2 /

FL_NO

NO_FLIGHTS

157

Computer science practical

-2016

------------------------------ ---------AM812

2)
1* select FL_NO , NO_FLIGHTS from FLIGHTS where STARTING ='KANPUR'and
ENDING='BANGLORE'
2 /

FL_NO

NO_FLIGHTS

------------------------------ ---------AM812

SQL> select*from FLIGHTS


2 order by FL_NO;

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------AM501

DELHI

TRIVANDRUM

AM812
BANGLORE

KANPUR
3

158

Computer science practical

IC301

-2016

MUMBAI

DELHI

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------IC302

DELHI

MUMBAI

IC701

DELHI

AHMEDABAD

IC799

BANGLORE

DELHI

FL_NO

STARTING

------------------------------ -----------------------------ENDING

NO_FLIGHTS NO_STOPS

------------------------------ ---------- ---------IC899


KOCHI

MC101

MUMBAI
1

INDORE

159

Computer science practical

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

4) select min(FARE) from FARES


2 where AIRlINES='INDIAN AIRLINES';

MIN(FARE)
---------4300

5)
1 select FLIGHTS.FL_NO, NO_FLIGHTS, AIRLINES from FLIGHTS, FARES where

160

Computer science practical

-2016

2* STARTING='DELHI' AND FLIGHTS.FL_NO=FARES.FL_NO


3 /

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

Computer science practical

-2016

2
1
2
3
4
5
6
7

create table workers


(
w_id number(3),
firstname char(20),
lastname char(20),
address char(30),
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)
SQL> insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
2 /
Enter value for w_id: 102
Enter value for firstname: sam
Enter value for lastname: tones
Enter value for address: 33 elm st
Enter value for city: paris
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(102,'sam','tones','33 elm st','paris')
1 row created.
1 row created.
SQL> /
Enter value for w_id: 105
Enter value for firstname: sarah

162

Computer science practical

-2016

Enter value for lastname: ackerman


Enter value for address: 440 u.s. 110
Enter value for city: new delhi
old 1: insert into workers values(&w_id,'&firstname','&lastname','&address','&city')
new 1: insert into workers values(105,'sarah','ackerman','440 u.s. 110','new delhi')
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')
1 row created.

163

Computer science practical

-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

Computer science practical

-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

create table desig


(
w_id number(3),
salary number(5),
benifits number(5),
designation char(20)
7* )
SQL> /
Table created.
SQL>descdesig
Name
Null? Type
----------------------------------------- -------- ---------------------------W_ID
NUMBER(3)
SALARY
NUMBER(5)
BENIFITS
NUMBER(5)
DESIGNATION
CHAR(20)
SQL> insert into desigvalues(&w_id,&salary,&benifits,'&designation')
2 /
Enter value for w_id: 102

165

Computer science practical

-2016

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.

SQL> /
Enter value for w_id: 255

166

Computer science practical

-2016

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> /
Enter value for w_id: 451
Enter value for salary: 28000

167

Computer science practical

-2016

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(451,28000,75000,'salesman')
1 row created.
SQL> select*from desig
2 /
W_ID SALARY BENIFITS DESIGNATION
---------- ---------- ---------- -------------------102
75000
15000 manager
105
85000
25000 director
144
70000
15000 manager
210
75000
12500 manager
255
50000
12000 clerk
300
45000
10000 clerk
335
40000
10000 clerk
400
32000
75000 salesman
451
28000
75000 salesman
9 rows selected.
Ques 1
SQL> select w_id,firstname,address,city from workers
2 where
3 city='new york'
4 /
W_ID FIRSTNAME
ADDRESSCITY
---------------------------------------403 ronny
121 harrisonstnew York
Ques2)
SQL> select*from workers
2 order by lastname
3 /
W_ID FIRSTNAME

LASTNAMEADDRESS

CITY

168

Computer science practical

-2016

------------------------------ -------------------- ----------------------------105 sarahackerman440 u.s. 110


new delhi
255 mary
jones
842 vine avelosantiville
403 ronny
lee
121 harrisonstnew york
300robertSamuel
9 fifth cross
washington
144 manila
sengupta24 friends street
new delhi
210 george
smith
83 first street
howard
451 pat
Thompson
11 red road
paris
102 sam
tones
33 elm stparis
335 henry
Williams
12 moore street
boston
9 rows selected.
Ques6)
1* select count(distinct designation) from desig
SQL> /
COUNT(DISTINCTDESIGNATION)
-------------------------4
Ques7)

SQL> select designation,sum(salary)


2 fromdesig
3 group by designation having count(*)<3
4 /
DESIGNATION
SUM(SALARY)
-------------------- ----------director
85000
salesman
60000
Ques8)

1 select sum(benifits)
2 fromdesig
3* where designation='salesman'
SQL> /
SUM(BENIFITS)

169

Computer science practical

-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)

1 selectfirstname,lastname,(salary+benifits) "total salary"


2 fromworkers,desig
3 whereworkers.w_id=desig.w_id
4* and designation ='clerk'
SQL> /
FIRSTNAME
LASTNAME
total salary
-------------------- -------------------- -----------mary jones
62000
robertsamuel
55000
henrywilliams
50000
Ques4)
SQL> select min(salary) from desig
2 group by designation
3 having designation in('manager','clerk')
4 /
MIN(SALARY)
----------40000

170

Computer science practical

-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

Computer science practical

-2016

old 1: insert into GARMENT


values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10001,'FORMAL',1250,'F01','12-JAN-08')
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> /
Enter value for gcode: 10019
Enter value for description: EVENING GOWN
Enter value for price: 850

172

Computer science practical

-2016

Enter value for fcode: F03


Enter value for readydate: 06-JUN-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10019,'EVENING GOWN',850,'F03','06-JUN-08')
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-OCT08')
1 row created.
Enter value for gcode: 10007
Enter value for description: FORMAL
Enter value for price: 1350
Enter value for fcode: F01
Enter value for readydate: 09-MAR-08
old 1: insert into GARMENT
values(&GCODE,'&Description',&Price,'&FCODE','&READYDATE')
new 1: insert into GARMENT values(10007,'FORMAL',1350,'F01','09-MAR-08')
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.
SQL> /
Enter value for gcode: 10089
Enter value for description: SLACKS

173

Computer science practical

-2016

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.
SQL> select* from GARMENT
2 ;
GCODE DESCRIPTION
PRICE FCODE
READYDATE
---------- ------------------------------ ---------- ---------- --------10023 PENCIL SKIRT
1150 F03
19-DEC-08
10001 FORMAL
1250 F01
12-JAN-08
10012 INFORMAL
1550 F02
06-JUN-08
10024 BABY TOP
750 F03
07-APR-07
10090 TULIP SKIRT
850 F02
31-MAR-07
10019 EVENING GOWN
850 F03
06-JUN-08
10009 INFORMAL PANT
1500 F02
20-OCT-08
10007 FORMAL
1350 F01
09-MAR-08
10020 FROCK
850 F04
09-SEP-07
10089 SLACKS
750 F03
31-OCT-08
10 rows selected.
SQL> create table FABRIC
2 (
3 FCODE char(10),
4 TYPE char(30)
5 );
Table created.
SQL> insert into FABRIC values('&FCODE','&TYPE')
2 ;
Enter value for fcode: F04
Enter value for type: POLYSTER
old 1: insert into FABRIC values('&FCODE','&TYPE')
new 1: insert into FABRIC values('F04','POLYSTER')
1 row created.
SQL> /
Enter value for fcode: F02
Enter value for type: COTTON

174

Computer science practical

-2016

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.
SQL> select* from FABRIC;
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
10012 INFORMAL
10009 INFORMAL PANT
10007 FORMAL

175

Computer science practical

-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

Computer science practical

-2016

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
-----------------------------------create table dress
(
dcode number(5),
descripton char(20),
price number(5),
mcode char(4),
launchdate date
);
/
Table Created.
------------------------------------

177

Computer science practical

-2016

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
Enter value for price: 850
Enter value for mcode: m002
Enter value for launchdate: 31-mar-07

178

Computer science practical

-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

Computer science practical

-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

Computer science practical

-2016

Questions and Answers


Q1---------------------------------------------------select dcode,descripton from dress
order by
dcode asc;
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'
/
DCODE DESCRIPTON
PRICE MCOD LAUNCHDAT
---------- -------------------- ---------- ---- --------10001 Formal Shirt
1250 m001 12-JAN-08
10012 informal shirt
1450 m002 06-JUN-08
10019 Evening Gown
850 m003 06-JUN-08
10007 Formal Skirt
1450 m001 09-MAR-08
-------------------------------------------------------------Q3
select avg(price) from dress,material where material.mcode='m003'
/
AVG(PRICE)
---------1075
------------------------------------------------Q4
select max(price),min(price) from dress;

181

Computer science practical

-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

Computer science practical

-2016

183

You might also like