You are on page 1of 67

COMPUTER SCIENCE PRACTICAL FILE

Q10. Write a menu driven program to search an integer in a list of


integers using any of the
following techniques:
1. Linear Search
2. Binary Search
Use bubble sort to sort the list, if required

CODING
#include<iostream.h>
#include<conio.h>
void Bubblesort(int A[],int s)
{
int temp,j;
for(int i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
int BSearch(int A[],int s,int item)
{
int beg,mid,last;
beg=0;
last=s-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(A[mid]==item)
return mid;
else if(A[mid]>item)
last=mid-1;
else if(A[mid]<item)
beg=mid+1;

ARJUN TYAGI XII-D 44


COMPUTER SCIENCE PRACTICAL FILE

}
return -1;
}
void main()
{
clrscr();
int A[10],s,item,index;
char ch='y';
cout<<"Enter the size of array";
cin>>s;
cout<<"Enter array";
for(int i=0;i<s;i++)
cin>>A[i];
Bubblesort(A,s);
do
{
cout<<"Enter element to be searched";
cin>>item;
index=BSearch(A,s,item);
if(index==-1)
cout<<"Element not found";
else
cout<<"Element found at index "<<index<<" and position
"<<index+1<<endl;
cout<<"Want to search more elements?";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

ARJUN TYAGI XII-D 45


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

Q11. Write a program to input integer data in two array. Sort one of the
arrays in ascending order and the other in descending order. Then merge
them into a third array so that the data in the third array is in ascending
order. The program should then display the data from all the three arrays.

CODING
#include<iostream.h>
#include<conio.h>
void Bubblesort1(int S[],int s)
{
int temp,j;
for(int i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
if(S[j]>S[j+1])
{
temp=S[j];
S[j]=S[j+1];
S[j+1]=temp;
}
}

ARJUN TYAGI XII-D 46


COMPUTER SCIENCE PRACTICAL FILE

cout<<"Array A\n";
for(i=0;i<s;i++)
cout<<S[i]<<' ';
}
void Bubblesort2(int S[],int s)
{
int temp,j;
for(int i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
if(S[j]<S[j+1])
{
temp=S[j];
S[j]=S[j+1];
S[j+1]=temp;
}
}
cout<<"\nArray B\n";
for(i=0;i<s;i++)
cout<<S[i]<<' ';
}
void mergesort(int A[],int m,int B[],int n,int C[])
{
int a,b,c;
for(a=0,b=n-1;c=0;a<m&&b>=0)
{
if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
if(a<m)
{
while(a<m)
C[c++]=A[a++];
}
else
{

ARJUN TYAGI XII-D 47


COMPUTER SCIENCE PRACTICAL FILE

while(b>=0)
C[c++]=B[b--];
}
}
void main()
{
clrscr();
int A[5],B[5],C[10],m=0,n=0,mn=0;
cout<<"Enter the size of array A and B";
cin>>m>>n;
cout<<"Enter elements of array A";
for(int i=0;i<m;i++)
cin>>A[i];
cout<<"Enter elements of array B";
for(i=0;i<n;i++)
cin>>B[i];
mn=m+n;
Bubblesort1(A,m);
Bubblesort2(B,n);
mergesort(A,m,B,n,C);
cout<<"\nArray C\n";
for(i=0;i<mn;i++)
cout<<C[i]<<' ';
getch();
}

OUTPUT

ARJUN TYAGI XII-D 48


COMPUTER SCIENCE PRACTICAL FILE

Q12. Write a menu driven program which allows the user to perform the
following operations on a one dimensional array:
Insertion, deletion, sorting(selection, insertion), display

CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<limits.h>
void selsort(int A[],int s)
{
int temp,small,pos;
for(int i=1;i<s;i++)
{
small=A[i];
pos=i;
for(int j=i+1;j<=s;j++)
{
if(A[j]<small)
{
small=A[j];
pos=j;

ARJUN TYAGI XII-D 49


COMPUTER SCIENCE PRACTICAL FILE

}
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
cout<<"\nArray after pass "<<i+1<<"\n";
for(j=1;j<=s;j++)
cout<<A[j]<<' ';
}
}
void Inssort(int A[],int s)
{
int temp,j;
A[0]=INT_MIN;
for(int i=1;i<=s;i++)
{
temp=A[i];
j=i-1;
while(temp<A[j])
{
A[j+1]=A[j];
j--;
}
A[j+1]=temp;
cout<<"\nArray after pass "<<i<<"\n";
for(int k=1;k<=s;k++)
cout<<A[k]<<' ';
}
}
int find(int A[],int s,int item)
{
int pos;
if(item<A[0])
pos=0;
else
{
for(int i=1;i<=s;i++)
{

ARJUN TYAGI XII-D 50


COMPUTER SCIENCE PRACTICAL FILE

if(A[i]<=item&&item<A[i+1])
{
pos=i;
break;
}
if(i==s-1)
pos=s;
}
}
return pos;
}
void Insert(int A[],int s)
{
int item,index;
cout<<"\nEnter element to be inserted";
cin>>item;
if(s==10)
{
cout<<"Overflow";
getch();
exit(-1);
}
index=find(A,s,item);
for(int i=s+1;i>index;i--)
A[i]=A[i-1];
A[index]=item;
s=s+1;
}
void deletion(int A[],int s)
{
int item,index=-1;
cout<<"\nEnter element to be deleted";
cin>>item;
if(s==0)
{
cout<<"underflow";
getch();
exit(-1);

ARJUN TYAGI XII-D 51


COMPUTER SCIENCE PRACTICAL FILE

}
for(int i=1;i<=s;i++)
{
if(A[i]==item)
index=i;
}
if(index!=-1)
A[index]=0;
else
cout<<"Element not found";
cout<<"\nArray after deletion where 0 signifies deleted element\n";
for(i=1;i<=s;i++)
cout<<A[i]<<' ';
cout<<"\nShifting emptied spaced to the right\n";
for(i=index;i<=s;i++)
A[i]=A[i+1];
s=s-1;

}
void display(int A[],int s)
{
for(int i=1;i<=s;i++)
cout<<A[i]<<' ';
}
void main()
{
clrscr();
int A[10],s,item,index,ch;
char ans;
cout<<"Enter the size of array";
cin>>s;
cout<<"Enter elements";
for(int i=1;i<=s;i++)
cin>>A[i];
do
{
cout<<"\n1. Insertion"<<endl;
cout<<"2. Deletion"<<endl;

ARJUN TYAGI XII-D 52


COMPUTER SCIENCE PRACTICAL FILE

cout<<"3. Sorting"<<endl;
cout<<"\t1. Selection sort"<<endl;
cout<<"\t2. Insertion sort"<<endl;
cout<<"4. Display"<<endl;
cout<<"Enter choice(1,2,31,32,4)";
cin>>ch;
switch(ch)
{
case 1: selsort(A,s);
Insert(A,s);
break;
case 2: selsort(A,s);
deletion(A,s);
break;
case 31: selsort(A,s);
break;
case 32: Inssort(A,s);
break;
case 4: display(A,s);
}
cout<<"Do you want to continue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}

OUTPUT

ARJUN TYAGI XII-D 53


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 54


COMPUTER SCIENCE PRACTICAL FILE

Q13. Imagine a publishing company that markets both books and audio
cassette versions of its works. Create a class publication that stores the
title (a string) and price (type float) of a publication. From this class derive
two classes book which adds a page count (type int) and a tape which adds
a playing time and minutes (type float). Each of these three classes should
have a getdata function to get its data from the user at the keyboard and a
putdata function to display its data.
Write a main program to test the book and tape classes by creating
instances of them asking the user to fill in the data with getdata and then
displaying the data with putdata.

CODING
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class publication
{
char title[10];
float price;
public:
void getdata()
{
cout<<"Enter title\n";
gets(title);
cout<<"Enter price";
cin>>price;
}
void putdata()
{
cout<<"Title: ";
puts(title);
cout<<"Price: "<<price<<endl;
}
};
class book:public publication

ARJUN TYAGI XII-D 55


COMPUTER SCIENCE PRACTICAL FILE

{
int page;
public:
void getdata()
{
publication::getdata();
cout<<"Enter no of pages";
cin>>page;
}
void putdata()
{
publication::putdata();
cout<<"No of pages: "<<page<<endl;
cout<<endl;
}
};
class tape:public publication
{
float time;
public:
void getdata()
{
publication::getdata();
cout<<"Enter the time";
cin>>time;
}
void putdata()
{
publication::putdata();
cout<<"Duration: "<<time<<endl
;
}
};
void main()
{
clrscr();
book b;
tape t;

ARJUN TYAGI XII-D 56


COMPUTER SCIENCE PRACTICAL FILE

b.getdata();
t.getdata();
b.putdata();
t.putdata();
getch();
}

OUTPUT

Q14 Write a program to input a text file namer ead the contents of the file
and create a new file named COPY.TXT which shall contain only those
words from the file (which has been specified by the user) that don’t end
with a vowel.

CODING
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
{

ARJUN TYAGI XII-D 57


COMPUTER SCIENCE PRACTICAL FILE

clrscr();
ifstream fin;
ofstream fout;
char name[20],word[15];
int l=0;
cout<<"Enter file name ";
cin>>name;
fin.open(name);
fout.open("COPY.txt");
if(!fin)
{
cout<<"Error";
getch();
exit(-1);
}
while(!fin.eof())
{
char ch='y';
fin.getline(word,40,' ');
l=strlen(word);
switch(word[l-1])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
ch='n';
}
if(ch=='y')
fout<<word<<' ';
}
fout.close();

ARJUN TYAGI XII-D 58


COMPUTER SCIENCE PRACTICAL FILE

fin.close();
getch();

OUTPUT

ARJUN TYAGI XII-D 59


COMPUTER SCIENCE PRACTICAL FILE

Q15. Write a program to perform SEARCH and REPLACE operation on


a text file. For this input the name of a text file from the user. Then input
two characters and search for the first character in the file and replace it
with the second character. Do it for all the occurrences of the first
character in the text file.

CODING
#include<fstream.h>
#include<conio.h>
#include<process.h>
void search()
{
fstream fio;
fio.open("TEXT",ios::in|ios::out);
char a,b,ch;
if(!fio)
{
cout<<"Error";
getch();

ARJUN TYAGI XII-D 60


COMPUTER SCIENCE PRACTICAL FILE

exit(-1);
}
cout<<"Enter character to be replaced and character to be replaced with";
cin>>a>>b;
while(!fio.eof())
{
fio>>ch;
if(ch==a)
{
fio.seekg(-1,ios::cur);
fio<<b;
}
}
fio.close();
}
void main()
{
clrscr();
search();
getch();
}

OUTPUT

ARJUN TYAGI XII-D 61


COMPUTER SCIENCE PRACTICAL FILE

Q16. Write a program to input the name of a text file from the user and
display
a) The number of blanks present in the file
b) The number of lines present in the file
c) The number of capital alphabets in the file
d) The number of small alphabets present in the file
e) The number of lines starting with a capital alphabet
f) The number of words present in the file
g) The number of digits present in the file
h) The number of words ending with a vowel

CODING
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
void main()

ARJUN TYAGI XII-D 62


COMPUTER SCIENCE PRACTICAL FILE

{
clrscr();
ifstream fin;
char name[50],str[50],st[50],ch;
int blank=0,line=0,upper=0,lower=0,upline=0,word=0,digit=0,vword=0;
int l;
cout<<"Enter file name ";
cin>>name;
fin.open(name);
if(!fin)
{
cout<<"Error";
getch();
exit(-1);
}
while(!fin.eof())
{
fin.get(ch);
if(fin.eof())
break;
if(isspace(ch))
blank++;
else if(isupper(ch))
upper++;
else if(islower(ch))
lower++;
else if(isdigit(ch))
digit++;
}
fin.close();
fin.open(name);
while(!fin.eof())
{
fin.getline(str,50,'.');
line++;
if(str[0]>='A'&&str[0]<='Z')
upline++;
}

ARJUN TYAGI XII-D 63


COMPUTER SCIENCE PRACTICAL FILE

fin.close();
fin.open(name);
while(!fin.eof())
{
fin.getline(st,50,' ');
word++;
l=strlen(st);
switch(st[l-1])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vword++;
}
}
fin.close();
cout<<"No. of blanks: "<<blank<<"\nNo. of lines: "<<line<<"\nNo. of
capital alphabets: "<<upper<<"\nNo. of small alphabets:
"<<lower<<"\nNo. of lines starting with capital alphabets:
"<<upline<<"\nNo. of words: "<<word<<"\nNo. of digits:
"<<digit<<"\nNo. of words ending with a vowel: "<<vword;
getch();
}

OUTPUT

ARJUN TYAGI XII-D 64


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 65


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 66


COMPUTER SCIENCE PRACTICAL FILE

Q17. Given class MOVIE with following declarations:


class MOVIE
{
int movieno;
char moviename[25];
float price;
public:
int compare(charnm[])
{
if(strcmp(moviename,nm)==0)
return1;
else
return0;
}
voidinput();
voiddisplay();
};
Write a menu driven interactive program to:
(i)Create a binary file “MOVIE.DAT”.
(ii)Append records into a binary file called “MOVIE.DAT”
(iii)Search for a particular movie name & display its price.
(iv)Increase the price of all movies in the same file whose price>200 by
10%.
(v)Delete a particular record whose name is specified by the user.

CODING
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h
#include<process.h>
class MOVIE
{
int movieno;
char moviename[25];
float price;
public:
int compare(charnm[])

ARJUN TYAGI XII-D 67


COMPUTER SCIENCE PRACTICAL FILE

{
if(strcmp(moviename,nm)==0)
return1;
else
return0;
}
voidinput()
{
cout<<"moviename:";
cin>>moviename;
cout<<"movieno:";
cin>>movieno;
cout<<"price:";
cin>>price;
}
voiddisplay()
{
cout<<"Moviename:"<<moviename<<endl;
cout<<"Movieno:"<<movieno<<endl;
cout<<"Price:"<<price<<endl;
}
char*ret_name()
{
returnmoviename;
}
intret_price()
{
returnprice;
}
voidchangeprice()
{
price+=(0.10*price);
}
};
voidmain()
{
clrscr();
MOVIE m;

ARJUN TYAGI XII-D 68


COMPUTER SCIENCE PRACTICAL FILE

char ch,nm[30],k;
int x,z,n;
fstream fio;
do{
clrscr();
cout<<"**MENU**"<<endl;
cout<<"1.Createfile"<<endl;
cout<<"2.Append Records"<<endl;
cout<<"3.Search movie"<<endl;
cout<<"4.Edit movies"<<endl;
cout<<"5.Deletearticularrecord"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enterchoice";
cin>>x;
switch(x)
{
case 1:
fio.open("MOVIE.DAT",ios::binary);
fio.close();
cout<<"FILECREATED!";
break;
case2:
do
{
fio.open("MOVIE.DAT",ios::app);
m.input();
fio.write((char*)&m,sizeof(m));
cout<<"Doyouwanttoaddmore";
cin>>ch;
}while(ch=='y');
break;
case3:
cout<<"Enternameofmovietobesearched";
gets(nm);
charflag='n';
while(fio)
{
fio.read((char*)&m,sizeof(m));

ARJUN TYAGI XII-D 69


COMPUTER SCIENCE PRACTICAL FILE

if(strcmpi(m.ret_name(),nm)==0)
{
flag='y';
m.display();
}
}
if(flag=='n')
cout<<"Recordnotfound";
fio.close();
break;
case4:
fio.open("MOVIE.DAT",ios::binary);
while(fio)
{
fio.read((char*)&m,sizeof(m));
if(m.ret_price()>200)
{
m.changeprice();
}
}
cout<<"pricechanged"<<endl;
fio.close();
break;
case5:
ifstream fin("MOVIE.DAT",ios::binary);
ofstream fout("temp.dat",ios::binary);
int r;
char confirm='n';
cout<<"Enter movie name of record to be deleted";
gets(nm);
while(fin)
{
fin.read((char*)&m,sizeof(m));
if(fin.eof())
break;
if(strcmpi(m.ret_name(),nm)==0)
{
m.display();

ARJUN TYAGI XII-D 70


COMPUTER SCIENCE PRACTICAL FILE

flag='f';
cout<<"Do you want to delete this";
cin>>confirm;
if(confirm=='n')
fout.write((char*)&m,sizeof(m));
}
else
fout.write((char*)&m,sizeof(m));
}
if(flag=='n')
cout<<"not found";
fin.close();
fout.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
break;
case6:
exit(0);
break;
}
cout<<"do you want to continue";
cin>>k;
}while(k=='y');
getch();
}
OUTPUT

ARJUN TYAGI XII-D 71


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 72


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 73


COMPUTER SCIENCE PRACTICAL FILE

Q18. Consider the following class definition:


classLabs
{
intLab_Code;
charDepartment[30];
intNumber;
public:
voidget_data()
{
cout<<“Lab.Code:”;cin>>Lab_Code;
cout<<“Department:”;gets(Department);;
cout<<“TotalLabs:”;cin>>Number;
}
voidDisp_data()
{cout<<“Lab.Code:”;<<Lab_Code;
cout<<“Department:”;<<Department;;
cout<<“TotalLabs:”;<<Number;
}
intGet_code(){returnLab_Code;}
};

Write a menu driven program using separate functions:


(i)To create a binary file called “Labs.dat” and write objects into it.
(ii)To read the file and display it’s contents.
(iii)To look for a record for a Lab_Code given by the user and replace the
record with a new record given by the user.

CODING
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
classLabs
{
intLab_Code;
charDepartment[30];
intNumber;
public:
voidget_data()

ARJUN TYAGI XII-D 74


COMPUTER SCIENCE PRACTICAL FILE

{
cout<<"LabCode:";
cin>>Lab_Code;
cout<<"Department:";
cin>>Department;
cout<<"TotalLabs:";
cin>>Number;
}
voidDisp_data()
{
cout<<"LabCode"<<Lab_Code<<endl;
cout<<"Department:"<<Department<<endl;
cout<<"TotalLabs:"<<Number<<endl;
}
intGet_code()
{
returnLab_Code;
}
};
void createfile()
{
char ans;
Labsl;
fstream fio;
fio.open("labs.dat",ios::binary|ios::app);
do
{
l.get_data();
fio.write((char*)&l,sizeof(l));
cout<<"do you want to enter more records";
cin>>ans;
}while(ans=='y');
fio.close();
}
void display()
{
Labs l;
ifstream fio;
fio.open("labs.dat",ios::binary);

ARJUN TYAGI XII-D 75


COMPUTER SCIENCE PRACTICAL FILE

while(fio)
{
if(fio.eof())
break;
fio.read((char*)&l,sizeof(l));
l.Disp_data();
}
fio.close();
}
void modify()
{
intr,pos=0;
charflag='n';
fstream fio("labs.dat",ios::binary|ios::in|ios::out);
Labs l;
cout<<"\nEntertheLabCodeofrecordtobemodified";
cin>>r;
while(fio)
{
fio.read((char*)&l,sizeof(l));
if(fio.eof())
break;
pos++;
if(l.Get_code()==r)
{
flag='f';
l.get_data();
fio.seekg((pos-1)*sizeof(l),ios::beg);
fio.write((char*)&l,sizeof(l));
cout<<"RECORDMODIFIED!"<<endl;
break;
}
}
if(flag=='n')
cout<<"notfound";
fio.close();
}

ARJUN TYAGI XII-D 76


COMPUTER SCIENCE PRACTICAL FILE

void main()
{
intx,n;
char ans;
Labs l;
do{
clrscr();
cout<<"***MENU***"<<endl;
cout<<"1.CreatefileandInputrecords"<<endl;
cout<<"2.Displayrecords"<<endl;
cout<<"3.Replacerecords"<<endl;
cout<<"4.Exit"<<endl;
cin>>x;
switch(x)
{
case1:
createfile();
break;
case2:
display();
break;
case3:
modify();
break;
case4:
exit(0);
break;
}
cout<<"Doyouwanttocontinue";
cin>>ans;
}while(ans=='y');
getch();
}

ARJUN TYAGI XII-D 77


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

ARJUN TYAGI XII-D 78


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 79


COMPUTER SCIENCE PRACTICAL FILE

Q19. Write a C++ program to create two text files and the and create a
third textfile, which contains alternate lines from both the files. Your
program should have the following functions:
(i)Create_File() to create a file, with name of file passed as a parameter.
(ii)Alternate()to write alternate lines in the third file. The function should
take as parameters, the names of the three files,the first two names should
be names of files which will serve as input, and third name of file where it
will hold the output.
(iii)Read_file()to display contents of file whose name is passed as a
parameter.

CODING
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
void Alternate(char file1[10],char file2[10],char file3[10]);
void Read_File(char file3[10]);
void main()
{
char file1[10],file2[10],file3[10];
cout<<"enter the name of file 1";
cin>>file1;
cout<<"enter the name of file 2";
cin>>file2;
cout<<"enter the name of file to be created";
cin>>file3;
Alternate(file1,file2,file3);
Read_File(file3);
getch();
}
void Alternate(char file1[10],char file2[10],char file3[10])
{
ifstream fin1,fin2;
ofstream fout;
fin1.open(file1,ios::binary);
fin2.open(file2,ios::binary);
fout.open(file3,ios::binary|ios::app);
char str1[200];

ARJUN TYAGI XII-D 80


COMPUTER SCIENCE PRACTICAL FILE

char ch1,ch2;
while(!fin1.eof()&&!fin2.eof())
{
if(!fin1.eof())
{
fin1.getline(str1,200,'.');
fout<<str1;
}
if(!fin2.eof())
{
fin2.getline(str1,199,'.');
fout<<str1;
}
}
fin1.close();
fin2.close();
fout.close();
}
void Read_File(char file3[10])
{
char ch;
ifstream fin;
fin.open(file3,ios::binary);
while(!fin.eof())
{
if(fin.eof())
break;
fin>>ch;
cout<<ch;
}
}

ARJUN TYAGI XII-D 81


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

ARJUN TYAGI XII-D 82


COMPUTER SCIENCE PRACTICAL FILE

Q20. A chain of grocery store keeps the records of all its customers and
their dealings with the store. The records need to be kept in order and
updated as and when required. Consider the following class definition and
perform the following operations using separate functions:
Note: Write the required member function for the class
class store
{
intID_No;
char Name[30];
char Address1[40];
char Address2[30];
long Tel_No;
char Status;//‘M’forMemberand‘N’fornon-members
public:
long R_Tel_No()
{
returnTel_no;
}
char R_status()
{
returnStatus;
}
void Assign_Tel(longTel)
{
Tel_No=Tel;
}
char*R_Name()
{
returnName;
}
};
Write a menu driven program with separate function to perform the
following tasks:-
(i)Create a file called “CUSTOMER.DAT” and write objects on it.
(ii)View the records of all the members.
(iii)Change the telephone number of a customer in his record by taking
the new number of the customer as an input.
(iv)Display the contents of the file.

ARJUN TYAGI XII-D 83


COMPUTER SCIENCE PRACTICAL FILE

CODING
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class store
{
intID_No;
char Name[30];
char Address1[40];
char Address2[30];
long Tel_No;
char Status;
public:
void readdata()
{
cout<<"IDNO.:";cin>>ID_No;
cout<<"Entername:";gets(Name);
cout<"Enter Address Line1:";
gets(Address1);
gets(Address2);
cout<<"Enter TelephoneNo.:";cin>>Tel_No;
cout<<"Enter Status[M/N]";cin>>Status;
}
void dispdata()
{
cout<<"IDNO.:"<<ID_No<<endl;
cout<<"Name:"<<Name<<endl;
cout<<"AddressLine1:"<<Address1<<endl;
cout<<"AddressLine2:"<<Address2<<endl;
cout<<"TelephoneNO.:"<<Tel_No<<endl;
cout<<"Status:"<<Status<<endl;;
}
longR_Tel_No()
{
returnTel_No;
}
charR_status()
{

ARJUN TYAGI XII-D 84


COMPUTER SCIENCE PRACTICAL FILE

returnStatus;
}
voidAssign_Tel(longTel)
{
Tel_No=Tel;
}
char*R_Name()
{
returnName;
}
intR_ID()
{
returnID_No;
}
};
void CreateFile()
{
ofstream fout("customer.dat",ios::binary|ios::app);
charans;
do
{
store s;
s.readdata();
fout.write((char*)&s,sizeof(s));
cout<<"doyouwanttoaddmorerecords";
cin>>ans;
}while(ans=='y');
fout.close();
}
void DisplayMember()
{
ifstream fin("customer.dat",ios::binary);
store s;
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(fin.eof())
break;

ARJUN TYAGI XII-D 85


COMPUTER SCIENCE PRACTICAL FILE

if(s.R_status()=='M'||s.R_status()=='m')
s.dispdata();
}
fin.close();
}
void modify()
{
store s;
longTel;
int sId;
char ans;
cout<<"EnterIDoftherecordforchangingno.";cin>>sId;
ifstream fin("customer.dat",ios::binary);
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(s.R_ID()==sId);
{
s.dispdata();
cout<<"Doyouwanttochangethisrecord"<<endl;
cin>>ans;
if(ans=='y')
{
cout<<"EnternewTelephoneNO.";
cin>>Tel;
s.Assign_Tel(Tel);
cout<<"Recordchanged";
}
}
}
}
void Display()
{
ifstream fin("customer.dat",ios::binary);
store s;
while(fin)
{
fin.read((char*)&s,sizeof(s));

ARJUN TYAGI XII-D 86


COMPUTER SCIENCE PRACTICAL FILE

if(fin.eof())
break;
s.dispdata();
}
fin.close();
}
void main()
{
int ans;
char x;
do
{
clrscr();
cout<<"1.ToCreateFileandinputrecords"<<endl;
cout<<"2.Toviewrecordsofallthemembers"<<endl;
cout<<"3.ToChangetelephoneNo.ofCustomer"<<endl;
cout<<"4.ToDiplayAllthecontents";
cout<<"Enter choice"<<endl;
cin>>ans;
switch(ans)
{
case1:
CreateFile();
break;
case2:
DisplayMember();
break;
case3:
modify();
break;
case4:
Display();
break;
}
cout<<"doyouwanttocontinue";
cin>>x;
}while(x=='y');
}

ARJUN TYAGI XII-D 87


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

ARJUN TYAGI XII-D 88


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 89


COMPUTER SCIENCE PRACTICAL FILE

Q21. Write a complete menu driven program to implement a STACK


using an array of float values. The program should include POP, PUSH
and DISPLAY.

CODING
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#defineMAX5
int top=-1;
float stack[MAX];
void push();
void pop();
void display();
void main()
{
clrscr();
int ch;
while(1)
{
clrscr();
cout<<"\n***StackMenu***";
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice(1-4):";
cin>>ch;
switch(ch)
{
case1:
push();
break;
case2:
pop();
break;

ARJUN TYAGI XII-D 90


COMPUTER SCIENCE PRACTICAL FILE

case3:
display();
break;
case4:
exit(0);
default:
cout<<"\nWrongChoice!!";
}
}
getch();
}
voidpush()
{
floatval;
if(top==MAX-1)
{
cout<<"\nStackisfull!!";
}
else
{
cout<<"\nEnterelementtopush:";
cin>>val;
top=top+1;
stack[top]=val;
}
}
voidpop()
{
if(top==-1)
{
cout<<"\nStackisempty!!";
}
else
{
cout<<"\nDeletedelementis:"<<stack[top];
top=top-1;
}
}

ARJUN TYAGI XII-D 91


COMPUTER SCIENCE PRACTICAL FILE

voiddisplay()
{
int i;
if(top==-1)
{
cout<<"\nStackisempty!!";
}
else
{
cout<<"\nStackis...\n";
for(i=top;i>=0;--i)
cout<<stack[i]<<endl;
}
}

OUTPUT

ARJUN TYAGI XII-D 92


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 93


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 94


COMPUTER SCIENCE PRACTICAL FILE

Q22. Write a complete menu driven program to implement a QUEUE


using an array of float values. The program should include INSERT,
DELETE and DISPLAY.

CODING
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#definemax5
float queue[max];
int front=-1,rear=-1;
void insert();
void delq();
void display();
void main()
{
clrscr();
int choice;
char ans;
do
{
clrscr();
cout<<"***QUEUE OPERATIONS***"<<endl;
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case1:
insert();
break;
case2:
delq();

ARJUN TYAGI XII-D 95


COMPUTER SCIENCE PRACTICAL FILE

break;
case3:
display();
break;
case4:
exit(0);
break;
default:
cout<<"Invalid choice:"<<endl;
break;
}
cout<<"Do you want to continue"<<endl;
cin>>ans;
}while(ans=='y');
}
void insert()
{
float item;
if(rear==(max-1))
{
cout<<"QueueOverflow:";
}
else
{
cout<<"Entertheelementtobeinserted:";
cin>>item;
rear=rear+1;
queue[rear]=item;
if(front==-1)
front=0;
}
}
void delq()
{
float item;
if(front==-1)
{
cout<<"\nQueue Underflow:";

ARJUN TYAGI XII-D 96


COMPUTER SCIENCE PRACTICAL FILE

}
else
{
item=queue[front];
cout<<"The deleted element:"<<item;
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
}
}
void display()
{
int i;
if(front==-1)
{
cout<<"\nQueue is Empty:";
}
else
{
cout<<"\nThe queue elements are:"<<endl;
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<endl;
}
}
}

ARJUN TYAGI XII-D 97


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

ARJUN TYAGI XII-D 98


COMPUTER SCIENCE PRACTICAL FILE

Q23 The following data item to be stored for a student:


Stu_no, Stu_name, Percentage
Create a data structure QUEUE to store the data using LINKED
implementation. Your program will have the following member functions:
i) addQ()
ii) deleteQ()
iii) display()
These functions are called as per the user’s choice. Write a complete menu
driven object oriented program to implement the above.

CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct student
{
int Stu_no;
char Stu_name[10];
int percentage;
student*next;
};
student*f=NULL,*r=NULL;
void addQ();
void deleteQ();
void display();
void main()
{
clrscr();
int choice;
char ans;
do
{
clrscr();
cout<<"1.Insert\n2.Delete\n3.display\n0.exit";
cout<<"\nEnterchoice:";
cin>>choice;
switch(choice)

ARJUN TYAGI XII-D 99


COMPUTER SCIENCE PRACTICAL FILE

{
case1: addQ();
break;
case2: deleteQ();
break;
case3: display();
break;
case0: exit(0);
}
cout<<"\nDoyouwanttocontinue?";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void addQ()
{
student*ptr=newstudent;
cout<<"Enter rollno:";
cin>>ptr->Stu_no;
cout<<"Enter name:";
gets(ptr->Stu_name);
cout<<"Enter percentage:";
cin>>ptr->percentage;
ptr->next=NULL;
if(r==NULL)
{
f=r=ptr;
cout<<"node inserted";
}
else
{
r->next=ptr;
r=ptr;
cout<<"node inserted";
}
}
void deleteQ()
{

ARJUN TYAGI XII-D 100


COMPUTER SCIENCE PRACTICAL FILE

if(f==NULL)
{
cout<<"stackempty";
return;
}
elseif(f==r)
{
student*temp=f;
f=r=NULL;
cout<<"nodetobrdeletedis:"<<temp->Stu_no<<""<<temp-
>Stu_name;
delete temp;
}
else
{
student*temp=f;
f=f->next;
cout<<"nodetobrdeletedis:"<<temp->Stu_no<<""<<temp-
>Stu_name;
delete temp;
}
}
void display()
{
if(f==NULL)
cout<<"Queueempty";
else
{
student*temp=f;
while(temp!=NULL)
{
cout<<temp->Stu_no<<""<<temp->Stu_name<<"<-"<<"";
temp=temp->next;
}
}
}

ARJUN TYAGI XII-D 101


COMPUTER SCIENCE PRACTICAL FILE

OUTPUT

ARJUN TYAGI XII-D 102


COMPUTER SCIENCE PRACTICAL FILE

Q24. The following data items are to be stored for a student:


Stu_no, Stu_name, Percentage
Write an object oriented program to store the data for a student in a
STACK using LINKED implementation. Your program should have the
following member functions.
i) push
ii) pop
iii) display
These functions are called as per the user’s choice.

CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct student
{
int Stu_no;
char Stu_name[10];
int percentage;
student* next;
};
student*top=NULL;
void push();
void pop();
void display();
void main()
{
clrscr();
intchoice;
char ans;
do
{
cout<<"1.Push\n2.Pop\n3.display\n0.exit";
cout<<"\nEnter choice:";
cin>>choice;
switch(choice)
{

ARJUN TYAGI XII-D 103


COMPUTER SCIENCE PRACTICAL FILE

case1: push();
break;
case2: pop();
break;
case3: display();
break;
case0: exit(0);
}
cout<<"\nDo you want to continue";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void push()
{
student*ptr=newstudent;
cout<<"Enter rno:";
cin>>ptr->Stu_no;
cout<<"Enter name:";
gets(ptr->Stu_name);
cout<<"Enter percentage:";
cin>>ptr->percentage;
ptr->next=NULL;
if(top==NULL)
{
top=ptr;
}
else
{
ptr->next=top;
top=ptr;
}
cout<<"node pushed";
}
void pop()
{
if(top==NULL)
cout<<"Nothing to delete. Stackempty";

ARJUN TYAGI XII-D 104


COMPUTER SCIENCE PRACTICAL FILE

else
{
student*temp=top;
cout<<"Node to be delete"<<temp->Stu_no<<""<<temp-
>Stu_name;
top=top->next;
delete temp;
cout<<"node deleted";
}
}
void display()
{
if(top==NULL)
{
cout<<"stack empty";
}
else
{
student*temp=top;
while(temp!=NULL)
{
cout<<temp->Stu_no<<""<<temp->Stu_name;
temp=temp->next;
}
}
}

OUTPUT

ARJUN TYAGI XII-D 105


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 106


COMPUTER SCIENCE PRACTICAL FILE

Q25. A QUEUE is to be created using circular array implementation each


element of the queue having marks of the students. Front and Rear are
two variables that indicate the position of the first and the last element.
Write the following functions:
i) QINSERT() to add an element in to t he QUEUE. [Check for
overflow].
ii) QDELETE() to remove an element from QUEUE [Check for
underflow].
iii) QDISPLAY() to display all elements of the QUEUE.
Write a complete object oriented menu driven program to implement the
QUEUE.

CODING
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size=50;
void insert(intCQ[size], int&f, int&r);
void Delete(intCQ[size], int&f, int&r);
void Display(intCQ[size], int&f, int&r);
voidmain()
{
int CQ[size],f=-1,r=-1;
clrscr();
int choice;
char ans;
do
{
clrscr();
cout<<"1.Insert\n2.Delete\n3.display\n0.exit"<<endl;
cout<<"Enter choice"<<endl;
cin>>choice;
switch(choice)
{
case1: insert(CQ,f,r);
break;
case2: Delete(CQ,f,r);
break;

ARJUN TYAGI XII-D 107


COMPUTER SCIENCE PRACTICAL FILE

case3: Display(CQ,f,r);
break;
case0: exit(0);
}
cout<<"Do you want to continue";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void insert(intCQ[size],int&f,int&r)
{
if((f==0&&r==size-1)||(f==r+1))
cout<<"Queue full";
elseif(r==-1)
{
f=r=0;
cin>>CQ[r];
}
elseif(r==size-1)
{
r=0;
cin>>CQ[r];
}
else
{
r++;
cin>>CQ[r];
}
}
void Delete(intCQ[size],int&f,int&r)
{
if(f==-1)
cout<<"Circular Queue empty";
elseif(f==r)
{
cout<<"Element to be deleted is:"<<CQ[f];
f=r=-1;
cout<<"Element deleted";

ARJUN TYAGI XII-D 108


COMPUTER SCIENCE PRACTICAL FILE

}
elseif(f==size-1)
{
cout<<"Element to be deleted is:"<<CQ[f];
f=0;
cout<<"Element deleted";
}
else
{
cout<<"Element to be deleted is:"<<CQ[f];
f++;
}
}
void Display(intCQ[size],int&f,int&r)
{
if(f==-1)
cout<<"Circular Queue empty";
else
{
for(inti=f;i<=r;i++)
cout<<CQ[i]<<"<-";
}
}

OUTPUT

ARJUN TYAGI XII-D 109


COMPUTER SCIENCE PRACTICAL FILE

ARJUN TYAGI XII-D 110

You might also like