You are on page 1of 97

COMPUTER

SCIENCE
PRACTICAL FILE

HARSHIT KAWATRA XII D


XII D
COMPUTER 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

Table: ITEM HARSHIT KAWATRA XII D 1


COMPUTER SCIENCE PRACTICAL FILE

ITEMCODE ITEMNAME PRICE


C201 Computer 39000
P1010 Printer 11000
S203 Scanner 4500
WC05 Camera 1200
U34 UPS 1900

(i) To select item purchased after 31-Jan-2005.


(ii) To list the item name in descending order of date of purchase where quantity is more
than three
(iii) To count number of Items (total quantity) whose cost is more than 10000.
(iv) To insert a new record in the table STOCK with following data:
8 C201 5 13-Jun-2005 1
(v) To find minimum distinct item type (itemcode) from STOCK.
(vi) To generate a report on each item code with the item name and total value (quantity *
Price).
(vii) To find average cost of all items with more than 2 items.
(viii) To find average cost of items with date of purchase is before 1-Jan-2005.
(ix) To reduce the cost of all UPS by 100.
(x) To delete the table STOCK.

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

Entrance exam marks out of 30(float type)


Audition marks out of 70 (float type) Result
of type char
Member function-
Private – Assign ( ) – to assign the value to result depending upon the following if exam marks
+ audition marks > 80 and audition marks are > 60 then the candidate is selected(S), if
exam marks + audition marks > 80 then wait listed (W) otherwise the candidate is
rejected(R).
Public
- Accept ( )- to input exam, audition marks and then call assign( )
- Show ( ) to display complete data
The program should declare an array of type RADIO_STATION with n number of elements &
should be menu driven with separate functions for:
- reading the array
- display the array
- Search and display list of selected candidates.
Q10. Write a menu driven program to search an integer in a list of integers using any of the
following techniques.
i.Linear Search
ii.Binary Search
Use bubble sort to sort the list, if required.

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

HARSHIT KAWATRA XII D 3


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.

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

Q17. Given class MOVIE with following declarations:


class MOVIE
{ int movieno;
char moviename[25];
float price;
public:
int compare(char nm[ ] )
{ if (strcmp ( moviename, nm) = = 0)
return 1;
else
return 0; }
void input( );
void display ( );
};
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 moviename & 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.

Q18. Consider the following class definition:


class Labs
{
int Lab_Code;
char Department[30];
int Number;
public:
void get_data( )
{
cout << “Lab. Code :”; cin >> Lab_Code;
cout << “Department :”; gets(Department);;
cout << “Total Labs :”; cin >> Number;
}
void Disp_data( )

HARSHIT KAWATRA XII D 4


COMPUTER SCIENCE PRACTICAL FILE

{ cout << “Lab. Code :”; << Lab_Code;


cout << “Department :”; << Department;;
cout << “Total Labs :”; << Number;
}
int Get_code( ) { return Lab_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.
Q19. 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.
E.g. File1.dat contains
Hello
How are you?
Bye
File2.dat contains
Welcome to the Computer Lab
There are 2 Labs in the Senior School
Alternate( )function should take file names as parameters File1.dat, File2.dat &
File3.dat.
File3.dat after execution of function Alternate() should contain
Hello
Welcome to Computer Lab
How are you?
There are 2 Labs in the Senior School
Bye
(iii) Read_file( ) to display contents of file whose name is passed as a parameter.

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

HARSHIT KAWATRA XII D 5


COMPUTER SCIENCE PRACTICAL FILE

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.

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

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.

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.
push(), pop(), display()
These functions are called as per the user’s choice

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

HARSHIT KAWATRA XII D 6


COMPUTER SCIENCE PRACTICAL FILE

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.

HARSHIT KAWATRA XII D 7


COMPUTER SCIENCE PRACTICAL FILE

Queries:-

1.)

HARSHIT KAWATRA XII D 8


COMPUTER SCIENCE PRACTICAL FILE

2.)

3.)

HARSHIT KAWATRA XII D 9


COMPUTER SCIENCE PRACTICAL FILE

4.)

5.)

HARSHIT KAWATRA XII D 10


COMPUTER SCIENCE PRACTICAL FILE

6.)

7.)

HARSHIT KAWATRA XII D 11


COMPUTER SCIENCE PRACTICAL FILE

8.)

9.)

HARSHIT KAWATRA XII D 12


COMPUTER SCIENCE PRACTICAL FILE

10.)

HARSHIT KAWATRA XII D 13


COMPUTER SCIENCE PRACTICAL FILE

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

(i) To select item purchased after 31-Jan-2005.


(ii) To list the item name in descending order of date of purchase where quantity is more
than three
(iii) To count number of Items (total quantity) whose cost is more than 10000.
(iv) To insert a new record in the table STOCK with following data:
8 C201 5 13-Jun-2005 1
(v) To find minimum distinct item type (itemcode) from STOCK.
(vi) To generate a report on each item code with the item name and total value (quantity *
Price).
(vii) To find average cost of all items with more than 2 items.
(viii) To find average cost of items with date of purchase is before 1-Jan-2005.
(ix) To reduce the cost of all UPS by 100.
(x) To delete the table STOCK.

HARSHIT KAWATRA XII D 14


COMPUTER SCIENCE PRACTICAL FILE

Queries:-
1.)

2.)

HARSHIT KAWATRA XII D 15


COMPUTER SCIENCE PRACTICAL FILE

3.)

4.)

HARSHIT KAWATRA XII D 16


COMPUTER SCIENCE PRACTICAL FILE

5.)

6.)

HARSHIT KAWATRA XII D 17


COMPUTER SCIENCE PRACTICAL FILE

7.)

8.)

HARSHIT KAWATRA XII D 18


COMPUTER SCIENCE PRACTICAL FILE

9.)

10.)

HARSHIT KAWATRA XII D 19


COMPUTER SCIENCE PRACTICAL FILE

Aim:-Write a menu driven program to add, subtract, multiply and divide 2


complex numbers using structures.
Code:-
#include<iostream.h>
#include<conio.h>
#include<math.h>

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

HARSHIT KAWATRA XII D 21


COMPUTER SCIENCE PRACTICAL FILE

Aim:- 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);
};

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

HARSHIT KAWATRA XII D 23


COMPUTER SCIENCE PRACTICAL FILE

Aim:-Write a menu driven program to show upper triangle, lower triangle


of a 2D matrix
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
int a[10][10],r,c,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<<"What do you want to show :-"<<endl;
cout<<"1. Upper-triangular matrix"<<endl;
cout<<"2. Lower-triangular matrix"<<endl;
cin>>l;
if(l==1)
{

cout << "Upper-triangular matrix " <<endl;


for(i=0; i<c; i++)
{
for(int j=0; j<(r-i); j++)
{
cout << a[i][j] << " ";
}
cout << "\n";
}
}
else if(l==2)
{
cout << "Lower-triangular matrix " <<endl;
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(j<i)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
HARSHIT KAWATRA XII D 24
COMPUTER SCIENCE PRACTICAL FILE

cout<<"\n";
}
}
cout<<"\ndo you want to continue[y/n] ";
cin>>s;
}while(s=='y');
getch();
}
Output:-

HARSHIT KAWATRA XII D 25


COMPUTER SCIENCE PRACTICAL FILE

Aim:-Write a menu driven program to show sum of primary diagonal and


sum of secondary diagonal of a 2D matrix.
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
int a[10][10],r,c,pdsum=0,sdsum=0,l;
char k;
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<<"1. Show sum of primary diagnol "<<endl;
cout<<"2. Show sum of secondry diagnol "<<endl;
cin>>l;
if(l==1)
{
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i==j)
pdsum+=a[i][j];
}

}
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

cout<<"\n do u want to continue[y/n]";


cin>>k;
}while(k=='y');

getch();
}
Output:-

HARSHIT KAWATRA XII D 27


COMPUTER SCIENCE PRACTICAL FILE

Aim:-Write a menu driven program for addition, subtraction and


multiplication of 2 matrices. Check necessary conditions in the respective
cases.
Code:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a,b, first[10][10], second[10][10], sum[10][10], subtract[10][10], multiply[10][10],n;
char g;
cout << "Enter the number of rows and columns of matrix : "<<endl;
cin >> a >> b;
cout << "\nEnter the elements of first matrix : "<<endl;
for (i=0;i<a;i++ )
{
for (j=0;j<b;j++ )
{
cin >> first[i][j];
}
}
cout << "Enter the elements of second matrix\n"<<endl;
for (i= 0 ;i< a ;i++ )
{
for (j= 0;j<b;j++ )
{
cin >> second[i][j];
}
}
do{
cout<<"\n1.ADD";
cout<<"\n2.SUBTRACT";
cout<<"\n3.MULTIPLY";
cin>>n;
if(n==1)
{
for (i=0;i< a;i++ )
{
for (j=0;j<b;j++ )
{
sum[i][j] = first[i][j] + second[i][j];
}
}
}
cout << "\nSum of entered matrices : "<<endl;
for (i=0 ; i < a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << sum[i][j] << "\t";
HARSHIT KAWATRA XII D 28
COMPUTER SCIENCE PRACTICAL FILE

}cout << endl;


}

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

}cout << endl;


}
}
if(n==3)
{
for (i=0;i< a;i++ )
{
for (j=0;j<b;j++ )
{
multiply[i][j] = first[i][j] * second[i][j];
}
}
cout << "\nMultiplication of entered matrices : "<<endl;
for (i=0 ; i <a ; i++ )
{
for ( j = 0 ; j < b ; j++ )
{
cout << multiply[i][j] << "\t";

}cout << endl;


}
}
cout<<"do u want to continue[y/n]";
cin>>g;
}while(g=='y');
getch();

HARSHIT KAWATRA XII D 29


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 30


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 32


COMPUTER SCIENCE PRACTICAL FILE

Aim:-Write a program to define a class RADIO_STATION with the


following data members:
Name of type string
Entrance exam marks out of 30(float type)
Audition marks out of 70 (float type) Result
of type char
Member function-
Private – Assign ( ) – to assign the value to result depending upon the following if exam marks
+ audition marks > 80 and audition marks are > 60 then the candidate is selected(S), if
exam marks + audition marks > 80 then wait listed (W) otherwise the candidate is
rejected(R).
Public
- Accept ( )- to input exam, audition marks and then call assign( )
- Show ( ) to display complete data
The program should declare an array of type RADIO_STATION with n number of elements &
should be menu driven with separate functions for:
- reading the array
- display the array
Search and display list of selected candidates.
Code:-
#include<iostream.h>
#include<conio.h>
#include<process.h>

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

HARSHIT KAWATRA XII D 34


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 35


COMPUTER SCIENCE PRACTICAL FILE

Aim:- Write a menu driven program to search an integer in a list of integers


using any of the following techniques.
i.Linear Search
ii.Binary Search
Use bubble sort to sort the list, if required.

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

cout<<" Element not found ";


getch();
exit(-1);
}
else
cout<<"ELement found at "<<index+1<<endl;

cout<<"Do you want to continue "<<endl;


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

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

HARSHIT KAWATRA XII D 37


COMPUTER SCIENCE PRACTICAL FILE

cout<<endl;
}
}

Output:-

HARSHIT KAWATRA XII D 38


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 39


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 40


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 41


COMPUTER SCIENCE PRACTICAL FILE

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;

HARSHIT KAWATRA XII D 42


COMPUTER SCIENCE PRACTICAL FILE

cout<<"want to insert more elements";


cin>>ch;
}
cout<<"Final array ";
for(int i=0;i<n;i++)
cout<<arr[i];
}

void deletion(int arr[50],int n)


{
int item,index;
char ch='y';
while(ch=='y')
{
cout<<"Enter element to be deleted"<<endl;
cin>>item;
if(n==0)
{
cout<<"underflow";
getch();
exit(-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];

void insertsort(int arr[50], int n)


{
int temp,j;

for(int i=1;i<n;i++)
{
temp=arr[i];
j=i-1;
while(temp>arr[j])
{
arr[j+1]=arr[j];

HARSHIT KAWATRA XII D 43


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 44


COMPUTER SCIENCE PRACTICAL FILE

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

cout<<"Do you want to continue "<<endl;


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

Output:-

HARSHIT KAWATRA XII D 45


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 46


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 47


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 49


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 50


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 51


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 52


COMPUTER SCIENCE PRACTICAL FILE

Aim:- 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.
Code:-
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char name[50],srh,rep,ch,str[200];
int l;
fstream fio;
cout<<"Enter the name of the file ";
cin>>name;
fio.open(name,ios::in|ios::out);
cout<<"enter the character to be replaced ";
cin>>srh;
cout<<"Enter the character to be replaced with ";
cin>>rep;
while(fio)
{
fio.get(ch);
if(ch==srh)
{
ch=rep;
fio.seekg(-1,ios::cur);
fio<<ch;
}

}
fio.close();
}

HARSHIT KAWATRA XII D 53


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 54


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 55


COMPUTER SCIENCE PRACTICAL FILE

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;

HARSHIT KAWATRA XII D 56


COMPUTER SCIENCE PRACTICAL FILE

cout<<"The no. of small alphabets are "<<s<<endl;


cout<<"The no. of lines starting with capaital letters are "<<lc<<endl;
cout<<"The no. of words are "<<w<<endl;
cout<<"The no. of digits are "<<d<<endl;
cout<<"The no. of words ending with vowel are "<<wv<<endl;
fin.close();
getch();
}

Output:-

HARSHIT KAWATRA XII D 57


COMPUTER SCIENCE PRACTICAL FILE

Aim:- Given class MOVIE with following declarations:


class MOVIE
{ int movieno;
char moviename[25];
float price;
public:
int compare(char nm[ ] )
{ if (strcmp ( moviename, nm) = = 0)
return 1;
else
return 0; }
void input( );
void display ( );
};
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 moviename & 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.
Code:-
#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(char nm[ ])
{ if (strcmp(moviename,nm)==0)
return 1;
else
return 0;
}
void input()
{
cout<<"moviename:";cin>>moviename;
cout<<"movieno:";cin>>movieno;
cout<<"price:";cin>>price;

}
void display()
{
cout<<"Movie name: "<<moviename<<endl;

HARSHIT KAWATRA XII D 58


COMPUTER SCIENCE PRACTICAL FILE

cout<<"Movie no: "<<movieno<<endl;


cout<<"Price: "<<price<<endl;
}
char* ret_name()
{
return moviename;
}
int ret_price()
{
return price;
}
void changeprice()
{
price+=(0.10*price);
}
};
void main()
{
clrscr();
MOVIE m;
char ch,nm[30],k;
int x,z,n;
fstream fio;
do{
clrscr();
cout<<"**MENU**"<<endl;
cout<<"1.Create file "<<endl;
cout<<"2.Append Records"<<endl;
cout<<"3.Search movie"<<endl;
cout<<"4.Edit movies "<<endl;
cout<<"5.Delete a particular record "<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter choice ";
cin>>x;
switch(x)
{
case 1:
fio.open("MOVIE.DAT",ios::binary);
fio.close();
cout<<"FILE CREATED!";
break;
case 2:
do
{
fio.open("MOVIE.DAT",ios::app);
m.input();
fio.write((char*)&m,sizeof(m));
cout<<"Do you want to add more";
cin>>ch;
}while(ch=='y');
break;
case 3:
cout<<"Enter name of movie to be searched ";
gets(nm);
char flag='n';
while(fio)
{
fio.read((char*)&m,sizeof(m));

HARSHIT KAWATRA XII D 59


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 60


COMPUTER SCIENCE PRACTICAL FILE

break;
}
cout<<"do you want to continue";
cin>>k;
}while(k=='y');
getch();
}
Output:-

HARSHIT KAWATRA XII D 61


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 62


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 63


COMPUTER SCIENCE PRACTICAL FILE

Aim:- Consider the following class definition:


class Labs
{
int Lab_Code;
char Department[30];
int Number;
public:
void get_data( )
{
cout << “Lab. Code :”; cin >> Lab_Code;
cout << “Department :”; gets(Department);;
cout << “Total Labs :”; cin >> Number;
}
void Disp_data( )
{ cout << “Lab. Code :”; << Lab_Code;
cout << “Department :”; << Department;;
cout << “Total Labs :”; << Number;
}
int Get_code( ) { return Lab_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.

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

HARSHIT KAWATRA XII D 64


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 65


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 66


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 67


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 68


COMPUTER SCIENCE PRACTICAL FILE

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

cout<<"enter the name of file 1"; /*getting the names of file to be


concatenated*/
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); /*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*/

HARSHIT KAWATRA XII D 69


COMPUTER SCIENCE PRACTICAL FILE

{
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:-

HARSHIT KAWATRA XII D 70


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 71


COMPUTER SCIENCE PRACTICAL FILE

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;

HARSHIT KAWATRA XII D 72


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 73


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 74


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 75


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 76


COMPUTER SCIENCE PRACTICAL FILE

Aim:- Write a complete menu driven program to implement a STACK using


an array of float
values. The program should include POP ( ), PUSH ( ) and DISPLAY( ).
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
int top=-1;
float stack[MAX];
void push();
void pop();
void display();
void main()
{
clrscr();
int ch;
while(1)
{
clrscr();
cout<<"\n*** Stack Menu ***";
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter your choice(1-4):";
cin>>ch;

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);

default: cout<<"\nWrong Choice!!";


}
}
getch();
}

void push()
{
float val;
if(top==MAX-1)
{
cout<<"\nStack is full!!";
}
else
{
cout<<"\nEnter element to push:";

HARSHIT KAWATRA XII D 77


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 78


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 79


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 80


COMPUTER SCIENCE PRACTICAL FILE

Aim:- Write a complete menu driven program to implement a QUEUE using


an array of float values. The program should include INSERT ( ), DELQ ( )
and DISPLAY( ).
Code:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#define max 5
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)
{
case 1:
insert();
break;
case 2:
delq();
break;
case 3:
display();
break;
case 4:
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<<"Queue Overflow:";
}

HARSHIT KAWATRA XII D 81


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 82


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 83


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 84


COMPUTER SCIENCE PRACTICAL FILE

Aim:- 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.
Code;-
#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<<"\nEnter choice:";
cin>>choice;
switch(choice)
{
case 1:addQ();
break;
case 2:deleteQ();
break;
case 3:display();
break;
case 0:exit(0);
}cout<<"\nDo you want to continue ?";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void addQ()
{
student *ptr=new student;

HARSHIT KAWATRA XII D 85


COMPUTER SCIENCE PRACTICAL FILE

cout<<"Enter roll no:";


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()
{
if(f==NULL)
{
cout<<"stack empty";
return;
}
else if(f==r)
{
student *temp=f;
f=r=NULL;
cout<<"node to br deleted is:"<<temp->Stu_no<<" "<<temp->Stu_name;
delete temp;
}
else
{
student *temp=f;
f=f->next;
cout<<"node to br deleted is:"<<temp->Stu_no<<" "<<temp->Stu_name;
delete temp;
}
}
void display()
{
if(f==NULL)
cout<<"Queue empty";
else
{
student*temp=f;
while(temp!=NULL)
{
cout<<
temp->Stu_no<<" "<<temp->Stu_name<<"<-"<<" ";
temp=temp->next;
}
}
}

HARSHIT KAWATRA XII D 86


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 87


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 88


COMPUTER SCIENCE PRACTICAL FILE

Aim:- 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. push(), pop(), display()
These functions are called as per the user’s choice
Code:-
#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();
int choice;
char ans;
do
{
cout<<"1.Push\n2.Pop\n3.display\n0.exit";
cout<<"\nEnter choice:";
cin>>choice;
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 0:exit(0);
}cout<<"\nDo you want to continue";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
void push()
{
student *ptr=new student;
cout<<"Enter rno:";
cin>>ptr->Stu_no;
cout<<"Enter name:";
gets(ptr->Stu_name);
cout<<"Enter percentage:";
cin>>ptr->percentage;

HARSHIT KAWATRA XII D 89


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 90


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 91


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 92


COMPUTER SCIENCE PRACTICAL FILE

Aim:- 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
Code:-
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size=50;
void insert(int CQ[size],int &f,int &r);
void Delete(int CQ[size],int &f,int &r);
void Display(int CQ[size],int &f ,int &r );

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

HARSHIT KAWATRA XII D 93


COMPUTER SCIENCE PRACTICAL FILE

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

HARSHIT KAWATRA XII D 94


COMPUTER SCIENCE PRACTICAL FILE

Output:-

HARSHIT KAWATRA XII D 95


COMPUTER SCIENCE PRACTICAL FILE

HARSHIT KAWATRA XII D 96

You might also like