Escolar Documentos
Profissional Documentos
Cultura Documentos
SCIENCE
PRACTICAL FILE
Questions
Q1. Create the table and Write the SQL queries for the following tables
Table: INTERIORS
NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
1 Red rose Double Bed 23-Feb-2006 32000 15
2 Soft touch Baby cot 20-Jan-2006 9000 10
3 Jerry’s home Baby cot 19-Feb-2006 8500 10
4 Rough woof Office table 01-Jan-2006 20000 20
5 Comfort zone Double bed 12-Jan-2006 15000 20
6 Jerry look Baby cot 24-Feb-2006 7000 19
7 Lion king Office table 20-Feb-2006 16000 20
8 Royal tiger Sofa 22-Feb-2006 30000 25
9 Park sitting Sofa 13-Dec-2005 9000 15
10 Dine Paradise Dining Table 19-Feb-2006 11000 15
(i) To show all information about the Sofa’s from the interiors table.
(ii) To list the item name which are priced more than 10000 from the interiors table.
(iii) To display item name and date of stock of those items, in which the discount
percentage is more than 15.
(iv) To list item name and type of items, in which data of stock is before 22-Jan-2002 in
descending order of item name.
(v) To count the number of items of each type.
(vi) To insert new row in interiors table with following data:
11 White wood Double Bed 23-Feb-2006 20000 20
(vii) To count distinct types from interiors table.
(viii) To find average discount from interiors for each type of interiors.
(ix) To calculate sum of price from interiors where data of stock is before 12-Feb-2002
(x) To increase price of all Office table by 3000 from interiors table.
Q2. Create the tables and Write the SQL queries for the following tables.
Table: STOCK
NO ITEMCODE QUANTITY DATEOFPURCHASE WARRANTY
1 C201 9 21-May-2005 2
2 P1010 3 21-May-2005 4
3 S203 1 29-Sep-2004 3
4 C201 2 13-Jun-2005 1
5 P1010 1 31-Oct-2004 2
6 U34 5 21-May-2005 1
7 P1010 2 11-Jan-2006 2
Q3. Write a menu driven program to add, subtract, multiply and divide 2 complex numbers
using structures.
Q4. Implement class distance having following declaration using a menu driven program.
class distance
{
int feet, inches;
public: distance();
void read_distance();
void disp_distance();
distance add(distance,distance);
distance subtract(distance,distance);
};
Q5. Write a menu driven program to show upper triangle, lower triangle of a 2D matrix
Q6. Write a menu driven program to show sum of primary diagonal and sum of secondary
diagonal of a 2D matrix.
Q7. Write a menu driven program for addition, subtraction and multiplication of 2 matrices.
Check necessary conditions in the respective cases.
Q8. Write a menu driven program to find row sum and column sum in a 2D array.
Q9. Write a program to define a class RADIO_STATION with the following data members:
Name of type string HARSHIT KAWATRA XII D 2
COMPUTER SCIENCE PRACTICAL FILE
Q11. Write a program to input integer data in two arrays. 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.
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.
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() .
Q14. Write a program to input a text file name, read 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. For example, if the original file contains
Physical, Mental, or Emotional harm to anyone is a crime. –Anonymous
Then the text file COPY.TXT shall contain
Physical, Mental, or Emotional harm is . – Anonymous
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.
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
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
{ int ID_No;
char Name[30];
char Address1[40];
char Address2[30];
long Tel_No;
char Status; // ‘M’ for Member and ‘N’ for non-members
public:
long R_Tel_No() { return Tel_no;}
char R_status() { return Status;}
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( ).
Q22. Write a complete menu driven program to implement a QUEUE using an array of float
values. The program should include INSERT ( ), DELQ ( ) and DISPLAY( ).
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 tshe 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
AIM:-Create the table and Write the SQL queries for the following tables
Table: INTERIORS
NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
1 Red rose Double Bed 23-Feb-2006 32000 15
2 Soft touch Baby cot 20-Jan-2006 9000 10
3 Jerry’s home Baby cot 19-Feb-2006 8500 10
4 Rough woof Office table 01-Jan-2006 20000 20
5 Comfort zone Double bed 12-Jan-2006 15000 20
6 Jerry look Baby cot 24-Feb-2006 7000 19
7 Lion king Office table 20-Feb-2006 16000 20
8 Royal tiger Sofa 22-Feb-2006 30000 25
9 Park sitting Sofa 13-Dec-2005 9000 15
10 Dine Paradise Dining Table 19-Feb-2006 11000 15
(i) To show all information about the Sofa’s from the interiors table.
(ii) To list the item name which are priced more than 10000 from the interiors table.
(iii) To display item name and date of stock of those items, in which the discount
percentage is more than 15.
(iv) To list item name and type of items, in which data of stock is before 22-Jan-2002 in
descending order of item name.
(v) To count the number of items of each type.
(vi) To insert new row in interiors table with following data:
11 White wood Double Bed 23-Feb-2006 20000 20
(vii) To count distinct types from interiors table.
(viii) To find average discount from interiors for each type of interiors.
(ix) To calculate sum of price from interiors where data of stock is before 12-Feb-2002
To increase price of all Office table by 3000 from interiors table.
Queries:-
1.)
2.)
3.)
4.)
5.)
6.)
7.)
8.)
9.)
10.)
Aim:- Create the tables and Write the SQL queries for the following tables.
Table: STOCK
NO ITEMCODE QUANTITY DATEOFPURCHASE WARRANTY
1 C201 9 21-May-2005 2
2 P1010 3 21-May-2005 4
3 S203 1 29-Sep-2004 3
4 C201 2 13-Jun-2005 1
5 P1010 1 31-Oct-2004 2
6 U34 5 21-May-2005 1
7 P1010 2 11-Jan-2006 2
Table: ITEM
ITEMCODE ITEMNAME PRICE
C201 Computer 39000
P1010 Printer 11000
S203 Scanner 4500
WC05 Camera 1200
U34 UPS 1900
Queries:-
1.)
2.)
3.)
4.)
5.)
6.)
7.)
8.)
9.)
10.)
struct complex
{
float r;
float i;
}s1,s2;
void main()
{
clrscr();
float a,b;
int l;char k;
cout<<"Enter real and imaginary part of 1st complex number:";
cin>>s1.r>>s1.i;
cout<<"Enter real and imaginary part of 2nd complex number:";
cin>>s2.r>>s2.i;
do{
cout<<"Menu";
cout<<"\n1.Addition"<<endl;
cout<<"2.Subtraction"<<endl;
cout<<"3.Multiplication"<<endl;
cout<<"4. Division"<<endl;
cin>>l;
if(l==1)
{
a=(s1.r)+(s2.r);
b=(s1.i)+(s2.i);
cout<<"\nAddition: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(l==2)
{
a=(s1.r)-(s2.r);
b=(s1.i)-(s2.i);
cout<<"\nSubtraction: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(l==3)
{
a=((s1.r)*(s2.r))-((s1.i)*(s2.i));
b=((s1.r)*(s2.i))+((s2.r)*(s1.i));
cout<<"\nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
else if(l==4)
{
HARSHIT KAWATRA XII D 20
COMPUTER SCIENCE PRACTICAL FILE
//Division
a=(((s1.r)*(s2.r))+((s1.i)*(s2.i)))/(pow(s2.r,2)+pow(s2.i,2));
b=(((s2.r)*(s1.i))-((s1.r)*(s2.i)))/(pow(s2.r,2)+pow(s2.i,2));
cout<<"\nDivision: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
cout<<"\ndo u want to continue";
cin>>k;
}while(k=='y');
getch();
}
Output:-
Code:-
#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inches;
public:
distance();
void read_distance();
void disp_distance();
distance add(distance,distance);
distance subtract(distance,distance);
};
distance::distance()
{
feet=0;
inches=0;
}
void distance::read_distance()
{
cout<<"\nEnter the no. of feet";
cin>>feet;
cout<<"\nEnter the no. of inches";
cin>>inches;
}
void distance::disp_distance()
{
cout<<"The new distance is "<<feet<<" f "<<inches<<" i ";
}
distance distance::add(distance a,distance b)
{
distance c;
c.feet=a.feet+b.feet;
c.inches=a.inches+b.inches;
return c;
}
distance distance::subtract(distance a,distance b)
HARSHIT KAWATRA XII D 22
COMPUTER SCIENCE PRACTICAL FILE
{
distance c;
c.feet=a.feet-b.feet;
c.inches=a.inches-b.inches;
return c;
}
void main()
{
clrscr();
int a;
distance d1,d2,d3;
d1.read_distance();
d2.read_distance();
cout<<"\nWhat do u want to do";
cout<<"\n1.Add";
cout<<"\n2.Subtract";
cin>>a;
if(a==1)
d3=d3.add(d1,d2);
else if (a==2)
d3=d3.subtract(d1,d2);
d3.disp_distance();
getch();
}
Output:-
cout<<"\n";
}
}
cout<<"\ndo you want to continue[y/n] ";
cin>>s;
}while(s=='y');
getch();
}
Output:-
}
cout<<"\nThe sum of primary diagnol is "<<pdsum;
}
else if(l==2)
{
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i+j==(r-1))
sdsum+=a[i][j];
}
}
cout<<"\n The sum of secondry diagnol is "<<sdsum;
}
HARSHIT KAWATRA XII D 26
COMPUTER SCIENCE PRACTICAL FILE
getch();
}
Output:-
if(n==2)
{
for (i=0;i< a;i++ )
{
for (j=0;j<b;j++ )
{
subtract[i][j] = first[i][j] - second[i][j];
}
}
cout << "\nSubtraction of entered matrices : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << subtract[i][j] << "\t";
Output:-
Aim:-Write a menu driven program to find row sum and column sum in a
2D array.
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
int a[10][10],r,c,rsum=0,csum=0,l;
char s;
cout<<"Enter no. of rows and coloumns";
cin>>r>>c;
cout<<"enter elements of matrix ";
for(int i=0; i<r;i++)
{ for(int j=0; j<c;j++)
{
cin>>a[i][j];
}
}
do{
cout<<"\n1. Row sum";
cout<<"\n2. Coloumn sum";
cin>>l;
if(l==1)
{
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
rsum+=a[i][j];
}
cout<<"\nThe sum of row "<<i+1<<" is "<<rsum;
}
}
else if(l==2)
{
for(i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
csum+=a[i][j];
}
cout<<"\nThe sum of coloumn "<<i+1<<" is "<<csum;
}}
cout<<"\n do u want to continue";
cin>>s;
HARSHIT KAWATRA XII D 31
COMPUTER SCIENCE PRACTICAL FILE
}while(s=='y');
getch();
}
Output:-
class RADIO_STATION
{
char Name[20];
float Ent_marks,Aud_marks;
void assign();
public:
char result;
void accept();
void show();
void select();
};
void RADIO_STATION::accept()
{
cout<<"Enter name"<<endl;
cin>>Name;
cout<<"Enter entrance exam marks out of 30";
cin>>Ent_marks;
cout<<"Enter audition exam marks out of 70";
cin>>Aud_marks;
assign();
}
void RADIO_STATION::assign()
{
if(Ent_marks+Aud_marks>80&&Aud_marks>60)
result='S';
else if(Ent_marks+Aud_marks>80)
HARSHIT KAWATRA XII D 33
COMPUTER SCIENCE PRACTICAL FILE
result='W';
else
result='R';
}
void RADIO_STATION::show()
{
cout<<"Name "<<Name<<" Entrance marks "<<Ent_marks<<" Audition marks
"<<Aud_marks<<" Result:"<<result<<endl;
}
void RADIO_STATION::select()
{
if(result=='S')
show();
}
void main()
{
clrscr();
RADIO_STATION r[20];
int n,m;
char j;
cout<<"enter no. of students";
cin>>n;
do
{
clrscr();
cout<<"MENU"<<endl;
cout<<"1.Read data";
cout<<"\n2.Show data";
cout<<"\n3.Show selected canditades";
cin>>m;
if(m==1)
{
for(int i=0;i<n;i++)
r[i].accept();
}
else if(m==2)
{
for(int i=0;i<n;i++)
r[i].show();
}
else if(m==3)
{
for(int i=0;i<n;i++)
r[i].select();
}
cout<<"do u want to continue[y/n]";
cin>>j;
}while(j=='y');
getch();
}
Output:-
Code:-
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Lsearch(int arr[],int n,int i);
int Bsearch(int arr[], int n,int i);
void blesort(int arr[],int n);
void main()
{
clrscr();
int n, arr[20], item, index,x;char y;
cout<<"Enter size of array "<<endl;
cin>>n;
cout<<"Enter elements of array "<<endl;
for(int i=0; i<n;i++)
{
cin>>arr[i];
}
cout<<"Elements entered are ";
for( i=0; i<n;i++)
{
cout<<arr[i]<<'\t';
}
getch();
do
{
clrscr();
cout<<"** SEARCHING IN THE ARRAY **"<<endl;
cout<<"1. Linear Search "<<endl;
cout<<"2. Binary search "<<endl;
cout<<" Enter choice "<<endl;
cin>>x;
cout<<"Enter the element to be found "<<endl;
cin>>item;
switch(x)
{
case 1:
index=Lsearch(arr,n,item);
break;
case 2:
index=Bsearch(arr,n,item);
break;
}
if(index==-1)
{
HARSHIT KAWATRA XII D 36
COMPUTER SCIENCE PRACTICAL FILE
}
int Lsearch(int arr[20],int n,int i)
{
blesort(arr,n);
for(int k=0;k<n;k++)
{
if(arr[k]==i)
return i;
}
return -1;
}
int Bsearch(int arr[20],int n, int i)
{
blesort(arr,n);
int temp,beg=0,last=n-1,mid,pos;
while(beg<=last)
{
mid=(beg+last)/2;
if(arr[mid]>i)
last=mid-1;
else if(arr[mid]<i)
beg=mid+1;
else if(arr[mid]==i)
return i;
}
return -1;
}
void blesort(int arr[20],int n)
{
int temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
cout<<" Array after pass "<<i+1<<endl;
for( j=0;j<n;j++)
cout<<arr[j];
cout<<endl;
}
}
Output:-
Aim:- Write a program to input integer data in two arrays. 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.
Code:-
#include<iostream.h>
#include<conio.h>
void selsort(int a[10],int n)
{
int small,pos,temp;
for(int i=0;i<n-1;i++)
{
small=a[i]; pos=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
cout<<"\narray after pass"<<i+1<<'\n';
for(j=0;j<n;j++)
cout<<a[j]<<' ';
}
}
void mergesort(int a[10],int n,int b[10],int m,int c[20],int mn)
{
int l,k,h;
for( l=0,k=m-1,h=0; l<=m&&k>=0;)
{
if(a[l]<=b[k])
c[h++]=a[l++];
else
c[h++]=b[k--];
}
if(l<n)
{
while(l<n)
c[h++]=a[l++];
}
else
{
while(b>=0)
c[h++]=b[k--];
}
for(int i=0;i<mn-1;i++)
{
cout<<c[i]<<"\t";
}
}
void bubblesort(int b[10],int m)
{
int temp;
for(int i=0;i<m;i++)
{
for(int j=0;j<m-1-i;j++)
{
if(b[j]<b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
cout<<" \n Array after pass "<<i+1<<endl;
for( j=0;j<m;j++)
cout<<b[j];
cout<<endl;
}
}
void main()
{
clrscr();
int a[10],b[10],m,n,c[20],mn;
cout<<"Enter no. of elements in array A ";
cin>>n;
cout<<"\nEnter the elements of array A";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter no. of elements in array B ";
cin>>m;
cout<<"\nEnter the elements in array B";
for( i=0;i<m;i++)
{
cin>>b[i];
}
cout<<" Arranging array A in ascerding order "<<endl;
selsort(a,n);
cout<<"Arranging array B in descending order "<<endl;
bubblesort(b,m);
cout<<"Merging both arrays "<<endl;//ends here
mn=m+n;
mergesort(a,n,b,m,c,mn);
getch();
}
Output:-
Aim:- 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.
Code:-
#include<iostream.h>
#include<conio.h>
#include<process.h>
int findpos(int arr[50],int n, int item)
{
int pos;
if(item<arr[0])
pos=0;
else
{
for(int i=0;i<n-1;i++)
{
if(arr[i]<=item&& item<n)
{
pos=i+1;break;
}
}
if(i==n-1)
pos=n;
}
return pos;
}
int lsearch(int arr[50], int n, int item)
{
for(int i=0; i<n; i++)
if(arr[i]==item)
return i;
return -1;
}
void insertion(int arr[50],int n)
{
int item,index;
char ch='y';
while(ch=='y')
{
cout<<"Enter element to be inserted"<<endl;
cin>>item;
if(n==50)
{
cout<<"overflow";
getch();
exit(-1);
}
index=findpos(arr,n,item);
for(int i=n;i>index;i--)
{
arr[i]=arr[i-1];}
arr[index]=item;
n=n+1;
}
index=lsearch(arr,n,item);
if(index!=-1)
arr[index]=0;
else
cout<<"element not found";
cout<<"now element from array is deleted and 0 signifies the
deleted element "<<endl;
for(int i=0;i<n;i++)
cout<<arr[i];
cout<<"Shifting emptied space to right"<<endl;
for(i=index;i<n;i++)
{
arr[i]=arr[i+1];
}
n-=1;
cout<<"Do you want to delete more ";
cin>>ch;
}
cout<<"Final array ";
for(int i=0;i<n;i++)
cout<<arr[i];
for(int i=1;i<n;i++)
{
temp=arr[i];
j=i-1;
while(temp>arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
cout<<"\n array after pass"<<i<<"\n";
for(int k=0;k<=n;k++)
cout<<arr[k];
}
}
void selectsort(int arr[50], int n)
{
int small,pos,temp;
for(int i=0;i<n-1;i++)
{
small=arr[i]; pos=i;
for(int j=i+1;j<n;j++)
{
if(arr[j]<small)
{
small=arr[j];
pos=j;
}
}
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
cout<<"array after pass"<<i+1<<'\n';
for(j=0;j<n;j++)
cout<<arr[j];
}
}
void disp(int arr[50], int n)
{
for(int i=0;i<n;i++)
cout<<arr[i];
}
void main()
{
clrscr();
int arr[50],n,choice;char l='y';
cout<<"Enter no. of elements in array"<<endl;
cin>>n;
cout<<"Enter elements";
for(int i=0;i<n;i++)
cin>>arr[i];
do
{
clrscr();
cout<<"1. Insertion in array"<<endl;
cout<<"2. Deletion in array"<<endl;
cout<<"3. Insertion sort "<<endl;
cout<<"4. Selection sort "<<endl;
cout<<"5. display array "<<endl;
cin>> choice;
if(choice==1)
insertion(arr,n);
else if(choice==2)
deletion(arr,n);
else if(choice==3)
insertsort(arr,n);
else if(choice==4)
selectsort(arr,n);
else if(choice==5)
disp(arr,n);
Output:-
Aim:- 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() .
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class publication
{
char title[20];float price;
public:
void getdata1()
{
cout<<"Enter the title and price ";
gets(title);
cin>>price;
}
void putdata1()
{
cout<<"The title is "<<title<<" and its price is "<<price<<endl;
}
};
class book:public publication
{
int pgcnt;
public:
void getdata2()
{
cout<<"Enter the page count of book ";
cin>>pgcnt;
}
void putdata2()
{
cout<<"The page count is "<<pgcnt<<endl;
}
};
class tape:public publication
{
float min;
public:
void getdata3()
{
cout<<"Enter the duration in min (for 2 min 5 sec write 2.05) ";
HARSHIT KAWATRA XII D 48
COMPUTER SCIENCE PRACTICAL FILE
cin>>min;
}
void putdata3()
{
cout<<"The duration of tape is "<<min<<endl;
}
};
void main()
{
clrscr();
int choice;
char cont;
tape t; book b;
do
{
cout<<"**CHOOSE THE TYPE OF PUBlICATION**";
cout<<"\n1. BOOK ";
cout<<"\n2. TAPE ";
cin>>choice;
if(choice==1)
{
b.getdata1();
b.getdata2();
cout<<"The details of your publication are "<<endl;
b.putdata1();
b.putdata2();
}
else if (choice==2)
{
t.getdata1();
t.getdata3();
cout<<"The details of your publication are "<<endl;
t.putdata1();
t.putdata3();
}
cout<<" Do you want to continue ";
cin>>cont;
}while(cont=='y');
getch();
}
Output:-
Aim:- Write a program to input a text file name, read 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.
Code:-
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
void main()
{
clrscr();
char name[20],word[100];
int l;
cout<<"Enter name of file ";
gets(name);
ofstream fout; ifstream fin;
fout.open("copy.txt");
fin.open(name);
if(!fin)
{
cout<<"error opening file";
getch();
exit(-1);
}
while(!fin.eof())
{
fin>>word;
for(int i=0;word[i]!='\0';i++);
if(word[i-1]=='a'||word[i-1]=='e'||word[i-1]=='i'||word[i-
1]=='o'||word[i-1]=='u')
{
}
else fout<<word<<endl;
}
getch();
}
Output:-
}
fio.close();
}
Output:-
Aim:- 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
Code:-
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
clrscr();
int b=0,l=0,c=0,s=0,lc=0,w=0,d=0,wv=0;
char ch1,ch,file[20],line[200];
ifstream fin;
cout<<"Enter the name of text file ";
cin>>file;
fin.open(file);
while(fin)
{
fin.get(ch);
if(isupper(ch))
c++;
else if(ch==' ')
{b++;w++;}
else if(islower(ch))
s++;
else if(isdigit(ch))
d++;
else if(ch=='.'||ch=='\n')
{
l++;w++;
if(ch+1>='A'&&ch+1<='Z')
lc++;
}
else if(ch=='a'||ch=='e'||ch=='o'||ch=='i'||ch=='u')
if(ch+1==' '||ch+1=='.'||ch+1=='\n')
wv++;
}
cout<<"The no. of blanks are "<<b<<endl;
cout<<"The no. of lines are "<<l<<endl;
cout<<"The no. of capital alphabets are "<<c<<endl;
Output:-
}
void display()
{
cout<<"Movie name: "<<moviename<<endl;
if(strcmpi(m.ret_name(),nm)==0)
{ flag='y';
m.display();
}
}
if(flag=='n')
cout<<"Record not found";
fio.close();
break;
case 4:
fio.open("MOVIE.DAT",ios::binary);
while(fio)
{
fio.read((char*)&m,sizeof(m));
if(m.ret_price()>200)
{
m.changeprice();
}
}
cout<<"price changed"<<endl;
fio.close();
break;
case 5:
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();
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;
case 6:
exit(0);
break;
}
cout<<"do you want to continue";
cin>>k;
}while(k=='y');
getch();
}
Output:-
Code:-
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class Labs
{
int Lab_Code;
char Department[30];
int Number;
public:
void get_data()
{
cout<<"LabCode :"; cin>>Lab_Code;
cout<<"Department :"; cin>>Department;
cout<<"TotalLabs :"; cin>>Number;
}
void Disp_data()
{
cout<<"LabCode "<<Lab_Code<<endl;
cout<<"Department:"<<Department<<endl;
cout<<"Total Labs:"<<Number<<endl;
}
int Get_code()
{
return Lab_Code;
}
};
void createfile()
{
char ans;
Labs l;
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);
while(fio)
{
if(fio.eof())
break;
fio.read((char*)&l,sizeof(l));
l.Disp_data();
}
fio.close();
}
void modify()
{
int r,pos=0;char flag='n';
fstream fio("labs.dat",ios::binary|ios::in|ios::out);
Labs l;
cout<<"\n Enter the LabCode of record to be modified ";
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<<"RECORD MODIFIED! "<<endl;
break;
}
}
if(flag=='n')
cout<<"not found";
fio.close();
}
void main()
{
int x,n;
char ans;
Labs l;
do{
clrscr();
cout<<"***MENU***"<<endl;
cout<<"1.Create file and Input records"<<endl;
cout<<"2.Display records"<<endl;
cout<<"3.Replace records"<<endl;
cout<<"4.Exit"<<endl;
cin>>x;
switch(x)
{
case 1:
createfile();
break;
case 2:
display();
break;
case 3:
modify();
break;
case 4:
exit(0);
break;
}
cout<<"Do you want to continue ";
cin>>ans;
}while(ans=='y');
getch();
}
Output:-
Aim:- Write a C++ program to create two text files and then create a third
text file, 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.
Code:-
/*
* C Program that Merges Lines Alternatively from 2 Files & Print Result
*/
#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];
ifstream fin1,fin2;
ofstream fout;
fin1.open(file1,ios::binary); /*opening the files in read only
mode*/
fin2.open(file2,ios::binary);
fout.open(file3,ios::binary|ios::app); /*opening a new file in
write,update mode*/
char str1[200];
char ch1, ch2;
while (!fin1.eof()&&!fin2.eof())
{
if (!fin1.eof()) /*getting lines in alternately from two
files*/
{
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;
}
}
Output:-
Aim:- 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
{ int ID_No;
char Name[30];
char Address1[40];
char Address2[30];
long Tel_No;
char Status; // ‘M’ for Member and ‘N’ for non-members
public:
long R_Tel_No() { return Tel_no;}
char R_status() { return Status;}
void Assign_Tel(long Tel) (Tel_No = Tel ;)
char* R_Name() { return Name ;}
};
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.
Code:-
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class store
{
int ID_No;
char Name[30];
char Address1[40];
char Address2[30];
long Tel_No;
char Status;
public:
void readdata()
{
cout<<"ID NO.:";cin>>ID_No;
cout<<"Enter name: ";gets(Name);
cout<"Enter Address Line 1: ";
gets(Address1);
gets(Address2);
cout<<"Enter Telephone No.:";cin>>Tel_No;
cout<<"Enter Status[M/N]";cin>>Status;
}
void dispdata()
{
cout<<"ID NO.:"<<ID_No<<endl;
cout<<"Name: "<<Name<<endl;
cout<<"Address Line 1: "<<Address1<<endl;
cout<<"Address Line 2: "<<Address2<<endl;
cout<<"Telephone NO.:"<<Tel_No<<endl;
cout<<"Status:"<<Status<<endl;;
}
long R_Tel_No()
{
return Tel_No;
}
char R_status()
{
return Status;
}
void Assign_Tel(long Tel)
{
Tel_No = Tel;
}
char* R_Name()
{
return Name;
}
int R_ID()
{
return ID_No;
}
};
void CreateFile()
{
ofstream fout("customer.dat",ios::binary|ios::app);
char ans;
do
{
store s;
s.readdata();
fout.write((char*)&s,sizeof(s));
cout<<"do you want to add more records ";
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;
if(s.R_status()=='M'||s.R_status()=='m')
s.dispdata();
}
fin.close();
}
void modify()
{
store s;
long Tel;int sId;char ans;
cout<<"Enter ID of the record for changing no."; cin>>sId;
ifstream fin("customer.dat",ios::binary);
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(s.R_ID()==sId);
{
s.dispdata();
cout<<"Do you want to change this record"<<endl;
cin>>ans;
if(ans=='y')
{
cout<<"Enter new Telephone NO.";
cin>>Tel;
s.Assign_Tel(Tel);
cout<<"Record changed";
}
}
}
}
void Display()
{
ifstream fin("customer.dat",ios::binary);
store s;
while(fin)
{
fin.read((char*)&s,sizeof(s));
if(fin.eof())
break;
s.dispdata();
}
fin.close();
}
void main()
{
int ans;
char x;
do{
clrscr();
cout<<"1.To Create File and input records"<<endl;
cout<<"2.To view records of all the members"<<endl;
cout<<"3.To Change telephone No. of Customer"<<endl;
cout<<"4.To Diplay All the contents";
cout<<"Enter choice"<<endl;
cin>>ans;
switch(ans)
{
case 1:
CreateFile();
break;
case 2:
DisplayMember();
break;
case 3:
modify();
break;
case 4:
Display();
break;
}
cout<<"do you want to continue";
cin>>x;
}while(x=='y');
}
Output:-
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
void push()
{
float val;
if(top==MAX-1)
{
cout<<"\nStack is full!!";
}
else
{
cout<<"\nEnter element to push:";
cin>>val;
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
cout<<"\nStack is empty!!";
}
else
{
cout<<"\nDeleted element is: "<<stack[top];
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
cout<<"\nStack is empty!!";
}
else
{
cout<<"\nStack is...\n";
for(i=top;i>=0;--i)
cout<<stack[i]<<endl;
}
}
Output:-
else
{
cout<<"Enter the element to be inserted: ";
cin>>item;
rear=rear+1;
queue[rear]=item;
if(front==-1)
front=0;
}
}
void delq()
{
float item;
if(front==-1)
{
cout<<"\nQueue Underflow:";
}
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;
}
}
Output:-
Output:-
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.Stack empty";
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:-
void main()
{
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)
{
case 1:insert(CQ,f,r);
break;
case 2:Delete(CQ,f,r);
break;
case 3:Display(CQ,f,r);
break;
case 0:exit(0);
}cout<<"Do you want to continue";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void insert(int CQ[size],int &f,int &r)
{
if((f==0&&r==size-1)||(f==r+1))
cout<<"Queue full";
else if(r==-1)
{
f=r=0;
cin>>CQ[r];
}
else if(r==size-1)
{
r=0;
cin>>CQ[r];
}
else
{
r++;
cin>>CQ[r];
}
}
void Delete(int CQ[size],int &f,int &r)
{
if(f==-1)
cout<<"Circular Queue empty";
else if(f==r)
{
cout<<"Element to be deleted is:"<<CQ[f];
f=r=-1;
cout<<"Element deleted";
}
else if(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(int CQ[size],int &f,int &r)
{
if(f==-1)
cout<<"Circular Queue empty";
else
{
for(int i=f;i<=r;i++)
cout<<CQ[i]<<"<-";
}
}
Output:-