You are on page 1of 320

COMPUTER

SCIENCE PROGRAM
FILE
Tushar Tandon XII-A 2017-2018 BOARD ROLL NO.:
This is to certify that this program file has been
made by Tushar Tandon of
STD XII A
FOR THE SESSION 2017 - 2018
and has been completed successfully without the use
of any unfair means
under the guidance of his teacher,
MS. NUPUR GUPTA
and is submitted as a part of A.I.S.S.C.E.
(BOARD PRACTICAL EXAMINATION 2017 - 2018)
for STD XII, COMPUTER SCIENCE.

_______________________

(Ms. NUPUR GUPTA)

REMARKS: _____________________________
I would like to express a deep sense of gratitude to my
teacher Ms. Nupur Gupta who has been a support
always, for guiding and helping me immensely to
complete this file.
I am grateful to our lab assistant Mr. Subhash Mishra
for constant and valuable guidance. I would like to
thank our Principal Ma’am, all my other teachers, for
always motivating me.
Also, I would like to thank my classmates who
supported and co-operated with me in this work.
Further, I would like to thank my parents for their
constant motivation and support.

Name: TUSHAR TANDON


Class: XII-A
Board Roll No. : ___________________
Session: 2017-2018
S. No. Topic Page No.

STRUCTURES

Write a program to define a structure HONOUR with the


following data members:
1. Name of the Winners
2. Type of the Award
3. Prize Amount
Now declare an array of structure with N number of 2
1. elements and perform the following using separate functions
for each:
1. Fill the array
2. Show the contents of the array in a tabular form
3. Display all those winners who have earned a prize
amount more than 50,000
4. Display the name and the prize amount of the
biggest prize amount winner

Declare a structure ELECTRIC having the following


members:
Cust_NO, Name, No_of_units consumed and bill. Then the
bill has to be calculated as below:
Units consumed Tariff 7
For the first 100 units Rs. 0.40 per unit
For the next 200 units Rs. 0.50 per unit
2. For the next 300 units Rs. 0.75 per unit
For the next 400 units Rs. 1.00 per unit
Beyond 1000 units Rs. 1.50 per unit
Write a complete C++ program to calculate the bill and
display the information of all the customers using array of
structures in the sorted order of customer number using array
sort. Make use of functions and make the program
interactive and menu driven.
FUNCTION OVERLOADING

13
3. Write a program to illustrate working of function overloading.
Calculate interest amount using function overloading.

19
Write a C++ program that uses an area( ) function for the
4. calculation of area of a triangle or a rectangle or a square.
Number of sides (3 for triangle, 2 for rectangle, 1 for square)
suggest about the shape for which area is to be calculated.

CLASSES AND OBJECTS

Declare a class to represent bank account of 10 customers


with the following data members. Name of the depositor,
Account number, Type of account, (S for Saving and C for
Current), Balance amount. The class also contains member
5.
functions to do the following: 24
i. To initialize data members
ii. To deposit money
iii. To withdraw money after checking
iv. To display the data Members.
The balance (minimum balance in Rs. 1000)

Write a C++ program using classes and objects to simulate


result preparation system for 20 students. The data available
for each student includes rollno, name and marks in 3
subjects. The percentage marks are the average marks and 31
the grade is calculated as follows:
6. Percentage marks Grade
<50 ‘E’
>=50<60 ‘D’
>=60<75 ‘C’
>=75<90 ‘B’
>=90<100 ‘A’
Write a C++ program to perform the various operations on a
string class without using language supported built-in string
functions. The operations to be done on the class are: 35
7. (a) Read a string
(b) Display the string
(c) Reverse the string
(d) Copy the string into an empty string
(e) Concatenate two strings

Write a class to represent a vector (1 – dimensional numeric


array). Include the following member functions :

8. 1. For vector creation 42


2. For modification of a given element
3. For displaying the largest value in the vector
4. For displaying the smallest value in the vector
5. For displaying the entire vector

CONSTRUCTORS AND DESTRUCTORS

Define a class TravelPlan in C++ with the following descriptions:


Private members :
PlanCode Of type long
Place Of type character array (string)
Number_of_travellers Of type integer
Number_of_buses Of type integer

Public members :
 A constructor to assign initial values of PlanCode as 1001,
Place as “Agra”, Number_of_travellers as 5,
9. Number_of_buses as 1 51
 A function NewPlan( ) which allows user to enter
PlanCode, Place and Number_of_travellers. Also, assign
the value of Number_of_buses as per the following
conditions :

Number_of_travellers Number_of_bus
es
Less than 20 1
Equal to or more than 20 and less than 40 2
Equal to 40 or more than 40 3
Define a class Outfit in C++ with the following descriptions :
Private members :

OCode Of type string


OType Of type string
OSize Of type integer
OFabric Of type string
OPrice Of type float

A function InitPrice( ) which calculates and assigns the values of


OPrice as follows :
For the value of OFabric “DENIM”, 56
OType OPrice(Rs)
10. TROUSER 1500
JACKET 2500
For fabric other than “DENIM” the above mentioned OPrice gets
reduced by 25%.

Public members:
 A constructor to assign initial values of OCode, OType and
OFabric with the word “NOT INITIALISED” and OSize and
OPrice with 0.
 A function Input( ) to input the values of the data
members OCode, OType, OSize and OFabric and invoke
the InitPrice( ) function.
 A function Display( ) which displays the content of all the
data members for an Outfit.

INHERITANCE

Write a C++ program to read and display information about


employees and managers. Employee is a class that contains
11. employee number (int), name (char array), address (char array) 63
and department (char array). Manager class contains all
information of the employee class and a list of employees
working under a manager.
DATA FILE HANDLING

Write a complete menu driven program that creates a file


,calculates and displays the total number of vowels in the file
,calculates and displays the total number of spaces in the file ,
calculates and displays the total number of alphabets in the file ,
calculates and displays the total number of words in the file. Make 68
12. use of the following functions and ask the user for his/her choice :

1. createfile( ) 4. spacecount( )
2. displayfile( ) 5. alphacount ( )
3. vowelcheck( ) 6. wordscount( )

Given a Binary file phone.dat , containing records of the


following type:
class Phonelist
{
char Name[20];
char Address[30];
char Areacode[5];
char PhoneNum[15];
public:
void Register( );
void Show( ); 74
13. void Transfer( );
int CheckCode(char AC[])
{
return strcmp(Areacode, AC);
}
};
Write complete menu driven program , complete the definitions
of the functions.Void Register( ) function to input all data and
write the records to the file , void Show( ) to display all data after
reading from the file .Void Transfer( ) to copy those records which
are having Areacode as “DEL” from Phone.dat to Phone_new.dat
.
Given a Binary file APPLY.DAT , containing records of the
following class Applicant type:
Class Applicant
{
Char A_Rno[10];
Char A_Name[30];
Int A_score;
Public:
Void Enrol ( )
{
Gets(A_Rno);
Gets(A_Name);
Cin>>A_Score;
14. }
Void Status( ) 79
{ cout<<setw(12)<<A_Rno;
Cout<<setw(32)<<A_Name;
Cout<<setw(3)<<A_Score<<endl;
}
Int ReturnScore( )
{ return A_Score;
};
Write functions in C++
1.to write the objects of the file on to disk
2. To read objects from the file and display on screen
3. to read and display the details of those students whose
A_Score is > 70.
Develop into a complete menu driven C++ program

Assuming the class Applicant given below, write functions in C++


to perform the following:
class Applicant
{ char A_Rno[10];
char A_Name[20];
int A_Score; };
(i) Write the objects of class to a binary file.
15. (ii) Reads the objects of class from binary file and display 86
them on screen.
(iii) Searches for a particular A_Rno.
(iv) Reads the file and lets the user make changes in data
and invokes the function to display the values.
(v) Just displays contents of the file.
Note : Include all the required function’s prototypes in the
public section of the class.
Write a menu driven program to read, display, append, and
delete records in the file MOVIE.DAT .Use the class given below.
class Movie
{ int movie_id;
char movie_name[20];
int no_of_seats;
16 int ticketcode;
95
int hallno;
public:
void readdata( );
void display( );
void append( );
void delete( );
};

Write a complete program that reads characters from the


keyboard one by one. All lower case characters get stored inside
17. the file LOWER, all upper case characters get stored inside the file 101
UPPER and all other characters get stored inside file OTHERS. Use
functions.

Write a complete file handling program which does the following


on a text file :
(Make the program interactive, menu driven and using functions
and switch case )
1. Create file 2. Display data from file 3. Show digit count 4. 104
18. Show alphabet count 5. Show word count 6. Show line count
7. Show space count 8. Show vowel count 9.
Show count of consonants 10.
Show count of all uppercase characters 11.
Show count of all lowercase characters 12.
Show occurrence of the word "THE" or "the" or 'The" 13.Show
count of all 3 lettered words 14. Exit

Write a complete program in C++ that reads a text file ORIG.TXT


19. and creates another identical file NEW.TXT except for the fact
that every sequence of consecutive blank space is replaced by a 123
single space.
Consider the class given:

class BUS
{
int Bus_No;
char Destination[15];
float Distance;
public:
void READ( );
20.
void WRITE( ); 130
void ENTER ( );
void SHOW( );
void UPDATE ( ); // to ask for a Bus_No and
update the other details
};

Complete the member functions definition of all methods. Use


the binary file "BUS.dat". Ensure that required validations(file
existing) are done before displaying the records, in the main (
).Records should be displayed sequentially from beginning to end.
Also, add any other function , if needed.

Consider the class given:

class CAR
{
int CAR_No;
char Destination[15];
float Distance;
public:
void READ( );
21.
void WRITE( ); 137
void ENTER();
void SHOW( );
void DELETE( ); // to delete records as per user's
choice
};

Complete the definition of all methods .Use the binary file


"CAR.dat" . Ensure that required validations(file existing) are
done before displaying the records, in the main ( ).Records should
be displayed sequentially from beginning to end. Add any
function if needed.
ARRAYS (USING FUNCTIONS)

Write a program to
1. Add two matrices
22. 2. Subtract two matrices
3. Multiply two matrices. 146
Use switch case and use functions to achieve the task.

Write a program to
1. Transpose a matrix
2. Find the row sum
3. Find the column sum
23. 4. Find sum of diagonal elements
5. Find sum of elements below main diagonal 156
6. Find sum of elements above main diagonal of a given
matrix.
Use switch case and use functions to achieve the task.

Write a program to illustrate the two types of searches in an 165


24. integer type array. Make use of functions and make a menu
driven program.

Write a program to illustrate all the three sorts – Insertion, 169


25. Selection and Bubble sort in an array. Make use of functions to do
the same. Make a menu driven and user friendly program.

Write a program to merge two given arrays into the third array C. 175
26. Use switch case and implement the same with atleast 4 different
combinations. Make use of functions.

An array, ARRAY [20], consists of 20 positive integers


.Write a complete user friendly, menu driven functions
oriented C++ program to do the following:
1. Find the pairs of integers stored in A having sum =
50
2. Find the number of elements which are odd 183
27. 3. Find the number of elements which are even
4. Find the number of elements which are divisible by
30
5. Find the largest of all the elements
6. Find the smallest of all the elements
7. Find the average of all the elements
Let A be N X N two-dimensional array. Write user-defined
functions in C++ for the following:
1. To find the number of non-zero elements in A.
28. 2. To find the sum of all elements in A.
3. To find the sum of all elements A[i][j] for which i > j. 190
4. To find the sum of all elements A[i][j] for which i < j.
5. To find the product of all elements A[i][j] for which i = j.

196
29. Write a program to handle arrays and strings using functions.

LINKED LISTS, STACKS & QUEUES

239
Write a program in C++ that creates a complete linked list
30. class.

250
31. Write a program to implement the use of Queue class.

Write a program to implement the use of Stack class. 255


32.

Write a program in C++ to add, delete, search and display an 261


element in a linked queue.
33.

268
Write a complete menu driven program in C++ for the
34.
implementation of linked stack.

276
Write an interactive menu driven C++ program to implement
35.
circular queue using an array consisting of integer data values.
SQL

Write SQL commands for (a) to (f) on the basis of teacher


relation:
(a) To show all information about the teachers of the
history dept.
(b) To list the names of the female teachers who are in 282
the Hindi Dept.
36.
(c) To list all the teachers with their Date of joining in
ascending order.
(d) To display student’s Name, Fee, Age for Male
teachers only.
(e) To count the number of teachers with Age<23.
(f) To insert a new row in the TEACHER table .

Consider the tables WORKERS and DESIG. Write the SQL


commands for the statements (i) to (v):
(i) To display W_ID first name, Address and City Of all
the employees living in New York from the table
workers.
(ii) To display the contents of WORKERS table in
37. ascending order of LASTNAME.
285
(iii) To display the First name, last name and total salary
of all clerks from the tables WORKERS and DESIG
,where Total Salary is calculated as salary + benefits.
(iv) To display the minimum salary among mangers and
clerks from the table DESIG
(v) To display firstname, lastname and their designation
who have their salaries above 60000.

Consider the tables CONSIGNOR and CONSIGNEE. Write SQL


commands for following statements:

1. To display names of all CONSIGNOR from MUMBAI.


2. To display the CneeName, CnorName where consignor 291
38. city is same as consignee city.
3. To display consignee detalils in ascending order of
CneeName.
4. To display distinct city from consignor.
5. To display CneeName and CnorName Where city is
Mumbai.
Study the tables FLIGHTS and FARES and write the SQL
commands for the questions that follow

1. Display FL_NO and NO_FLIGHTS from “KANPUR” to


“BANGALORE” .
39. 2.
3.
Display the total fare collected by INDIAN AIRLINES
Display the FL_NO and FARE to be paid for the flights
297
from DELHI to MUMBAI
4. Display STARTING, ENDING, AIRLINES and FARE from
FLIGHTS and FARES where TAX greater than 5
5. Display FL_NO and DISTINCT AIRLINES from FLIGHTS and
FARES

Signature: _____________________
(Ms. Nupur Gupta)
STRUCTURES

1|Page
/* Write a program to define a structure HONOUR with the following data members:
1. Name of the Winners 2.Type of the Award 3.Prize Amount
Now declare an array of structure with N number of elements and perform the
following using separate functions for each:
1. Fill the array
2. Show the contents of the array in a tabular form
3. Display all those winners who have earned a prize amount more than 50,000
Display the name and the prize amount of the biggest prize amount winner*/

#include<iostream.h> //for cin and cout funtion


#include<conio.h> // for getch and clrscr funtion
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
#include<process.h> // for exit function
struct HONOUR // structure definition
{
char Name[30];
char type[10];
float amount ;
};
void fill(HONOUR H1[],int N) //function
definition
{
for(int i=0;i<N;i++)
{
cout<<"\n Enter the Name of the Winner : "<<endl;
gets(H1[i].Name);
cout<<"\n Enter the type of Award \n \t ( 1: First 2: Second 3: Third 4: Special mention ):
"<<endl;
gets(H1[i].type);
cout<<"\n Enter the Prize amount : "<<endl;
cin>>H1[i].amount;

2|Page
}
}
void show(HONOUR H1[],int N)
{
for(int i=0;i<N;i++)
{
cout<<"\n Name of the Winner : "<<endl;
puts(H1[i].Name);
cout<<"\n Type of Award \n \t ( 1: First 2: Second 3: Third 4: Special mention ): "<<endl;
puts(H1[i].type);
cout<<"\n Prize amount : "<<endl;
cout<<H1[i].amount;
}
}
void search(HONOUR H1[],int N)
{
int found=0;
for(int i=0;i<N;i++)
{
if(H1[i].amount>50000)
{ found=1;
cout<<"\n Name of the Winner : "<<endl;
puts(H1[i].Name);
cout<<"\n Type of Award \n \t ( 1: First 2: Second 3: Third 4: Special mention ): "<<endl;
puts(H1[i].type);
cout<<"\n Prize amount : "<<endl;
cout<<H1[i].amount;
}
}
if(found==0)

3|Page
cout<<"\n NO winner found !";
}
void big(HONOUR H1[],int N)
{
float temp=0.0 ;
int i;
temp=H1[0].amount;
for(i=0;i<N;i++) //biggest element // find the biggest
amount
{
if (temp<=H1[i].amount)
temp=H1[i].amount;
}
for(i=0;i<N;i++)
{
if(H1[i].amount==temp)
{
cout<<"\n Name of the Winner : "<<endl;
puts(H1[i].Name);
cout<<"\n Type of Award \n \t ( 1: First 2: Second 3: Third 4: Special mention ): "<<endl;
puts(H1[i].type);
cout<<"\n Prize amount : "<<endl;
cout<<H1[i].amount;
}
}
}
void main()
{
clrscr();
HONOUR H2[50]; // object created of the given structure

4|Page
int Num,choice;
char ch='y';
cout<<"\n \t Enter the Number of honored members :"<<endl;
cin>>Num;
do
{ // do while loop
cout<<"\n \t Please enter your choice (1 to 5) :";
cout<<"\n 1) Enter the details of winner \n 2) Show details of the winners " ;
cout<<"\n 3) Find winner with prize amount greater than 5000. \n 4) Find winner with the
greatest prize amount " ;
cout<<"\n 5) exit "<<endl;
cin>>choice;
switch(choice)
{case 1 : fill(H2,Num);
break;
case 2 : show(H2,Num);
break;
case 3 : search(H2,Num);
break;
case 4 : big(H2,Num);
break;
case 5 : exit(0);
break;
default: cout<<"\n Wrong choice " ; }
cout<<"\n do you want to continue(Y/N) ";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();

5|Page
OUTPUT:

6|Page
/*Declare a structure ELECTRIC having the following members:
Cust_NO, Name, No_of_unitsconsumed and bill.
Then the bill has to be calculated as below:
Units consumed Tariff
For the first 100 units Rs. 0.40 per unit
For the next 200 units Rs. 0.50 per unit
For the next 300 units Rs. 0.75 per unit
For the next 400 units Rs. 1.00 per unit
Beyond 1000 units Rs. 1.50 per unit
Write a complete C++ program to calculate the bill and display the information of all the
customers using array of structures in the sorted order of customer number using array
sort.
Make use of functions and make the program interactive and menu driven.*/

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
#include<process.h> // for exit function
struct ELECTRIC // class definition
{
int Cust_NO;
char Name[30];
int No_of_unitsconsumed;
float bill ;
};
void fill(ELECTRIC E1[],int N) //function
definition
{
for(int i=0;i<N;i++)
{
cout<<"\n Enter the Customer ID : "<<endl;
cin>>E1[i].Cust_NO;
cout<<"\n Enter the Customer name : "<<endl;
gets(E1[i].Name);

7|Page
cout<<"\n Enter the No. of units consumed : "<<endl;
cin>>E1[i].No_of_unitsconsumed;
// calculation of bill
E1[i].bill=0.0;
if(E1[i].No_of_unitsconsumed<=100)
E1[i].bill=0.40*E1[i].No_of_unitsconsumed;
if((E1[i].No_of_unitsconsumed>100)&&(E1[i].No_of_unitsconsumed<=300))
E1[i].bill=(0.40*100)+ 0.50*(E1[i].No_of_unitsconsumed-100);
if((E1[i].No_of_unitsconsumed>300)&&(E1[i].No_of_unitsconsumed<=600))
E1[i].bill=(0.40*100)+(0.50*200)+0.75*(E1[i].No_of_unitsconsumed-300);
if((E1[i].No_of_unitsconsumed>600)&&(E1[i].No_of_unitsconsumed<=1000))
E1[i].bill=(0.40*100)+(0.50*200)+(0.75*300)+1.00*(E1[i].No_of_unitsconsumed-600);
if(E1[i].No_of_unitsconsumed>1000)
E1[i].bill=(0.40*100)+(0.50*200)+(0.75*300)+(1.00*400)+1.50*(E1[i].No_of_unitscons
umed-1000);
}
}
void show(ELECTRIC E1[],int N)
{
for(int i=0;i<N;i++)
{
cout<<"\n Customer ID : "<<endl;
cout<<E1[i].Cust_NO;
cout<<"\n Customer name : "<<endl;
puts(E1[i].Name);
cout<<"\n No. of units consumed : "<<endl;
cout<<E1[i].No_of_unitsconsumed;
cout<<"\n Total bill : "<<endl;
cout<<E1[i].bill;
}

8|Page
}
void sort(ELECTRIC E1[],int N)
{
int i,j;
ELECTRIC temp;
for( i=0;i<N;i++)
{
for(j=0;j<(N-1)-i;j++)
{
if (E1[j].Cust_NO>E1[j+1].Cust_NO)
{
temp=E1[j];
E1[j]=E1[j+1];
E1[j+1]=temp;
}
}
}
}
void main()
{
clrscr();
ELECTRIC E2[50]; // object created of the given structure
int Num,choice;
char ch='y';
cout<<"\n \t Enter the Number of Customers members :"<<endl;
cin>>Num;
do
{ // do while loop
cout<<"\n \t Please enter your choice (1 to 3) :";

9|Page
cout<<"\n 1) Enter the details of customer \n 2) Show details of the customer \n 3) exit
"<<endl;
cin>>choice;
switch(choice)
{
case 1 : fill(E2,Num);
break;
case 2 : sort(E2,Num);
show(E2,Num);
break;
case 3 : exit(0);
break;
default: cout<<"\n Wrong choice " ;
}
cout<<"\n do you want to continue(Y/N) ";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}

10 | P a g e
OUTPUT:

11 | P a g e
FUNCTION
OVERLOADING

12 | P a g e
/*Write a program to illustrate working of function overloading. Calculate interest amount
using function overloading.*/

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
// example of function overloading
void amount(float principal,int time,float rate) //f1
{
float interest=0.0;
cout<<"\n Principal amount :"<<endl<<principal;
cout<<"\n Time period :"<<endl<<time;
cout<<"\n Rate :"<<endl<<rate;
interest=(principal*time*rate);
cout<<"\n Interest amount:"<<endl<<interest<<endl;
}
void amount(float principal,int time) //f2
{
float interest=0.0;
cout<<"\n Principal amount :"<<endl<<principal;
cout<<"\n Time period :"<<endl<<time;
cout<<"\n Rate : \n 0.08 "<<endl;
interest=(principal*time*0.08);
cout<<"\n Interest amount:"<<endl<<interest<<endl;
}
void amount(float principal,float rate) //f3
{
float interest=0.0;
cout<<"\n Principal amount :"<<endl<<principal;
cout<<"\n Time period : \n 2 "<<endl;
cout<<"\n Rate :"<<endl<<rate;

13 | P a g e
interest=(principal*2*rate);
cout<<"\n Interest amount:"<<endl<<interest<<endl;
}
void amount(int time,float rate) //f4
{
float interest=0.0;
cout<<"\n Principal amount :\n 2000 "<<endl;
cout<<"\n Time period :"<<endl<<time;
cout<<"\n Rate :"<<endl<<rate;
interest=(2000*time*rate);
cout<<"\n Interest amount:"<<endl<<interest<<endl;
}
void amount(float principal) //f5
{
float interest=0.0;
cout<<"\n Principal amount :"<<endl<<principal;
cout<<"\n Time period : \n 2"<<endl;
cout<<"\n Rate :\n 0.08 "<<endl;
interest=(principal*2*0.08);
cout<<"\n Interest amount:"<<endl<<interest<<endl;
}
void main()
{
clrscr();
int choice,time;
float princ,rate;
char ch='n';
cout<<"\n An example to show function overloading by simple interest:"<<endl;
cout<<"\n Enter the principal (default=2000): ";
cin>>princ;

14 | P a g e
cout<<"\n Enter the time period (default=2): ";
cin>>time;
cout<<"\n Enter the rate default=0.08): ";
cin>>rate;
do
{
cout<<"\n Please enter your choice (1-5) to provide argument to function ";
cout<<"\n 1) principal,time,rate \t 2) principal,time";
cout<<"\n 3) principal,rate \t 4)time,rate \t 5)principal :"<<endl;
cin>>choice;
switch(choice)
{
case 1 : amount(princ,time,rate);
break;
case 2 : amount(princ,time);
break;
case 3 : amount(princ,rate);
break;
case 4 : amount(time,rate);
break;
case 5 : amount(princ);
break;
default : cout<<"\n wrong choice ";
}
cout<<"\n Do you want to continue (Y/N) "<<endl;
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}

15 | P a g e
OUTPUT:

16 | P a g e
17 | P a g e
18 | P a g e
/*Write a C++ program that uses an area( ) function for the calculation of area of a triangle
or a rectangle or a square. Number of sides (3 for triangle, 2 for rectangle, 1 for square)
suggest about the shape for which area is to be calculated.*/

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
#include<math.h> // for sqrt function
#include<process.h> // for exit function
void area(float sa,float sb,float sc) //f1
{
float ar=0.0,s=0.0;
cout<<"\n Side 1 :"<<endl<<sa;
cout<<"\n Side 2 :"<<endl<<sb;
cout<<"\n Side 3 :"<<endl<<sc;
s=(sa+sb+sc)*0.5 ;
ar=sqrt((s*(s-sa)*(s-sb)*(s-sc)));
cout<<"\n Area of triangle is :"<<endl<<ar<<endl;
}
void area(float sa,int sb) //f2
{
float ar=0.0;
cout<<"\n Side 1 :"<<endl<<sa;
cout<<"\n Side2 :"<<endl<<sb;
ar=(sa*sb);
cout<<"\n Area of rectangle is :"<<endl<<ar<<endl;
}
void area(float sa) //f3
{
float ar=0.0;
cout<<"\n Side 1:"<<endl<<sa;
ar=(sa*sa);
cout<<"\n Area of square:"<<endl<<ar<<endl;
}
void main()
{
clrscr();
int choice;
float sa,sb,sc;
char ch='n';
19 | P a g e
do
{
cout<<"\n Please enter your choice (1-5) to calculate are of the figure ";
cout<<"\n 1) Area of Triangle (3 sides) \t 2) Area of rectangle (2 sides) \t 3) Area of square ( 1
side) :"<<endl;
cin>>choice;
switch(choice)
{
case 1 : cout<<"\n Enter the Side 1 : ";
cin>>sa;
cout<<"\n Enter the Side 2 : ";
cin>>sb;
cout<<"\n Enter the Side 3 : ";
cin>>sc;
area(sa,sb,sc);
break;
case 2 : cout<<"\n Enter the Side 1 : ";
cin>>sa;
cout<<"\n Enter the Side 2 : ";
cin>>sb;
area(sa,sb);
break;
case 3 : cout<<"\n Enter the Side 1 : ";
cin>>sa;
area(sa,sa);
break;
default : cout<<"\n wrong choice ";
}
cout<<"\n Do you want to continue (Y/N) "<<endl;
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}

20 | P a g e
OUTPUT:

21 | P a g e
22 | P a g e
CLASSES AND
OBJECTS

23 | P a g e
/*Declare a class to represent bank account of 10 customers with the following data
members. Name of the depositor, Account number, Type of account, (S for Saving and C
for Current), Balance amount. The class also contains member functions to do the
following:
(i) To initialize data members (ii) To deposit money
(iii) To withdraw money after checking (iv) to display the data Members.
The balance (minimum balance in Rs. 1000) */

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
#include<process.h> // for exit function
class Bank // class definition
{
char Name[30]; // class members
long AccNo;
char type;
float bamount;
public: //access specifier
Bank() // default constructor
{
strcpy(Name,"Not assigned");
AccNo=0;
type='c';
bamount=0.0;
}
~Bank() // destructor
{ cout<<"\n Memory released "; }
void ind() // inline function definition
{

24 | P a g e
cout<<"\n Enter the Name : "<<endl;
gets(Name);
cout<<"\n Enter the Account no. : "<<endl;
cin>>AccNo;
cout<<"\n Enter the type of account ( 'S' for saving and 'C' for current ) : "<<endl;
cin>>type;
}
void Showd()
{
cout<<"\n Name : "<<endl;
puts(Name);
cout<<"\n AccNo : "<<endl<<AccNo;
cout<<"\n Type of account : "<<endl<<type ;
cout<<"\n Current Balance amount : "<<endl<<bamount;
}
void deposit()
{
float amount=0.0;
cout<<"\n Enter Amount to be deposit for "<<"\t"<<AccNo<<endl;
cin>>amount;
bamount+=amount;
Showd();
}
void withdraw()
{
float amount=0.0;
cout<<"\n Enter amount to be withdrawn :"<<endl;
cin>>amount ;
if (bamount-amount<1000)
cout<<"\n Sorry minimum rs. 1000 requires in balance "<<endl;

25 | P a g e
else
bamount-=amount;
Showd();
}
};
void main()
{
clrscr();
Bank B[10]; // array of objet created of the given class
char ch='y';
int choice,N,i;
cout<<"\n Enter the no. of bank customers (less than 10) "<<endl;
cin>>N;
do
{ // do while loop
cout<<"\n Please choose choice (1 to 5)"<<endl;
cout<<"\n 1) Enter the customer data \n 2) Display the customer data \n 3) Deposit money \n
4) Withdraw money \n 5) exit :"<<endl;
cin>>choice;
for(i=0;i<N;i++)
{
switch(choice)
{
case 1 : B[i].ind();
break;
case 2 : B[i].Showd();
break;
case 3 : B[i].deposit();
break;
case 4 : B[i].withdraw();

26 | P a g e
break;
case 5 : exit(0);
break;
default : cout<<"\n wrong choice ";
}
}
cout<<"\n Do you want to continue (Y/N) "<<endl;
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}

27 | P a g e
OUTPUT:

28 | P a g e
29 | P a g e
30 | P a g e
/*Write a C++ program using classes and objects to simulate result preparation system for
20 students. The data available for each student includes rollno, name and marks in 3
subjects. The percentage marks are the average marks and the grade is calculated as
follows:
Percentage marks grade
<50 ‘E’
>=50<60 ‘D’
>=60<75 ‘C’
>=75<90 ‘B’
>=90<100 ‘A’ */

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
class student // class definition
{
int rollno;
char name[20];
float marks[3];
float percent;
char grade;
public:
student() // default constructor
{
rollno=0;
strcpy(name,"not assigned");
marks[0,1,2]=(0,0,0);
percent=0.0;
grade='n';
}
~student() // destructor
{ cout<<"\n memory released ";}
void enter();
void show();
void calculate();
};
void student::enter()
{

31 | P a g e
float total=0.0;
cout<<"\n Enter the rollno of the student"<<endl;
cin>>rollno;
cout<<"\n Enter the name of the student "<<endl;
gets(name);
for(int i=0;i<3;i++)
{
cout<<"\n Enter the marks for subject (out of 25 ) : "<<i+1<<endl;
cin>>marks[i];
total+=marks[i];
}
// calculation for percentage
percent=(total/(3*25))*100;
}
void student::show()
{
cout<<" \n Student data :"<<endl;
cout<<" \n Roll no : "<<rollno<<endl;
cout<<" \n Name of the student: "<<endl;
puts(name);
for(int j=0;j<3;j++)
{
cout<<" \n Marks of the subject = "<<j+1<<"\t";
cout<<marks[j]<<endl;
}
cout<<"\n Percentage of marks : "<<percent;
calculate();
cout<<"\n Grade of the Student is "<<grade;
}
void student::calculate()
{
if(percent<50)
grade='E';
if((percent>=50)&&(percent<60))
grade='D';
if((percent>=60)&&(percent<75))
grade='C';
if((percent>=75)&&(percent<90))
grade='B';
32 | P a g e
if((percent>=90)&&(percent<100))
grade='A';
}
void main()
{
clrscr();
student s1[20]; // array of objects of class created
int N;
char ch='y';
clrscr();
cout<<"\n Enter the no. of students "<<endl;
cin>>N;
do
{
for( int i=0;i<N;i++)
{
s1[i].enter();
s1[i].show();
}
cout<<"\n Do you want to continue(Y/N) ";
cin>>ch;
}while((ch=='Y')||(ch=='y'));
getch();
}

33 | P a g e
OUTPUT:

34 | P a g e
/* Write a C++ program to perform the various operations on a string class without using
language supported built-in string functions. The operations to be done on the class are:
(f) Read a string
(g) Display the string
(h) Reverse the string
(i) Copy the string into an empty string
(j) Concatenate two strings */

#include<iostream.h>
#include<conio.h>
class String
{
char str[50];
public:
void Read();
void Display();
void Reverse();
void Copy();
void Concatenate();
}s;
void String::Read()
{
cout<<"\n Enter a string ";
gets(str);
}
void String::Display()
{
cout<<"\n Enter a string ";
gets(str);
cout<<"\n The entered string is: ";
for(int i=0;str[i]!='\0';i++)
{
cout<<str[i];
35 | P a g e
}
}
void String::Reverse()
{
cout<<"\n Enter a string ";
gets(str);
int i;
cout<<"\n Reversed string:";
for(i=0;str[i]!='\0';i++)
{ }
for(i ;i>=0; i--)
{
cout<<str[i];
}
}
void String::Copy()
{
char ptr[50];
int i;
cout<<"\n Enter a string ";
gets(str);
for(i=0;str[i]!='\0';i++)
{
ptr[i]=str[i];
}
ptr[i]=NULL;
cout<<"\n Copied string = "<<ptr;
}
void String::Concatenate()
{

36 | P a g e
char ptr[50];
int j=0, i=0;
cout<<"\n Enter first string ";
gets(str);
cout<<"\n Enter second string ";
gets(ptr);
for(i=0;str[i]!='\0';i++);
for(j=0;ptr[j]!='\0';j++)
{
str[i++]=ptr[j];
}
cout<<"\n concatenated string: "<<str;
}
void main()
{
int n;
char ch;

while(1)
{
cout<<"\n String menu";
cout<<"\n 1. Read a string";
cout<<"\n 2. Display a string";
cout<<"\n 3. Reverse a string";
cout<<"\n 4. Copy a string";
cout<<"\n 5. Concatenate two strings";
cout<<”\n 6. Exit”;
cout<<"\n Enter our choice(1-5) ";
cin>>n;
switch(n)

37 | P a g e
{
case 1 : s.Read();
break;
case 2 : s.Display();
break;
case 3 : s.Reverse();
break;
case 4 : s.Copy();
break;
case 5 : s.Concatenate();
break;
case 6 : exit(0);
default : cout<<"\n INVALID OPTION!!";
break;
}
}
getch();
}

38 | P a g e
OUTPUT:

39 | P a g e
40 | P a g e
41 | P a g e
/*Write a class to represent a vector (1 – dimensional numeric array) . Include the
following member functions :
1. For vector creation
2. For modification of a given element
3. For displaying the largest value in the vector
4. For displaying the smallest value in the vector
5. For displaying the entire vector*/

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Vector
{
int vec[100], n;
public:
void Create();
void Modify();
void Large();
void Small();
void Display();
}v;
void Vector::Create()
{
cout<<"\n Enter to size of the vector (max 100) : ";
cin>>n;
cout<<"\n enter the elements: ";
for(int i=0;i<n;i++)
cin>>vec[i];
cout<<"\n vector created!!";
}
void Vector::Modify()
{

42 | P a g e
int x, p, i;
cout<<"\n Enter te size of the vector (max 100) : ";
cin>>n;
cout<<"\n enter the elements: ";
for(i=0;i<n;i++)
cin>>vec[i];
cout<<"\n Enter the value which you want to replace : ";
cin>>x;
cout<<"\n Enter the new value : ";
cin>>p;
for(i=0;i<n;i++)
{
if(vec[i]==x)
{
vec[i]=p;
}
}
cout<<"\n Modified Array ";
for(i=0;i<n;i++)
{
cout<<" "<<vec[i];
}
}
void Vector::Large()
{
int max=0, i;
cout<<"\n Enter te size of the vector (max 100) : ";
cin>>n;
cout<<"\n enter the elements: ";
for(i=0;i<n;i++)

43 | P a g e
{
cin>>vec[i];
}
max=vec[0];
for(i=0;i<n;i++)
{
if(max<vec[i])
max=vec[i];
}
cout<<"\n Largest element"<<max;
}
void Vector::Small()
{
int min=0, i;
cout<<"\n Enter te size of the vector (max 100) : ";
cin>>n;
cout<<"\n enter the elements: ";
for(i=0;i<n;i++)
{
cin>>vec[i];
}
min=vec[0];
for(i=0;i<n;i++)
{
if(min>vec[i])
min=vec[i];
}
cout<<"\n Smallest element"<<min;
}
void Vector::Display()

44 | P a g e
{
int i;
cout<<"\n Enter te size of the vector (max 100) : ";
cin>>n;
cout<<"\n enter the elements: ";
for(i=0;i<n;i++)
{
cin>>vec[i];
}
cout<<"\n vector: ";
for(i=0;i<n;i++)
{
cout<<vec[i]<<"\t";
}
}
void main()
{
clrscr();
int z;
char ch;
cout<<"\n Vector menu";
cout<<"\n 1. Create a vector";
cout<<"\n 2. Modify an element of vector";
cout<<"\n 3. Largest element of the vector";
cout<<"\n 4. Smallest element of the vector";
cout<<"\n 5. Display a vector";
cout<<"\n 6. Exit";
while(1)
{
cout<<"\n Enter your choice : ";

45 | P a g e
cin>>z;
switch(z)
{
case 1 : v.Create();
break;
case 2 : v.Modify();
break;
case 3 : v.Large();
break;
case 4 : v.Small();
break;
case 5 : v.Display();
break;
case 6: exit(0);
break;
default : cout<<"\n INVALID CHOICE!!!";
}
}
getch();
}

46 | P a g e
OUTPUT:

47 | P a g e
48 | P a g e
49 | P a g e
CONSTRUCTORS
AND
DESTRUCTORS

50 | P a g e
/*Define a class TravelPlan in C++ with the following descriptions:
Private members :
PlanCode Of type long
Place Of type character array (string)
Number_of_travellers Of type integer
Number_of_buses Of type integer

Public members :
 A constructor to assign initial values of PlanCode as 1001, Place as “Agra”,
Number_of_travellers as 5, Number_of_buses as 1
 A function NewPlan( ) which allows user to enter PlanCode, Place and
Number_of_travellers. Also, assign the value of Number_of_buses as per the
following conditions :

Number_of_travellers Number_of_buses
Less than 20 1
Equal to or more than 20 2
and less than 40
Equal to 40 or more than 3
40

*/

#include<iostream.h> //for cin and cout funtion


#include<conio.h> // for getch and clrscr funtion
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
class TravelPlan // class defination
{
long PlanCode; // class members
char Place[30];
int Number_of_travellers;

51 | P a g e
int Number_of_buses;
public: //
TravelPlan() // default contructor
{
PlanCode=1001;
strcpy(Place,"Agra");
Number_of_travellers=5;
Number_of_buses=1;
}
~TravelPlan() // destructor
{ cout<<"\n Memory realsed "; }
void NewPlan() // inline fuction definition
{
cout<<"\n Enter the Travel code : "<<endl;
cin>>PlanCode;
cout<<"\n Enter the destination : "<<endl;
gets(Place);
cout<<"\n Enter the Number of travellers : "<<endl;
cin>>Number_of_travellers;
// condition for no. of buses
if(Number_of_travellers<20)
Number_of_buses=1;
else if(Number_of_travellers<40)
Number_of_buses=2;
else
Number_of_buses=3;
}
void ShowPlan()
{
cout<<"\n Plan Code: "<<endl<<PlanCode ;

52 | P a g e
cout<<"\n Place: "<<endl;
puts(Place);
cout<<"\n Number of travellers: "<<endl<<Number_of_travellers ;
cout<<"\n Number of buses: "<<endl<<Number_of_buses<<endl;
}
};
void main()
{
clrscr();
TravelPlan T1; // objet created of the given class
char ch='y';
do
{ // do while loop
T1.NewPlan();
T1.ShowPlan();
cout<<"\n do you want to continue(Y/N) ";
cin>>ch;
}while(ch=='Y');
getch();
}

53 | P a g e
OUTPUT:

54 | P a g e
55 | P a g e
/*Define a class Outfit in C++ with the following descriptions :
Private members :

OCode Of type string


OType Of type string
OSize Of type integer
OFabric Of type string
OPrice Of type float

A function InitPrice( ) which calculates and assigns the values of OPrice as follows :
For the value of OFabric “DENIM”,
OType OPrice(Rs)
TROUSER 1500
JACKET 2500
For fabric other than “DENIM” the above mentioned OPrice gets reduced by 25%.

Public members:
 A constructor to assign initial values of OCode, OType and OFabric with the word
“NOT INITIALISED” and OSize and OPrice with 0.
 A function Input( ) to input the values of the data members OCode, OType, OSize
and OFabric and invoke the InitPrice( ) function.
 A function Display( ) which displays the content of all the data members for an
Outfit. */

#include<iostream.h> //for cin and cout function


#include<conio.h> // for getch and clrscr function
#include<string.h> // for strcpy()
#include<stdio.h> // for puts() and gets()
class Outfit // class definition
{
char OCode[20];
char OType[20];

56 | P a g e
int OSize;
char OFabric[20];
float OPrice;
float InitPrice() ; // function prototype
public: // access specifier
void input() // inline function definition
{
cout<<"\n \t Enter the code of dress (FORMAL/INFORMAL) "<<endl;
gets(OCode);
cout<<"\n \t Enter the type of dress (TROUSER/JACKET) "<<endl;
gets(OType);
cout<<"\n \t Enter the size of the dress : "<<endl;
cin>>OSize;
cout<<"\n \t Enter the fabric (DENIM/NAYLON) "<<endl;
gets(OFabric);
OPrice=InitPrice(); //function call
}
void display() // inline function definition
{
cout<<"\n \t The code of the dress is : ";
puts(OCode);
cout<<"\n \t The type of the dress is : ";
puts(OType);
cout<<"\n \t The size of the dress is : "<<OSize<<endl;
cout<<"\n \t The fabric of the dress is : ";
puts(OFabric);
cout<<"\n \t The price of the dress is : "<<OPrice;
}
Outfit() // default constructor
{

57 | P a g e
strcpy(OCode,"Not INITIALISED");
strcpy(OType,"Not INITILISED");
OSize=0;
strcpy(OFabric,"Not INITILISED");
OPrice=0.0;
} // destructor
~Outfit()
{ cout<<"\n memory released"; }
};
float Outfit::InitPrice() // function definition
{ float num=0.0;
if(strcmp(OType,"TROUSER")==0)
{
if(strcmp(OFabric,"DENIM")==0)
num=1500.0;
else
num=2500.0;
}
if(strcmp(OType,"JACKET")==0)
{
if(strcmp(OFabric,"DENIM")==0)
num=1500.0-(1500.0*0.25);
else
num=2500.0-(2500.0*0.25);
}
return(num);
}
void main()
{
clrscr();

58 | P a g e
Outfit o1; // objet created of the given class
char ch='y';
do
{ // do while loop
o1.input();
o1.display();
cout<<"\n do you want to continue(Y/N) ";
cin>>ch;
}while(ch=='Y');
getch();
}

59 | P a g e
OUTPUT:

60 | P a g e
61 | P a g e
INHERITANCE

62 | P a g e
/* Write a C++ program to read and display information about employees and managers.
Employee is a class that contains employee number (int), name (char array), address (char
array) and department (char array). Manager class contains all information of the
employee class and a list of employees working under a manager.*/

#include<iostream.h> //for input & output operations


#include<conio.h> //for clrscr and getch functions
#include<stdio.h> //for gets & puts functions
void func(); //a function declared
class Employee { //a class Employee is defined
protected:
int eno;
char ename[30];
char eaddress[30];
char edepartment[30];
public:
void enter();
void show();
};
void Employee::enter() { //function definition for entries
cout<<"\nEmployee number: ";
cin>>eno;
cout<<"\nName :";
gets(ename);
cout<<"\nAddress : ";
gets(eaddress);
cout<<"\nDepartment: ";
gets(edepartment);
}
void Employee::show() { //function definition for display
cout<<"\nEmployee Number: ";

63 | P a g e
cout<<eno;
cout<<"\nName: ";
puts(ename);
cout<<"\nAddress: ";
puts(eaddress);
cout<<"\nDepartment: ";
puts(edepartment);
}
class Manager : public Employee
{ //a class Manager is defined
Employee em[50];
public:
int no_em;
void enter();
void show();
};
void Manager::enter() {
cout<<"\n\n\t <Enter Manager Details>";
Employee::enter();
cout<<"\nEnter the no of Employees under "<<ename<<"\n";
cin>>no_em;
cout<<"\nEnter the Details of Employees working under "<<ename<<"\n";
for(int i=0; i<no_em; i++)
{
cout<<endl;
em[i].Employee::enter();
}
}
void Manager::show() { //function definition for display
Employee::show();

64 | P a g e
cout<<"\nThe Employees working under "<<ename<<" are \n";
for(int i=0; i<no_em; i++) {
cout<<endl;
em[i].show();
}
}
void main() { //main function
clrscr();
int i;
char ch;
cout<<"\nHow many managers do you want: ";
cin>>i;
Manager M[5];
for(int j=0;j<i;j++)
{ M[j].enter();
//function called
}
cout<<"\n\nOUTPUT\n";
for(int k=0;k<i;k++)
{
M[k].show();
//function called
}
getch();
}

65 | P a g e
OUTPUT:

66 | P a g e
DATA FILE
HANDLING

67 | P a g e
/*Write a complete menu driven program that creates a file ,calculates and displays the
total number of vowels in the file ,calculates and displays the total number of spaces in the
file , calculates and displays the total number of alphabets in the file , calculates and
displays the total number of words in the file. Make use of the following functions and ask
the user for his/her choice :
1. createfile( ) 2. displayfile( ) 3. vowelcheck( )
4. spacecount( ) 5. alphacount ( ) 6. wordscount( )*/

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void vowel(); //functions for counting vowels,alphabets,spaces and words
void alpha();
void space();
void word();
void main()
{
clrscr();
char ch,abc[50];
int a;
ifstream fin;
ofstream fout;
fout.open("Text.txt");
cout<<"\n Enter the text:";
gets(abc);
fout<<abc;
fout.close();
do
{
68 | P a g e
cout<<"\n MENU ";
cout<<"\n 1) Vowel check";
cout<<"\n 2) Space count";
cout<<"\n 3) Alpha count";
cout<<"\n 4) Word count";
cout<<"\n Enter choice:\n ";
cin>>a;
switch(a)
{
case 1: vowel();
break;
case 2: space();
break;
case 3: alpha();
break;
case 4: word();
break;
default: cout<<" Wrong choice";
break;
}
cout<<"\n Do you want to continue(y/n):\n ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
void vowel() //Function for counting vowels
{ char ch;int c=0;
ifstream fin;
fin.open("Text.txt");
while(!fin.eof())

69 | P a g e
{fin>>ch;
if(ch=='a'||ch=='e'||ch=='o'||ch=='u'||ch=='i')
c++;
}
cout<<"\n Vowel count: "<<c;
}
void space() //Function for counting spaces
{
char ch;
int c=0;
ifstream fin;
fin.open("Text.txt");
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
c++;
}
cout<<"\n Space count: "<<c;
}
void alpha() //Function for counting alphabets
{ char ch;
int c=0;
ifstream fin;
fin.open("Text.txt");
while(!fin.eof())
{ fin.get(ch);
if((isalpha(ch))&&(ch!=' ')) //To not count spaces
c++;
}

70 | P a g e
cout<<"\n Alphabet count: "<<c;
}

void word() //Function for counting words


{ char ch[100];
int c=0;
ifstream fin;
fin.open("Text.txt");
while(!fin.eof())
{ fin.getline(ch,100,' ');
c++;
}
cout<<"\n Word count: "<<c;
}

71 | P a g e
OUTPUT:

72 | P a g e
73 | P a g e
/*Given a Binary file phone.dat , containing records of the following type:
class Phonelist
{
char Name[20];
char Address[30];
char Areacode[5];
char PhoneNum[15];
public:
void Register( );
void Show( );
void Transfer( );
int CheckCode(char AC[])
{
return strcmp(Areacode, AC);
}
};
Write complete menu driven program , complete the definitions of the functions.Void
Register( ) function to input all data and write the records to the file , void Show( ) to
display all data after reading from the file .Void Transfer( ) to copy those records which
are having Areacode as “DEL” from Phone.dat to Phone_new.dat .*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
class Phonelist //Class definition
{
char Name[20];
char Address[30];
char Areacode[5];
char PhoneNum[15];
public:
void Register();
void Show();
void Transfer();

74 | P a g e
int checkcode(char AC[])
{
return strcmp ( Areacode,AC);
}
};
void Phonelist::Register()
{
cout<<"\n Enter the name : " ;
gets(Name);
cout<<"\n Enter the Address : " ;
gets(Address);
cout<<"\n Enter the areacode : " ;
gets(Areacode);
cout<<"\n Enter the Phone Number : " ;
gets(PhoneNum);
}
void Phonelist::Show()
{
cout<<"\n The Name is : ";
puts(Name);
cout<<"\n The Address is : " ;
puts(Address);
cout<<"\n The Areacode is : " ;
puts(Areacode);
cout<<"\n The Phone Number is : " ;
puts(PhoneNum);
}
void main()
{
clrscr();

75 | P a g e
Phonelist P;
char ch='y';
ofstream fout;
fout.open("Phone.dat",ios::binary|ios::app);
do
{
cout<<"\n Enter Data : " ;
P.Register();
fout.write((char*)&P,sizeof (P));
cout<<"\n Do you want to continue?(y/n): " ;
cin>>ch;
}while((ch=='y')||(ch=='Y'));
fout.close();

ifstream fin;
fin.open("Phone.dat",ios::binary);
if(!fin)
{ cout<<"\n File does not exist : " ;
exit(-1);
}
while(fin.read((char*)&P,sizeof (P)))
{
P.Show();
cout<<"\n\n";
}
fin.close();
cout<<"\n The records which are having the areacode as 'DEL' have been copied. " ;
P.Transfer();
getch();
}

76 | P a g e
void Phonelist::Transfer()
{
ifstream fin;
ofstream fout ;
Phonelist P;
fin.open("Phone.dat",ios::binary);
fout.open("Phone_new.dat",ios::binary);
while(fin.read((char*)&P,sizeof(P)))
{ if(P.checkcode("DEL")==0)
fout.write((char*)&P,sizeof(P));
}
fin.close();
fout.close();
}

77 | P a g e
OUTPUT:

78 | P a g e
/*Given a Binary file APPLY.DAT, containing records of the following class Applicant
type:
Class Applicant
{
Char A_Rno[10];
Char A_Name[30];
Int A_score;
Public:
Void Enrol ( )
{
Gets(A_Rno);
Gets(A_Name);
Cin>>A_Score;
}
Void Status( )
{ cout<<setw(12)<<A_Rno;
Cout<<setw(32)<<A_Name;
Cout<<setw(3)<<A_Score<<endl;
}
Int ReturnScore( )
{ return A_Score;
};
Write functions in C++
1.to write the objects of the file on to disk
2. To read objects from the file and display on screen
3. to read and display the details of those students whose A_Score is > 70.
Develop into a complete menu driven C++ program*/

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void writedata(); //Function prototypes
void score();
void display();
class applicant //Class definition
{
char a_rno[10];
char a_name[30];

79 | P a g e
int a_score;
public:
void enrol()
{
cout<<"\n Roll no: ";
gets(a_rno);
cout<<"\n Name: ";
gets(a_name);
cout<<"\n Score: ";
cin>>a_score;
}
void status()
{
cout<<"\n Roll no: "<<a_rno<<"\n";
cout<<"\n Name: "<<a_name<<"\n";
cout<<"\n Score: "<<a_score<<"\n";
}
int returnscore()
{
return a_score;
}
};
void main()
{
clrscr();
applicant a1;
char ch,choice;
do
{
cout<<"\n\tMenu";

80 | P a g e
cout<<"\n 1.Enter data ";
cout<<"\n 2.Score more than 70 ";
cout<<"\n 3.Dislay all objects on file\n";
cin>>ch;
switch(ch)
{
case '1': writedata();
break;
case '2': score();
break;
case '3': display();
break;
default:cout<<"\nERROR";
}
cout<<"\n Do you want to continue(y/n): ";
cin>>choice;
}while((choice=='y')||(choice=='Y'));
getch();
}
void score() //Displays objects with score more than 70
{ clrscr();
applicant a2;
ifstream fin;
fin.open("applicant.dat",ios::binary);
cout<<"\nApplicants with score greater than 70 are:\n\n";
while(!fin.eof())
{ fin.read((char*)&a2,sizeof(a2));
if(a2.returnscore()>70)
a2.status();
cout<<"\n\n";

81 | P a g e
}
fin.close();
}
void display() //Displays all objects
{
clrscr();
applicant a3;
ifstream fin;
fin.open("applicant.dat",ios::binary);
if(!fin)
{
cout<<"\nFile not found";
exit(-1);
}
while(fin.read((char*)&a3,sizeof(a3)))
{
cout<<"\n";
a3.status();
}
}
void writedata() //Writes objects on file
{
clrscr();
ofstream fout;
fout.open("applicant.dat",ios::binary);
applicant a1;
char choice;
do{
cout<<"\n Enter data ";
a1.enrol();

82 | P a g e
fout.write((char*)&a1,sizeof(a1));
cout<<"\n Do you wish to enter more(y/n): ";
cin>>choice;
}while((choice=='y')||(choice=='Y'));
fout.close();
}

83 | P a g e
OUTPUT:

84 | P a g e
85 | P a g e
/*Assuming the class Applicant given below, write functions in C++ to perform the
following:
class Applicant
{ char A_Rno[10];
char A_Name[20];
int A_Score; };
(vi) Write the objects of class to a binary file.
(vii) Reads the objects of class from binary file and display them on screen.
(viii) Searches for a particular A_Rno.
(ix) Reads the file and lets the user make changes in data and invokes the function to
display the values.
(x) Just displays contents of the file.
Note : Include all the required function’s prototypes in the public section of the class.*/

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class applicant //Class definition
{
char a_rno[10];
char a_name[20];
int a_score;
public:
void getdata() //Console input
{
cout<<"\n Roll number : ";
gets(a_rno);
cout<<"\n Name : " ;
gets(a_name);
cout<<"\n Score : " ;
cin>>a_score;
}

86 | P a g e
void display() //Console output
{
cout<<"\n The roll number is : " ;
puts(a_rno);
cout<<"\n The name of the student is : " ;
puts (a_name);
cout<<"\n The score of the student is : " << a_score;
}
char* getrno()
{
return a_rno;
}
void writedata(); //File output
void readdata(); //File input
void search(); //File search
void modify() ; //File modify
void dispfile(); //Displays all contents of file
};
void main()
{
clrscr();
int ch;
char choice;
applicant a;
do{
cout<<"\n\t\tMENU"<<endl;
cout<<"\n 1.Write objects of class to file ";
cout<<"\n 2.Display objects from file on screen ";
cout<<"\n 3.Search for a particular roll no. ";
cout<<"\n 4.Modify data ";

87 | P a g e
cout<<"\n 5.Display contents of file ";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: a.writedata();
break;
case 2: a.readdata();
break;
case 3: a.search();
break;
case 4: a.modify();
break;
case 5: a.readdata();
break;
default:cout<<"\n Error";
}
cout<<"\n Do you wish to continue(y/n): " ;
cin>>choice;
clrscr();
}while((choice=='y')||(choice=='Y'));
getch();
}
void applicant::writedata() //File output
{
ofstream fout;
fout.open("app.dat",ios::binary|ios::app);
applicant a1;
char choice;
do{

88 | P a g e
cout<<"\n Enter data: ";
a1.getdata();
fout.write((char*)&a1,sizeof(a1));
cout<<"\n Do you wish to enter more(y/n): ";
cin>>choice;
}while((choice=='y')||(choice=='Y'));
fout.close();
}
void applicant::readdata() //File input
{ ifstream fin;
fin.open("app.dat",ios::binary);
if(!fin)
{
cout<<"\n File not found";
exit(-1);
}
applicant a1;
cout<<"\n Applicant data is:";
while(fin.read((char*)&a1,sizeof(a1)))
{
cout<<"\n\n";
a1.display();
}
cout<<"\n";
fin.close();
}
void applicant::search() //To search for a record
{ ifstream fin;
fin.open("app.dat",ios::binary);
if(!fin)

89 | P a g e
{
cout<<"\n File not found";
exit(-1);
}
applicant a1;
char rno[10];
int flag=0;
cout<<"\n Enter the roll no. to be searched for: ";
gets(rno);
while(fin.read((char*)&a1,sizeof(a1)))
{ if(strcmp(a1.getrno(),rno)==0)
{ flag=1;
cout<<"\n Roll no. found\n\n";
a1.display();
}
}
fin.close();
cout<<"\n";
if( flag==0)
cout<<"\n Roll no. not found";
}
void applicant::modify() //To modify record
{
char ch='n';
applicant s1;
char s_rno[10];
int flag=0;
fstream fio;
fio.open("app.dat",ios::binary|ios::in|ios::out|ios::ate);
fio.seekg(0);

90 | P a g e
cout<<"\n Enter the roll number you want to modify: ";
gets(s_rno);
while(!fio.eof())
{fio.read((char*)&s1,sizeof(s1));
if(strcmpi(s1.getrno(),s_rno)==0)
{
flag=1;
cout<<" Are you sure you want to modify this applicant's data?\n ";
s1.display();
cout<<"\n ";
cin>>ch;
if((ch=='y')||(ch=='Y'))
{cout<<" Enter new data:\n"<<endl;
s1.getdata();
fio.seekp(fio.tellp()-sizeof(s1));
fio.write((char*)&s1,sizeof(s1));
break;
}
}
}
if(flag==0)
cout<<"\n Data not Found"<<endl;
fio.close();
}

91 | P a g e
OUTPUT:

92 | P a g e
93 | P a g e
94 | P a g e
/*Write a menu driven program to read, display, append, and delete records in the file
MOVIE.DAT .Use the class given below.
class Movie
{ int movie_id;
char movie_name[20];
int no_of_seats;
int ticketcode;
int hallno;
public:
void readdata( );
void display( );
void append( );
void delete( );
};*/

#include<fstream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
class Movie //class definition
{ int movie_id;
char movie_name[20];
int no_of_seats;
int ticketcode;
int hallno;
public :
void readdata()
{ cout<<"\n Enter Movie Id : ";
cin>>movie_id;
cout<<"\n Ennter Movie name : ";
gets(movie_name);
cout<<"\n Enter No. of seats : ";
cin>>no_of_seats;
cout<<"\n Enter Ticket code : ";
cin>>ticketcode;

95 | P a g e
cout<<"\n Hall Number : ";
cin>>hallno;
}
void display()
{ cout<<"\n Enter Movie Id : "<<movie_id;
cout<<"\n Ennter Movie name : "<<movie_name;
cout<<"\n Enter No. of seats : "<<no_of_seats;
cout<<"\n Enter Ticket code : "<<ticketcode;
cout<<"\n Hall Number : "<<hallno;
}
void append();
void Delete();
int retid()
{ return movie_id ; }
};
void Movie :: append()
{char ch;
Movie M;
ofstream fout;
fout.open("MOVIE.DAT",ios::binary|ios::app) ;
do {
M.readdata();
fout.write((char*)&M,sizeof(M));
cout<<"\n File Appended ! ";
cout<<"\n Do you want to append more : ";
cin>>ch;
} while ((ch=='y')||(ch=='Y')) ;
fout.close();
}
void Movie :: Delete()

96 | P a g e
{ Movie M;
char confirm='n',ch;
int ID,flag=0;
ifstream fin;
fin.open("MOVIE.DAT",ios::binary);
if (!fin)
{cout<<"\n File does not exist ";
exit(-1); }
ofstream fout;
fout.open("TEMP.DAT",ios::binary);
cout<<"\n Enter the Movie ID : ";
cin>>ID;
while (fin.read((char*)&M,sizeof(M)))
{ if (M.retid()==ID)
{ M.display();
flag=1;
cout<<"\n Are you sure you want to delete : ";
cin>>confirm;
if (confirm == 'n')
{ fout.write((char*)&M,sizeof(M));
break ; }
}
else
{ fout.write((char*)&M,sizeof(M));
break ; }
}
if(flag==0)
cout<<"\n Movie record not found.\n";
fin.close();
fout.close();

97 | P a g e
remove("MOVIE.DAT");
rename("TEMP.DAT","MOVIE.DAT");
}
void main()
{
clrscr();
Movie M;
char ch,choice;
do {
cout<<"\n Movie Menu : "<<endl;
cout<<" 1. Append "<<endl;
cout<<" 2. Delete "<<endl<<" ";
cin>>choice;
switch(choice)
{
case '1' : M.append();
break;
case '2' : M.Delete();
break;
default : cout<<"\n Wrong Choice !! ";
}
cout<<"\n Do you want to continue : ";
cin>>ch;
} while ((ch=='y')||(ch=='Y')) ;
getch();
}

98 | P a g e
OUTPUT:

99 | P a g e
100 | P a g e
/*Write a complete program that reads characters from the keyboard one by one. All lower
case characters get stored inside the file LOWER, all upper case characters get stored
inside the file UPPER and all other characters get stored inside file OTHERS. Use
functions.*/

#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{ clrscr();
char ch,choice='y';
ofstream fout1,fout2,fout3;
fout1.open("UPPER.txt");
fout2.open("LOWER.txt");
fout3.open("OTHER.txt");
while((choice=='y')||(choice=='Y'))
{
cout<<"\n Enter character to be stored in file: ";
cin>>ch;
if(isupper(ch))
{
fout1<<ch;
}
else if(islower(ch))
{
fout2<<ch;
}
else
{

101 | P a g e
fout3<<ch;
}
cout<<"\n Do you want to continue('y'/'n'): ";
cin>>choice;
}
fout1.close();
fout2.close();
fout3.close();
getch();
}

102 | P a g e
OUTPUT:

103 | P a g e
/*Write a complete file handling program which does the following on a text file :
(Make the program interactive, menu driven and using functions and switch case )
1. Create file 2. Display data from file 3. Show digit count 4. show alphabet count 5.
show word count 6. show line count 7. show space count 8. show vowel count 9. show
count of consonants 10. show count of all uppercase characters 11. show count of all
lowercase characters 12. show occurrence of the word "THE" or "the" or 'The" 13.show
count of all 3 lettered words 14. Exit */

#include<fstream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
void create() //Function to create or append file
{
char ch,str[50];
ofstream fout;
cout<<"\nDo you want to continue with old file(O) or new file(N): ";
cin>>ch;
if((ch=='N')||(ch=='n'))
fout.open("Text.txt");
else
fout.open("Text.txt",ios::app);
cout<<"Now enter the text: "<<endl;
cin.get(ch);
cin.getline(str,50);
fout<<str<<endl;
fout.close();
}
void display() //function to display data
{
char str[50];
104 | P a g e
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(!fin.eof())
{
fin.getline(str,50);
cout<<str<<endl;
}
fin.close();
}
void digit() //function to count number of digits
{int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>ch)
{
if(isdigit(ch))
count++;
}
cout<<"Number of digits are: "<<count;

105 | P a g e
fin.close();
}
void alphabet() //function to count number of alphabets
{int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>ch)
{
if(isalpha(ch))
count++;
}
cout<<"Number of alphabets are: "<<count;
fin.close();
}
void word() //function to count number of words
{int count=0;
char str[50];
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}

106 | P a g e
while(fin>>str)
{
count++;
}
cout<<"Number of words are: "<<count;
fin.close();
}
void line() //function to find number of lines
{int count=0;
char str[50];
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin.getline(str,50))
{
count++;
}
cout<<"Number of lines are: "<<count;
fin.close();
}
void space() //function to count number of spaces
{int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)

107 | P a g e
{
cout<<"File not found";
exit(-1);
}
while(fin.get(ch))
{
if(ch==' ')
count++;
}
cout<<"Number of spaces are: "<<count;
fin.close();
}
void vowel() //function to count number of vowels
{int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>ch)
{
if(isalpha(ch))
{switch(ch)
{
case 'a':
case 'e':
case 'i':

108 | P a g e
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':count++;break;
}
}
}
cout<<"Number of vowels are: "<<count;
fin.close();
}
void consonant() //function to count number of consonants
{
int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>ch)
{
if(isalpha(ch))
{
switch(ch)
{

109 | P a g e
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':break;
default:count++;
}}}
cout<<"Number of consonants are: "<<count;
fin.close();
}
void uppercase() //function to count number of uppercase characters
{int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}

while(fin>>ch)
{
if(isupper(ch))
count++;

110 | P a g e
}
cout<<"Number of uppercase characters are: "<<count;
fin.close();
}
void lowercase() //function to count number of lowercase characters
{
int count=0;
char ch;
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>ch)
{
if(islower(ch))
count++;
}
cout<<"Number of lowercase characters are: "<<count;
fin.close();
}
void the() //function to count number of the
{
int count=0;
char str[50];
ifstream fin;
fin.open("Text.txt");
if(!fin)

111 | P a g e
{
cout<<"File not found";
exit(-1);
}
while(fin>>str)
{
if(strcmpi(str,"the")==0)
count++;
}
cout<<"Number of \"the\"s are: "<<count;
fin.close();
}
void three_letter() //function to count number of three letter words
{
int count=0;
char str[50];
ifstream fin;
fin.open("Text.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(fin>>str)
{int flag=1;
for(int i=0;i<3;i++)
if(!isalpha(str[i]))
flag=0;
if((strlen(str)==3)&&(flag))
count++;

112 | P a g e
}
cout<<"Number of three lettered words are: "<<count;
fin.close();
}
void main()
{
int choice;char ans='y';
do
{clrscr();
cout<<"\t\tMENU"; //menu
cout<<"\n1) Create file or append old file";
cout<<"\n2) Display Data from file";
cout<<"\n3) Show digit count";
cout<<"\n4) Show alphabet count";
cout<<"\n5) Show word count";
cout<<"\n6) Show line count";
cout<<"\n7) Show space count";
cout<<"\n8) Show vowel count";
cout<<"\n9) Show consonants";
cout<<"\n10) Show count of all uppercase characters";
cout<<"\n11) Show count of all lowercase characters";
cout<<"\n12) Show count of the word 'THE','the' or 'The'";
cout<<"\n13) Show count of all three lettered words";
cout<<"\n14) Exit the program"<<endl;
cin>>choice;
clrscr();
switch(choice)
{
case 1:create();
break;

113 | P a g e
case 2:display();
break;
case 3:digit();
break;
case 4:alphabet();
break;
case 5:word();
break;
case 6:line();
break;
case 7:space();
break;
case 8:vowel();
break;
case 9:consonant();
break;
case 10:uppercase();
break;
case 11:lowercase();
break;
case 12:the();
break;
case 13:three_letter();
break;
case 14:cout<<"\n***Program Ended***";
exit(0);
break;
default:cout<<"\nsorry invalid choice";
}
cout<<"\nDo you want to continue ";

114 | P a g e
cin>>ans;
}while((ans=='y')||(ans=='Y'));
}

115 | P a g e
OUTPUT:

116 | P a g e
117 | P a g e
118 | P a g e
119 | P a g e
120 | P a g e
121 | P a g e
122 | P a g e
/*Write a complete program in C++ that reads a text file ORIG.TXT and creates another
identical file NEW.TXT except for the fact that every sequence of consecutive blank space
is replaced by a single space. */
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void create() //function to create or append file
{
char ch,str[50];
ofstream fout;
cout<<"\nDo you want to append old file(O) or enter new data(N): ";
cin>>ch;
if((ch=='N')||(ch=='n'))
fout.open("Original.txt");
else
fout.open("Original.txt",ios::app);
cout<<"Now enter the text:"<<endl;
cin.get(ch);
cin.getline(str,50);
fout<<str<<endl;
fout.close();
}
void display_original() //function to display original file
{
cout<<"Displaying original"<<endl;
char str[50];
ifstream fin;
fin.open("Original.txt");
if(!fin)
{
123 | P a g e
cout<<"File not found";
exit(-1);
}
while(!fin.eof())
{
fin.getline(str,50);
cout<<str<<endl;
}
fin.close();
}
void display_new() //function to display new file
{
cout<<"Displaying new:"<<endl;
char str[50];
ifstream fin;
fin.open("New.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
while(!fin.eof())
{
fin.getline(str,50);
cout<<str<<endl;
}
fin.close();
}
void copy_alter() //function to copy data after altering
{char str[50];

124 | P a g e
ifstream fin;
ofstream fout;
fin.open("Original.txt");
if(!fin)
{
cout<<"File not found";
exit(-1);
}
fout.open("New.txt");
while(fin.getline(str,50))
{
for(int i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
fout<<str[i];
else if(i==strlen(str)-1)
fout<<str[i];
else if(str[i+1]!=' ')
fout<<" ";
}fout<<endl;
}
fin.close();
fout.close();
cout<<"\nData copied"<<endl;
}
void main()
{int choice;char ans;
do
{clrscr();
cout<<"\t\tMENU"; //menu

125 | P a g e
cout<<"\n1) Enter text in original file";
cout<<"\n2) Display original file";
cout<<"\n3) Copy data from original to new after altering";
cout<<"\n4) Display new file";
cout<<"\n5) Exit"<<endl;
cin>>choice;
clrscr();
switch(choice)
{
case 1:create();
break;
case 2:display_original();
break;
case 3:copy_alter();
break;
case 4:display_new();
break;
case 5: cout<<"***Program Ended***";
exit(1);
break;
default:cout<<"\nInvalid Choice";
}
cout<<"\nDo you want to continue(y/n): ";
cin>>ans;
}while((ans=='Y')||(ans=='y'));
}

126 | P a g e
OUTPUT:

127 | P a g e
128 | P a g e
129 | P a g e
/* Consider the class given:
class BUS
{
int Bus_No;
char Destination[15];
float Distance;
public:
void READ( );
void WRITE( );
void ENTER ( );
void SHOW( );
void UPDATE ( ); // to ask for a Bus_No and update the other details
};

Complete the member functions definition of all methods. Use the binary file "BUS.dat".
Ensure that required validations(file existing) are done before displaying the records, in the
main ( ).Records should be displayed sequentially from beginning to end. Also, add any
other function , if needed.*/

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class BUS
{
int Bus_No;
char Destination[15];
float Distance;
public:
void ENTER()
{
cout<<"\n\t\t ENTER THE DETAILS OF THE BUS"<<endl;
cout<<"\n\t ENTER THE BUS NUMBER :";
cin>>Bus_No;
cout<<"\n\t ENTER THE DESTINATION :";

130 | P a g e
gets(Destination);
cout<<"\n\tENTER THE DISTANCE :";
cin>>Distance;
SHOW();
}
void SHOW()
{
cout<<"\n\t\t BUS DETAILS "<<endl;
cout<<"\n\t THE BUS NUMBER IS :"<<Bus_No;
cout<<"\n\t THE DESTINATION IS :";
puts(Destination);
cout<<"\t THE DISTANCE TO BE TRAVELLED IS :"<<Distance;
}
void WRITE(BUS b1)
{
ofstream fout;
fout.open("BUS.dat",ios::binary|ios::app);
fout.write((char*)&b1,sizeof(b1));
fout.close();
}
void READ();
int getbusno()
{
return Bus_No;
}
void UPDATE();
};
void BUS::READ()
{
BUS B;

131 | P a g e
ifstream fin;
fin.open("BUS.dat",ios::binary);
if(!fin)
{
cout<<"\n\t THE FILE DOESNOT EXISTS :";
exit(-1);
}
while( fin.read((char*)&B,sizeof(B)))
{
B.SHOW();
getch();
}
}
void BUS::UPDATE()
{
BUS b1;
fstream f;
f.open("BUS.dat",ios::binary|ios::in|ios::out);
if(!f)
{
cout<<"\n\t THE FILE DOESNOT EXISTS !!!";
exit(-1);
}
int busno,found=0;
cout<<"\n\t ENTER THE BUS NUMBER WHOSE INFORMATION IS
TO BE UPDATED :";
cin>>busno;
while(f.read((char*)&b1,sizeof(b1)))
{
if(b1.getbusno()==busno)

132 | P a g e
{
found=1;
cout<<"\n\t ENTER THE NEW DATA :"<<endl;
b1.ENTER();
f.seekg(-1*sizeof(b1),ios::cur);
f.write((char*)&b1,sizeof(b1));
f.close();
break;
}
}
if(found==1)
{
cout<<"\n\t RECORD FOUND !!!!"<<endl;
f.seekg(0);
cout<<"\n\t NOW THE FILE CONTAINS :";
while(!f.eof())
{
f.read((char*)&b1,sizeof(b1));
b1.SHOW();
getch();
}
f.close();
}
else
{
cout<<"\n\t THE RECORD IS NOT FOUND";
}
}
void main()
{

133 | P a g e
clrscr();
BUS b1;
int ch;
cout<<"\n\t\tBUS SERVICES"<<endl;
cout<<"\n\t1.Enter details";
cout<<"\n\t2.Show Records";
cout<<"\n\t3.Update Records";
re:
cout<<"\n\n\tEnter your input: ";
cin>>ch;
switch(ch)
{
case 1: b1.ENTER();
b1.WRITE(b1);
break;
case 2: b1.READ();
break;
case 3: b1.UPDATE();
break;
default: cout<<"\n\tWrong Input!"<<endl;
goto re;
}
getch();
}

134 | P a g e
OUTPUT:

135 | P a g e
136 | P a g e
/* Consider the class given:

class CAR
{
int CAR_No;
char Destination[15];
float Distance;
public:
void READ( );
void WRITE( );
void ENTER();
void SHOW( );
void DELETE( ); // to delete records as per user's choice
};

Complete the definition of all methods .Use the binary file "CAR.dat" . Ensure that
required validations(file existing) are done before displaying the records, in the main (
).Records should be displayed sequentially from beginning to end. Add any function if
needed.*/

#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<stdio.h>
class CAR
{
int CAR_NO;
char Destination[15];
float Distance;
public:
void DELETE();
void READ();
void WRITE();
void ENTER()
{
cout<<"\nEnter The Car Number: ";
cin>>CAR_NO;

137 | P a g e
cout<<"Enter The Destination: ";
gets(Destination);
cout<<"Enter Distance: ";
cin>>Distance;
}
void SHOW()
{
cout<<"\nCar Number: ";
cout<<CAR_NO;
cout<<"\nDestination: ";
puts(Destination);
cout<<"Distance: ";
cout<<Distance<<endl;
}
int ReturnCN()
{
return CAR_NO;
}
};
void CAR::READ()
{
CAR C;
ifstream fin;
fin.open("CAR.dat",ios::binary);
if(!fin)
{
cout<<"\nFile Does Not Exist ";
exit(-1);
}

138 | P a g e
while(fin.read((char*)&C,sizeof(C)))
{
C.SHOW();
cout<<"\nPress any key to view more"<<endl;
getch();
}
if(fin.eof())
cout<<"\n*End of file reached*";
fin.close();
}
void CAR::WRITE()
{
CAR C;
ofstream fout;
fout.open("CAR.dat",ios::binary|ios::app);
C.ENTER();
fout.write((char*)&C,sizeof(C));
fout.close();
}
void CAR::DELETE()
{
CAR C;
int cno;
char ans,found='f';
cout<<"\nEnter The Car No whose data is to be deleted: ";
cin>>cno;
ifstream fin("CAR.dat",ios::binary);
ofstream fout("TEMP.dat",ios::binary);
if(!fin)
{
cout<<"\nFile Does Not Exist ";
139 | P a g e
exit(-1);
}
while(fin.read((char*)&C,sizeof(C)))
{
if(C.ReturnCN()==cno)
{
found='t';
cout<<"\nData which is to be deleted ";
C.SHOW();
cout<<"\nAre you sure you want to delete ";
cin>>ans;
if(ans=='n')
fout.write((char*)&C,sizeof(C));
}
else
fout.write((char*)&C,sizeof(C));
}
if(found=='f')
cout<<"\nData was not found ";
fin.close();
fout.close();
remove("CAR.dat");
rename("TEMP.dat","CAR.dat");
fin.open("CAR.dat",ios::binary);
cout<<"\nNow the file contains: ";
while(fin.read((char*)&C,sizeof(C)))
{
C.SHOW();
}
fin.close();

140 | P a g e
}
void main()
{
clrscr();
int ans;
char ch;
CAR C;
do
{
cout<<"\n\t\t\t***CAR MENU***"<<endl;
cout<<"\n1)Write the data onto the file ";
cout<<"\n2)Read the data and show its contents ";
cout<<"\n3)Delete the record ";
cout<<"\n\nEnter your input: ";
cin>>ans;
switch(ans)
{
case 1: cout<<"\nEnter the details: "<<endl;
C.WRITE();
break;
case 2: cout<<"\nThe file contains: "<<endl;
C.READ();
break;
case 3: C.DELETE();
break;
default:cout<<"\nWrong Option\b";
}
cout<<"\n\n\tDo you want to continue?(y/n): ";
cin>>ch;
}while((ch=='y')||(ch=='Y'));

141 | P a g e
getch();
}

142 | P a g e
OUTPUT:

143 | P a g e
144 | P a g e
ARRAYS
(USING
FUNCTIONS)

145 | P a g e
/*Write a program to 1.add two matrices 2. Subtract 2 matrices 3. Multiply 2 matrices. Use
switch case and use functions to achieve the task.*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main( )
{
clrscr();
char ch,ch1;
void ADD();
void SUB();
void PROD();
do
{
cout<<"\n\t\t****MATRIX MENU****"<<endl<<endl;;
cout<<"\nWhich operation do you want to perform?"<<endl;
cout<<"\n 1.Addition of two matrices.\n 2.Subraction of two matrices.\n 3.Product of two
matrices.";
cout<<"\n\nEnter your choice(1,2 or 3): ";
cin>>ch;
switch(ch)
{
case '1': ADD();
break;
case '2': SUB();
break;
case '3': PROD();
break;
default: cout<<"\nInvalid choice!";
break;
146 | P a g e
}
cout<<"\n\nDo you want to continue?(y/n): ";
cin>>ch1;
}while((ch1=='y')||(ch1=='Y'));
getch();
}
void ADD()
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q,k;
cout<<"\n Input row and column for matrix-A:";
cin>>m>>n;
cout<<"\n Input row and column for matrix-B:";
cin>>p>>q;
if(m==p && n==q)
cout<<"\n\nMatrix can be added."<<endl;
else
{
cout<<"\n Matrix cannot be added";
exit(0);
}
cout<<"\nInput matrix-A:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n MATRIX-A:"<<endl;
for(i=0;i<m;i++)
{

147 | P a g e
cout<<"\n" ;
for(j=0;j<n;j++)
cout<<" "<<a[i][j];
}
cout<<"\n\nInput matrix-B:"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
cout<<"\n MATRIX-B:";
for(i=0;i<p;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
cout<<" "<<b[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"\n\n Sum of matrices is :";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
}

148 | P a g e
void SUB()
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q,k;
cout<<"\n Input row and column for matrix-A:";
cin>>m>>n;
cout<<"\n Input row and column for matrix-B:";
cin>>p>>q;
if((m==p) && (n==q))
{
cout<<"\n Matrix can be subtracted";
}
else
{
cout<<"\n Matrix cannot be subtracted";
exit(0);
}
cout<<"\nInput matrix-A:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n MATRIX-A:"<<endl;
for(i=0;i<m;i++)
{
cout<<"\n" ;
for(j=0;j<n;j++)
cout<<" "<<a[i][j];
}

149 | P a g e
cout<<"\n\nInput matrix-B:"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
cout<<"\n MATRIX-B:";
for(i=0;i<p;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
cout<<" "<<b[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
cout<<"\n\n Difference of matrices is :";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
}
void PROD()
{ int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q,k;
cout<<"\n Input row and column for matrix-A:";

150 | P a g e
cin>>m>>n;
cout<<"\n Input row and column for matrix-B:";
cin>>p>>q;
if(n==p)
{
cout<<"\n Matrix can be multiplied";
{
cout<<"\nInput matrix-A:"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\n MATRIX-A:"<<endl;
for(i=0;i<m;i++)
{
cout<<"\n" ;
for(j=0;j<n;j++)
cout<<" "<<a[i][j];
}
cout<<"\n\nInput matrix-B:"<<endl;
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
cout<<"\n MATRIX-B:";
for(i=0;i<p;i++)
{
cout<<"\n";

151 | P a g e
for(j=0;j<q;j++)
cout<<" "<<b[i][j];
}
cout<<"\n\n Product of two matrices is: ";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+a[i][j]*b[i][j];
cout<<c[i][j]<<" ";
}
}}
}
else
{
cout<<"\nMatrices cannot be multiplied!";
exit(0);
}
}

152 | P a g e
OUTPUT:

153 | P a g e
154 | P a g e
155 | P a g e
/*Write a program to 1.transpose a matrix 2. Find the row sum 3. Find the column sum 4.
sum diagonal elements 5. sum elements below main diagonal 6. sum elements above main
diagonal of a given matrix. Use switch case and use functions to achieve the task.*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void rowsum();
void columnsum();
void transmat();
void diag();
void diagsum();
void main()
{
char ch;
int mat1[10][10],i,j,mat2[10][10],choice,a,b;
do
{
clrscr();
cout<<"\n\n\n\n\t\t Menu ";
cout<<"\n\n 1. Transpose a matrix";
cout<<"\n\n 2. Row sum ";
cout<<"\n\n 3. Column sum";
cout<<"\n\n 4. Sum of diagonal elements";
cout<<"\n\n 5. Sum of above and below main diagonal";
cout<<"\n\n Enter choice: ";
cin>>choice;
clrscr();
switch(choice)
{
156 | P a g e
case 1: transmat();
break;
case 2: rowsum();
break;
case 3: columnsum();
break;
case 4: diagsum();
break;
case 5: diag();
break;
default: cout<<"\n wrong choice";
}
cout<<"\n\n Do you wish to continue(y/n): ";
cin>>ch;
}while(ch=='y');
getch();
}
void transmat()
{
int i,j,mat1[10][10];
cout<<"\n\n Matrix:"<<endl;
cout<<"\nEnter rows: ";
cin>>i;
cout<<"Enter columns: ";
cin>>j;
cout<<"\n Enter elements in matrix"<<endl;
for(int a=0;a<i;a++)
{
for(int b=0;b<j;b++)
{ cin>>mat1[a][b];

157 | P a g e
}
}
int temp[10][10];
cout<<"\n Transpose a matrix"<<endl;
for(a=0;a<j;a++)
{
for(int b=0;b<i;b++)
{
temp[a][b]=mat1[b][a];
}
}
for(a=0;a<j;a++)
{ for(int b=0;b<i;b++)
{
cout<<" "<<temp[a][b];
}
cout<<endl;
}
}

void rowsum()
{
int i,j,mat1[10][10];
cout<<"\n\n Matrix:"<<endl;
cout<<"\nEnter rows: ";
cin>>i;
cout<<"Enter columns: ";
cin>>j;
cout<<"\n Enter elements in matrix"<<endl;
for(int a=0;a<i;a++)

158 | P a g e
{
for(int b=0;b<j;b++)
cin>>mat1[a][b];
}
for(a=0;a<i;a++)
{
int rs=0;
for(int b=0;b<j;b++)
{
rs+=mat1[a][b];
}
cout<<"\n Sum of row #"<<a+1<<" is: "<<rs;
}
}
void columnsum()
{
int i,j,mat1[10][10];
cout<<"\n\n Matrix:"<<endl;
cout<<"\nEnter rows: ";
cin>>i;
cout<<"Enter columns: ";
cin>>j;
cout<<"\n Enter elements in matrix"<<endl;
for(int a=0;a<i;a++)
{
for(int b=0;b<j;b++)
cin>>mat1[a][b];
}
for(a=0;a<j;a++)
{

159 | P a g e
int cs=0;
for(int b=0;b<i;b++)
{
cs+=mat1[b][a];
}
cout<<"\n Sum of column #"<<a+1<<" is: "<<cs;
}
}
void diag()
{
int i,j,mat1[10][10];
cout<<"\n\n Matrix:"<<endl;
cout<<"\nEnter rows: ";
cin>>i;
cout<<"Enter columns: ";
cin>>j;
cout<<"\n Enter elements in matrix"<<endl;
for(int a=0;a<i;a++)
{
for(int b=0;b<j;b++)
cin>>mat1[a][b];
}
int sum1=0;
int sum2=0;
for(a=0;a<i;a++)
{
for(int b=0;b<j;b++)
{
if(a<b)
{

160 | P a g e
sum1+=mat1[a][b];
}
}
}
cout<<"\n Sum of above main diagonal is: "<<sum1;
for(a=0;a<i;a++)
{
for(int b=0;b<j;b++)
{
if(a>b)
{
sum2+=mat1[a][b];
}
}
}
cout<<"\n Sum of below main diagonal is: "<<sum2;
}
void diagsum()
{
int i,j,mat1[10][10];
cout<<"\n\n Matrix:"<<endl;
cout<<"\nEnter rows: ";
cin>>i;
cout<<"Enter columns: ";
cin>>j;
cout<<"\n Enter elements in matrix"<<endl;
for(int a=0;a<i;a++)
{
for(int b=0;b<j;b++)
cin>>mat1[a][b];

161 | P a g e
}
int sum=0;
for(a=0;a<i;a++)
{
for(int b=0;b<j;b++)
{
if(a==b)
sum+=mat1[a][b];
else if(b==(j-1-a))
sum+=mat1[a][b];
}
}
cout<<"\nSum of diagonal elements: "<<sum;
}

162 | P a g e
OUTPUT:

163 | P a g e
164 | P a g e
/*Write a program to illustrate the two types of searches in an integer type array. Make use
of functions and make a menu driven program.*/

#include<iostream.h>
#include<conio.h>
int lsearch(int [],int,int);
int bsearch(int [],int,int);
void main()
{
clrscr();
int A[50],size,ch1,ele,res;
char ch;
do
{
cout<<"\n\t\t\t****SEARCH MENU****";
cout<<"\n\n\tEnter size of array : ";
cin>>size;
cout<<"\n\n\tEnter elements of array(ascending order) : ";
for( int i=0;i<size;i++)
cin>>A[i];
cout<<"\n\n\tEnter the element you want to search : ";
cin>>ele;
cout<<"\n\n\tChoose the type of search: "<<endl;
cout<<"\n1.Linear Search";
cout<<"\n2.Binary Search";
cout<<"\n\n\tEnter your choice : ";
cin>>ch1;
if(ch1==1)
res=lsearch(A,size,ele);
if(ch1==2)

165 | P a g e
res=bsearch(A,size,ele);
if(res==-1)
cout<<"\n\tELEMENT NOT FOUND";
else
cout<<"\n\n\tElement found at Index: "<<res<<", Position: "<<res+1;
cout<<"\n\n\tDo you want to continue?(y/n): ";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}
int lsearch(int AR[],int s,int val)
{
for(int i=0;i<s; i++)
if(AR[i]==val)
return i;
return -1;
}
int bsearch(int AR[],int s,int val)
{
int beg,last,mid;
beg=0;
last=s-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(AR[mid]==val)
return mid;
else if(val>AR[mid])
beg=mid+1;
else

166 | P a g e
last=mid-1;
}
return -1;
}

167 | P a g e
OUTPUT:

168 | P a g e
/*Write a program to illustrate all the three sorts – Insertion, Selection and Bubble sort in
an array. Make use of functions to do the same. Make a menu driven and user friendly
program.*/

#include<iostream.h>
#include<conio.h>
#include<limits.h>
void Selection(int ar[],int n)
{
int small,pos,tmp;
for(int i=0;i<n;i++)
{
small=ar[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(ar[j]<small)
{
small=ar[j];
pos=j;
}
}
tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
}
cout<<"\n\n The sorted array : ";
for(i=0;i<n;i++)
cout<<" "<<ar[i];
}
void bubble(int ar[],int n)
169 | P a g e
{
int tmp;
for(int i=0;i<n;i++)
{
for(int j=0;j<(n-1)-i;j++)
{
if(ar[j]>ar[j+1])
{
tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp;
}
}
}
cout<<"\n\n The sorted array : ";
for(i=0;i<n;i++)
cout<<" "<<ar[i];
}
void insertion(int ar[],int n)
{
int AR[50];
for(int k=0;k<n;k++)
AR[k+1]=ar[k];
int tmp,j;
AR[0]=INT_MIN;
for(int i=1;i<=n;i++)
{
tmp=AR[i];
j=i-1;
while(tmp<AR[j])

170 | P a g e
{
AR[j+1]=AR[j];
j--;
}
AR[j+1]=tmp;
}
cout<<"\n\n The sorted array : ";
for(i=1;i<=n;i++)
cout<<AR[i]<<" ";
}
void main()
{
char ch='y';
do
{
int ar[50],item,n,index,flag=0;
char ans;
clrscr();
cout<<"\n\n\t\t\t****SORTING MENU****\n\n Enter the size of array: ";
cin>>n;
cout<<"\n Enter the array : ";
for(int i=0;i<n;i++)
{
cout<<" ";
cin>>ar[i];
}
cout<<"\n Enter the method of sorting :\n A. Selection Sort \n B. Bubble Sort \n C. Insertion
Sort \n ";
cout<<"\n Enter your choice: ";
do

171 | P a g e
{
cin>>ans;
if((ans=='A')||(ans=='a'))
{
Selection(ar,n);
flag=0;
}
else
if((ans=='B')||(ans=='b'))
{
bubble(ar,n);
flag=0;
}
else
if((ans=='C')||(ans=='c'))
{
insertion(ar,n);
flag=0;
}
else
{
cout<<"\n\n WRONG CHOICE, TRY AGAIN \n\n ";
flag=1;
}
}while(flag==1);
cout<<"\n\n Do you want to continue ? : ";
cin>>ch;
}while(ch=='y');
getch();
}

172 | P a g e
OUTPUT:

173 | P a g e
174 | P a g e
/*Write a program to merge two given arrays into the third array C .Use switch case and
implement the same with atleast 4 different combinations. Make use of functions.*/

#include<conio.h>
#include<iostream.h>
void ASC1DES1(int n1,int n2)
{
int A[20],B[20],C[40],a,b,c;
cout<<"\n Enter Array 1 [Ascending] : ";
for(int i=0;i<n1;i++)
cin>>A[i];
cout<<"\n Enter Array 2 [Descending] : ";
for(i=0;i<n2;i++)
cin>>B[i];
for(a=0,b=n2-1,c=0;((a<n1)&&(b>=0));)
{
if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
if(a<n1)
{
while(a<n1)
C[c++]=A[a++];
}
else
{
while(b>=0)
C[c++]=B[b--];

175 | P a g e
}
cout<<"\n The Merged Array is : ";
for(i=0;i<n1+n2;i++)
cout<<C[i]<<" ";
}
void DES1ASC1(int n1, int n2)
{
int A[20],B[20],C[40],a,b,c;
cout<<"\n Enter Array 1 [Descending] : ";
for(int i=0;i<n1;i++)
cin>>A[i];
cout<<"\n Enter Array 2 [Ascending] : ";
for(i=0;i<n2;i++)
cin>>B[i];
for(a=n1-1,b=0,c=0;((a>=0)&&(b<n2));)
{
if(A[a]<=B[b])
C[c++]=A[a--];
else
C[c++]=B[b++];
}
if(b<n2)
{
while(b<n2)
C[c++]=B[b++];
}
else
{
while(a>=0)
C[c++]=A[a--];

176 | P a g e
}
cout<<"\n The Merged Array is : ";
for(i=(n1+n2)-1;i>=0;i--)
cout<<C[i]<<" ";
}
void DES2(int n1, int n2)
{
int A[20],B[20],C[40],a,b,c;
cout<<"\n Enter Array 1 [Descending] : ";
for(int i=0;i<n1;i++)
cin>>A[i];
cout<<"\n Enter Array 2 [Descending] : ";
for(i=0;i<n2;i++)
cin>>B[i];
for(a=n1-1,b=n2-1,c=0;((a>=0)&&(b>=0));)
{
if(A[a]<=B[b])
C[c++]=A[a--];
else
C[c++]=B[b--];
}
if(a>0)
{
while(a>=0)
C[c++]=A[a--];
}
else
{
while(b>=0)
C[c++]=B[b--];

177 | P a g e
}
cout<<"\n The Merged Array is : ";
for(i=0;i<n1+n2;i++)
cout<<C[i]<<" ";
}
void ASC2(int n1, int n2)
{
int A[20],B[20],C[40],a,b,c;
cout<<"\n Enter Array 1 [Ascending] : ";
for(int i=0;i<n1;i++)
cin>>A[i];
cout<<"\n Enter Array 2 [Ascending] : ";
for(i=0;i<n2;i++)
cin>>B[i];
for(a=0,b=0,c=0;((a<n1)&&(b<n2));)
{
if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b++];
}
if(a<n1)
{
while(a<n1)
C[c++]=A[a++];
}
else
{
while(b<n2)
C[c++]=B[b++];

178 | P a g e
}
cout<<"\n The Merged Array is : ";
for(i=0;i<n1+n2;i++)
cout<<C[i]<<" ";
}
void main()
{
int ch,n1,n2;
char ch1;
do
{
clrscr();
cout<<"\n\t\tMerging Menu"<<endl;
cout<<"\n 1. To Merge 1 Ascending and 1 Descending Array into ascending ";
cout<<"\n 2. To Merge 1 Descending and 1 Ascending Array into descending ";
cout<<"\n 3. To Merge 2 Descending Arrays into ascending ";
cout<<"\n 4. To Merge 2 Ascending Arrays into ascending ";
cout<<"\n\n Select An Option : ";
cin>>ch;
cout<<"\nEnter size of array 1: ";
cin>>n1;
cout<<"Enter size of array 2: ";
cin>>n2;
switch(ch)
{
case 1 : ASC1DES1(n1,n2);
break;
case 2 : DES1ASC1(n1,n2);
break;
case 3 : DES2(n1,n2);

179 | P a g e
break;
case 4 : ASC2(n1,n2);
break;
default : cout<<"\n Wrong Choice !! ";
}
cout<<"\n\nDo you want to continue?(y/n): ";
cin>>ch1;
}while((ch1=='y')||(ch1=='Y'));
getch();
}

180 | P a g e
OUTPUT:

181 | P a g e
182 | P a g e
/*An array, ARRAY [20], consists of 20 positive integers .Write a complete user friendly,
menu driven functions oriented C++ program to do the following:
1. Find the pairs of integers stored in A having sum = 50
2. Find the number of elements which are odd
3. Find the number of elements which are even
4. Find the number of elements which are divisible by 30
5. Find the largest of all the elements
6. Find the smallest of all the elements
7. Find the average of all the elements*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{clrscr();
int Array[20],r,s;
char t;
cout<<"\nENTER THE NO OF ELEMENTS OF THE ARRAY"<<endl;
cin>>r;
cout<<"\nENTER THE VALUES OF THE ARRAY"<<endl;
for(int i=0;i<r;i++)
{cin>>Array[i];
}
do
{
void sum50(int h[20],int c);
void oddel(int h[20],int c);
void evenel(int h[20],int c);
void largestel(int h[20],int c);

183 | P a g e
void smallestel(int h[20],int c);
void averagel(int h[20],int c);
cout<<"\nENTER YOUR CHOICE ABOUT FUNCTION YOU WANT TO PROCESS ON
ARRRAY"<<endl;
cout<<"\nENTER 1 FOR FINDING PAIRS WHICH HAVE SUM EQUAL TO 50"<<endl;
cout<<"\nENTER 2 FOR FINDING ODD ELEMENTS OF THE ARRAY"<<endl;
cout<<"\nENTER 3 FOR FINDING EVEN ELEMENTS OF THE ARRAY"<<endl;
cout<<"\nENTER 4 FOR FINDING LARGEST ELEMENT OF THE ARRAY"<<endl;
cout<<"\nENTER 5 FOR FINDING SMALLEST ELEMENT OF THE ARRAY"<<endl;
cout<<"\nENTER 6 FOR FINDING AVERAGE OF ALL ELEMENTS OF THE
ARRAY"<<endl;
cout<<"\nENTER YOUR CHOICE :";
cin>>s;
if(s==1)
{sum50(Array,r);
}
else if(s==2)
{oddel(Array,r);
}
else if(s==3)
{evenel(Array,r);
}
else if(s==4)
{largestel(Array,r);
}
else if(s==5)
{smallestel(Array,r);
}
else if(s==6)
{averagel(Array,r);
}
184 | P a g e
else
cout<<"\n!!!!!ERROR!!!!!"<<endl;
cout<<"\nENTER YOUR CHOICE DO YOU WANT TO CONTINUE TYPE Y FOR YES
"<<endl;
cin>>t;
}while((t=='y')||(t=='Y'));
getch();
}
void sum50(int h[20],int c )
{int count=0;
for(int i=0;i<c;i++)
{
if(h[i]+h[i+1]==50)
count++;
}
cout<<"\nTHE NUMBER OF PAIRS ARE :- "<<count;
}
void oddel(int h[20],int c)
{
for(int i=0;i<c;i++)
{
if(h[i]%2!=0)
{
cout<<"\nODD NO :"<<h[i];
}
}
}
void evenel(int h[20],int c)
{
for(int i=0;i<c;i++)

185 | P a g e
{
if(h[i]%2==0)
{
cout<<"\nEVEN NO :-"<<h[i];
}
}
}
void largestel(int h[20],int c)
{
int largest=0;
for(int i=0;i<c;i++)
{
if(h[i]>largest)
largest=h[i];
}
cout<<"\nLARGEST ELEMENT IS "<<largest;
}
void smallestel(int h[20],int c)
{
int smallest=h[0];
for(int i=0;i<c;i++)
{
if(h[i]<smallest)
{
smallest=h[i];
}
}
cout<<"\nSMALLEST ELEMENT IS "<<smallest;
}
void averagel(int h[20],int c)

186 | P a g e
{float temp=0.0;
float avg;
for(int i=0;i<c;i++)
{
temp=h[i]+temp;
}
avg=temp/c;
cout<<"\nTHE AVERAGE OF ARRAY IS :- "<<avg;
}

187 | P a g e
OUTPUT:

188 | P a g e
189 | P a g e
/*Let A be N X N two-dimensional array. Write user-defined functions in C++ for the
following: 1. To find the number of non-zero elements in A. 2. To find the sum of all
elements in A. 3. To find the sum of all elements A[i][j] for which i > j. 4. To find the sum of
all elements A[i][j] for which i < j. 5. To find the product of all elements A[i][j] for which i
= j.*/

#include<iostream.h>
#include<conio.h>
#include<process.h>
void zero(int a[100][100],int m, int n)
{
int count=0;
for(int i=0;i<m;i++)
{ for(int j=0;j<n;j++)
if(a[i][j]!=0)
count++;
}
cout<<"\nNumber of Non-Zero elements are: ";
cout<<count;
}
void sum(int a[100][100],int m,int n)
{ int sum=0;
for(int i=0;i<m;i++)
{ for(int j=0;j<n;j++)
sum+=a[i][j];
}
cout<<"\nSum of the matrix is: "<<sum;
}
void sum1(int a[100][100],int m,int n)
{ int sum=0,i,j;
for(i=0;i<m;i++)

190 | P a g e
{ for(j=0;j<n;j++)
if(i>j)
sum+=a[i][j];
}
cout<<"\nThe sum is: "<<sum;
}
void sum2(int a[100][100],int m,int n)
{ int sum=0,i,j;
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
if(i<j)
sum+=a[i][j];
}
cout<<"\nThe sum is: "<<sum;
}
void prod(int a[100][100],int m,int n)
{ int i,j,prod=1;
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
if(i==j)
prod=prod*a[i][j];
}
cout<<"\nThe product is: "<<prod;
}
void main()
{ clrscr();
int a[100][100],i,j,m,n;
int c;
char ch;
cout<<"\n\t\t\t****SQUARE MATRIX OPERATIONS****"<<endl<<endl;

191 | P a g e
cout<< "\nEnter the row and column of the matrix: ";
cin>>m>>n;
if(m==n)
cout<<"\nIt is a square matrix! Proceed!";
else
{ cout<< "\nIt is not a square matrix! Cannot Proceed!";
getch();
exit(0);
}
cout<<"\nInput matrix: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{ cout<<"\t";
cin>> a[i][j];
}
cout<<"\n";
}
do
{
cout<<"\n\tChoices are as follows:"<<endl;
cout<<"\n1.Display the number non zero elements";
cout<<"\n2.Display the sum of all the elements";
cout<<"\n3.Display the sum of elements for which i>j";
cout<<"\n4.Display the sum of elements for which i<j";
cout<<"\n5.Display the product of all the elements for which i=j";
cout<<"\n\nEnter choice:";
cin>>c;
switch(c)
{ case 1:zero(a,m,n);

192 | P a g e
break;
case 2:sum(a,m,n);
break;
case 3:sum1(a,m,n);
break;
case 4:sum2(a,m,n);
break;
case 5:prod(a,m,n);
break;
default: cout<<"Wrong choice";
}
cout<<"\n\nDo you wish to continue?(y/n): ";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
getch();
}

193 | P a g e
OUTPUT:

194 | P a g e
195 | P a g e
//Write a program to handle arrays and strings using functions.
CODE 1:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void LENGTH(char word[20]); //A
void DIV10(int s[20], int sz) ; //B
void DIV10SUM(int arr[20], int sz); //C
void DIV102D(int MAT[10][10], int r, int c); //D
void DIV10SUM(int MAT[10][10], int r, int c); //E
void diagonal(int A[10][10], int r, int c); //F
void diagnolprod(int MAT[10][10], int r, int c); //G
void FINDCHAR1(char ch); //H
void FINDCHAR2( char ch); //I
void LOWERCHAR1(char ch); //J
void LOWERCHAR2(char ch); //K
void REV(int ar[10], int sz); //L
void ROWSUM(int MAT[10][10], int r, int c); //M
void COLSUM(int MAT[10][10], int r, int c); //N
void ROWPROD(int MAT[10][10], int r, int c); //O
void COLPROD(int MAT[10][10], int r, int c); //P
void diagonalsum(int MAT[10][10], int r, int c); //Q
void ADDFUNCT100 (int AR[10], int sz); //R
void ADDFUNCT100(int A[10][10], int r, int c); //S
void COUNTSPACE(char str[10]); //T
void EQUAL(int A[20][20],int B[20][20], int r, int c,int r2,int c2); //U
void ARRAY(double arr[10],int); //V
void DOUBLE(int a[4][4],int r, int c); //W

196 | P a g e
void ADDMAT(int B[20][20],int A[20][20], int r, int c,int r2,int c2); //X
void SUBMAT(int A[20][20],int B[20][20], int r, int c,int r2,int c2); //Y
void PRODMAT(int A[20][20],int B[20][20], int r, int c,int r2,int c2); //Z
void main()
{
clrscr();
char ch,x,p;
char s2[100];
int arr[20],MAT[10][10],A[20][20],B[20][20],D[20][20],a[4][4];
double s[10];
int i,j,r,c,sz,r2,c2;
cout<<"\n \t MENU OF FUNCTIONS\n ";
cout<<"\n A.Length of string ";
cout<<"\n B.Elements divisible by 10 ";
cout<<"\n C.Sum of elements divisible by 10 ";
cout<<"\n D.Elements of matrix divisible by 10";
cout<<"\n E.Sum of elements of matrix divisible by 10 ";
cout<<"\n F.Elements on diagonal ";
cout<<"\n G.Product of elements on diagonal ";
cout<<"\n H.Identify character ";
cout<<"\n I.Identify character(built in functions) ";
cout<<"\n J.Identify Upper/Lowercase character ";
cout<<"\n K.Identify Upper/Lowercase character(built in functions) ";
cout<<"\n L.Reverse of array ";
cout<<"\n M.Find row sum ";
cout<<"\n N.Find column Sum";
cout<<"\n O.Find row Product";
cout<<"\n P.Find column Product";
cout<<"\n Q.Sum of elements on diagonal(2D) ";
cout<<"\n R.Add 100 to each element(1D)";

197 | P a g e
cout<<"\n S.Add 100 to each element(2D) ";
cout<<"\n T.Count nunmber of spaces";
cout<<"\n U.Check for matrice equality ";
cout<<"\n V.Swapping 1st and last value ";
cout<<"\n W.Doubling every element ";
getch();
cout<<"\n X.Add two matrices ";
cout<<"\n Y.Subtract two matrices ";
cout<<"\n Z.Product of two matrices";
do{
cout<<"\n\n Enter your choice(A-Z): ";
cin>>ch;
switch(ch)
{
case 'A': cout<<"\n Enter the string: ";
gets(s2);
LENGTH(s2);
break;
case 'B': cout<<"Enter size of array: ";
cin>>sz;
cout<<"\n Enter the array: ";
for (int i=0;i<sz; i++)
cin>>arr[i];
DIV10(arr,sz);
break;
case 'C': cout<<"Enter size of array: ";
cin>>sz;
cout<<"\n Enter the array: ";
for (i=0;i<sz; i++)
cin>>arr[i];

198 | P a g e
DIV10SUM(arr,sz);
break;
case 'D': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\nEnter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
DIV102D(MAT,r,c);
break;
case 'E': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
DIV10SUM(MAT,r,c);
break;
case 'F': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";

199 | P a g e
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
diagonal(MAT,r,c);
break;
case 'G': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for( i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
diagnolprod( MAT,r,c);
break;
case 'H': cout<<"\n Enter a character: ";
cin>>p;
FINDCHAR1(p);
break;
case 'I': cout<<"\n Enter a character: ";
cin>>p;

200 | P a g e
FINDCHAR2(p);
break;
case 'J': cout<<"\n Enter a character: ";
cin>>p;
LOWERCHAR1(p);
break;
case 'K': cout<<"\n Enter a character: ";
cin>>p;
LOWERCHAR2(p);
break;
case 'L': cout<<"Enter size of array: ";
cin>>sz;
REV(arr,sz);
break;
case 'M': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
ROWSUM(MAT,r,c);
break;
case 'N': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;

201 | P a g e
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
COLSUM(MAT,r,c);
break;
case 'O': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
ROWPROD(MAT,r,c);
break;
case 'P': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for( i=0;i<r;i++)

202 | P a g e
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
COLPROD( MAT,r,c);
break;
case 'Q': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for( i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
diagonalsum(MAT,r,c);
break;
case 'R': cout<<"Enter size of array: ";
cin>>sz;
cout<<"\n Enter the array: ";
for (i=0;i<sz; i++)
cin>>arr[i];
ADDFUNCT100(arr,sz);
break;
case 'S': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";

203 | P a g e
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
ADDFUNCT100(MAT,r,c);
break;
case 'T': cout<<"\n Enter the string: ";
gets(s2);
COUNTSPACE(s2);
break;
case 'U': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter matrix 1: \n";
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
cin>>A[i][j];
}
cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r2;
cout<<"Columns: ";
cin>>c2;
cout<<"\n Enter matrix 2: \n";
for(i=0;i<r2;i++)

204 | P a g e
{ for(j=0;j<c2;j++)
cin>>B[i][j];
}
EQUAL(A,B,r,c,r2,c2);
break;
case 'V': cout<<"Enter size of array: ";
cin>>sz;
cout<<"\n Enter the array: ";
for(i=0;i<sz;i++)
cin>>s[i];
ARRAY(s,sz); //V
break;
case 'W': int MAT[4][4];
cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter the matrix: \n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>MAT[i][j];
}
DOUBLE(MAT,r,c);
break;
case 'X': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";

205 | P a g e
cin>>c;
cout<<"\n Enter matrix 1: \n";
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
cin>>B[i][j];
}
cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r2;
cout<<"Columns: ";
cin>>c2;
cout<<"\n Enter matrix 2: \n";
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
cin>>A[i][j];
}
ADDMAT(A,B,r,c,r2,c2);
break;
case 'Y': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter matrix 1: \n";
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
cin>>A[i][j];
}
cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";

206 | P a g e
cin>>r2;
cout<<"Columns: ";
cin>>c2;
cout<<"\n Enter matrix 2: \n";
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
cin>>B[i][j];
}
SUBMAT(A,B,r,c,r2,c2);
break;
case 'Z': cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r;
cout<<"Columns: ";
cin>>c;
cout<<"\n Enter matrix 1: \n";
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
cin>>A[i][j];
}
cout<<"\nEnter number of rows and columns:";
cout<<"\nRows: ";
cin>>r2;
cout<<"Columns: ";
cin>>c2;
cout<<"\n Enter matrix 2: \n";
for(i=0;i<r2;i++)
{ for(j=0;j<c2;j++)
cin>>B[i][j];
}

207 | P a g e
PRODMAT(A,B,r,c,r2,c2);
break;
default: cout<<"\n Invalid choice!";
}
cout<<"\n\n Do you want to continue?(y/n): ";
cin>>x;
}while((x=='y')||(x=='Y'));
getch();
}
void LENGTH(char word[20]) //A
{
for(int i=0;word[i]!='\0';i++);
cout<<"\nLength of string: "<<i;
}
void DIV10(int arr[10], int sz) //B
{
cout<<"\nElements divisible by 10: ";
for(int i=0;i<sz;i++)
{
if(arr[i]%10==0)
{
cout<<arr[i]<<" ";
}
}
}
void DIV10SUM( int arr[10], int sz) //C
{
int sum=0;
for(int i=0;i<sz;i++)
{ if( arr[i]%10==0)

208 | P a g e
sum+=arr[i];
}
cout<<"\nSum of elements divisible by 10: "<<sum;
}
void DIV102D(int MAT[10][10], int r, int c) //D
{
cout<<"\nElements of matrix divisible by 10: ";
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
if(MAT[i][j]%10==0)
cout<<MAT[i][j]<<" ";
}
void DIV10SUM(int MAT[10][10], int r, int c) //E
{
int sum=0;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
if(MAT[i][j]%10==0)
sum+=MAT[i][j];
cout<<"\nSum of elements divisible by 10: "<<sum;
}
/*void DIV2DSUM(int A[10][10], int r, int c) //
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{ if(i==j)
cout<<A[i][j];
else
cout<<" ";

209 | P a g e
}
{ if(j==c-1)
cout<<"\n";
}
}
} */
void diagonal(int A[10][10], int r, int c) //F
{
cout<<"\nElements on diagonal: ";
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{ if(i==j)
cout<<A[i][j]<<" ";
}
}
}
void diagnolprod(int MAT[10][10], int r, int c) //G
{ int prod=1;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{ if(i==j)
prod*=MAT[i][j];
}
}
cout<<"\nProduct of elements on diagonal: "<<prod;
}
void FINDCHAR1(char ch) //H
{

210 | P a g e
if(isalpha(ch))
cout<<"\n It is an alphabet. ";
else if(isdigit(ch))
cout<<"\n It is a digit. ";
else
cout<<"\n It is a special character. ";
}
void FINDCHAR2( char ch) //I
{
if(isalpha(ch))
cout<<"\n An alphabet. ";
else if(isdigit(ch))
cout<<"\n A digit. ";
else
cout<<"\n Symbol";
}
void LOWERCHAR1(char ch) //J
{
if((ch>='a')&&(ch<='z'))
cout<<"\n Entered character is a lowercase alphabet. ";
else if((ch>='A')&&(ch<='Z'))
cout<<"\n Entered character is a uppercase alphabet. ";
else
cout<<"\n Entered character is not an alphabet.";
}
void LOWERCHAR2(char ch) //K
{
if(islower(ch))
cout<<"\n It is a lowercase. ";
else if(isupper(ch))

211 | P a g e
cout<<"\n It is an uppercase. ";
}
void REV(int arr[10], int sz) //L
{
int j;
cout<<"\n Enter the array: ";
for(int i=0;i<sz;i++)
{
cin>>arr[i];
}
cout<<"\n The original array is: ";
for(i=0;i<sz;i++)
{
cout<<arr[i]<<" ";
}
for (j=sz-1,i=0;i<sz/2;i++,j--)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
cout<<"\n Revered array : ";
for(i=0;i<sz;i++)
cout<<arr[i]<<" ";
}
void ROWSUM(int MAT[10][10], int r, int c) //M
{
int i,j;
for (i=0;i<r;i++)
{

212 | P a g e
int s=0;
for(j=0;j<c;j++)
{
s=s+MAT[i][j];
}
cout<<"\n Sum of row #"<<i+1<<": "<<s;
}
}
void COLSUM(int MAT[10][10], int r, int c) //N
{
int i,j;
for (i=0;i<r;i++)
{
int s=0;
for(j=0;j<c;j++)
{
s=s+MAT[j][i];
}
cout<<"\n Sum of column #"<<i+1<<": "<<s;
}
}
void ROWPROD(int MAT[10][10], int r, int c) //O
{
for(int i=0;i<r;i++)
{
int p=1;
for(int j=0;j<c;j++)
{
p*=MAT[i][j];
}

213 | P a g e
cout<<"\n Product of row #"<<i+1<<": "<<p;
}
}
void COLPROD(int MAT[10][10], int r, int c) //P
{
for(int j=0;j<r;j++)
{
int p=1;
for(int i=0;i<c;i++)
p*=MAT[i][j];
cout<<"\n Product of column #"<<j+1<<": "<<p;
}
}
void diagonalsum(int MAT[10][10], int r, int c) //Q
{
int sum=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
if(i ==j)
sum+=MAT[i][j];
}
cout<<"\nSum of elements on diagonal: "<<sum;
}
void ADDFUNCT100 (int ARR[10], int sz) //R
{
cout<<"\n\tOriginal Array: ";
for(int i=0;i<sz;i++)
cout<<ARR[i]<<" ";
cout<<"\n\tNew Array: ";

214 | P a g e
for(i=0;i<sz;i++)
cout<<ARR[i]+100<<" ";
}
void ADDFUNCT100(int A[10][10], int r, int c) //S
{
cout<<"\nOriginal Matrix: "<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
cout<<"\nNew Matrix: "<<endl;
for(i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<A[i][j]+100<<" ";
}
cout<<endl;
}
}
void COUNTSPACE(char str[10]) //T
{
int count=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]==' ')

215 | P a g e
count++;
}
cout<<"\n Number of spaces: "<<count;
}
void EQUAL(int A[20][20],int B[20][20], int r, int c,int r2,int c2) //U
{
if((r==r2)&&(c==c2))
{
cout<<"\n Proceed ";
int flag=1;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
if( A[i][j]==B[i][j])
{
flag=0;
break;
}
if(flag==0)
cout<<"\n Matrices are equal. ";
else
cout<<"\n Matrices are not equal. ";
}
}
else
{
cout<<"\n Don't Proceed ";
}
}
void ARRAY(double arr[10], int sz) //V

216 | P a g e
{
int temp;
temp =arr[0];
arr[0]=arr[sz-1];
arr[sz-1]=temp;
cout<<"\nArray after swapping: ";
for(int i=0;i<sz;i++)
cout<<arr[i]<<" ";
}
void DOUBLE(int A[4][4],int r,int c) //W
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
A[i][j]=A[i][j]*2;
}
}
cout<<"\nNew Matrix: "<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}
void ADDMAT(int A[20][20],int B[20][20], int r, int c,int r2,int c2) //X

217 | P a g e
{
int C[20][20];
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
cout<<"\n Sum of matrices= ";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<" "<<C[i][j];
}
}
}
void SUBMAT(int A[20][20],int B[20][20], int r, int c,int r2,int c2) //Y
{
int C[20][20];
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
C[i][j]=A[i][j]-B[i][j];
}

218 | P a g e
}
cout<<"\n Difference ";
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<" "<<C[i][j];
}
}
}
void PRODMAT(int A[20][20],int B[20][20], int r, int c, int r2,int c2) //Z
{
if((r==r2)&&(c==c2))
{
int C[20][20];
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
C[i][j]=1;
C[i][j]=A[i][j]*B[i][j];
}
}
cout<<"\n Product of 2 matrices= ";
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)

219 | P a g e
{
cout<<" "<<C[i][j];
}
}
}
}

220 | P a g e
OUTPUT:

221 | P a g e
222 | P a g e
223 | P a g e
224 | P a g e
CODE 2:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void LOWER(char str1[50]); //1
void LOWER1(char str1[50]); //2
void LOWER2(char str1[50]); //3
void LOWER3(char str1[50]); //4
void LOWER4(char str1[50]); //5
void LOWER5(char str1[50]); //6
void UPPER(char str1[50]); //7
void UPPER1(char str1[50]); //8
void UPPER2(char str1[50]); //9
void UPPER3(char str1[50]); //10
void UPPER4(char str1[50]); //11
void UPPER5(char str1[50]); //12
void FUNCSTRLEN(char str1[50]); //13
int FUNCSTRLEN1(char str1[50]); //14
int FUNCSTRLEN2(char str1[50]); //15
void STRCONCAT1(char str1[50], char str2[50]); //16
void STRCONCAT2(char str1[50], char str2[50]); //17
void STRCOPYSTR1(char str1[50]); //18
void STRCOPYSTR2(char str1[50]); //19
void STRCONVERT(char str1[50]); //20
void main()
{
clrscr();

225 | P a g e
int choice1, len;
char choice2, s1[50], s2[50];
cout<<"\n\t\tSTRINGS AND ARRAYS ";
cout<<"\n\t\t\tMENU";
cout<<"\n1. Find and print all the lower case letters without ctype.h";
cout<<"\n2. Find and print the count of all the lower case letters without ctype.h";
cout<<"\n3. Find and print all the lower case letters with ctype.h";
cout<<"\n4. Find and print the count of all the lower case letters with ctype.h";
cout<<"\n5. Find all lower case letters and convert them to upper case without ctype.h";
cout<<"\n6. Find all lower case letters in array and convert to upper case with ctype.h";
cout<<"\n7. Find and print all the upper case letters without ctype.h";
cout<<"\n8. Find and print count of all the upper case letters without ctype.h";
cout<<"\n9. Find all upper case letters and convert them to lower case using ctype.h";
cout<<"\n10. Find and print the count of all upper case letters using ctype.h";
cout<<"\n11. Find all upper case letters and convert them to lower case without ctype.h";
cout<<"\n12. Find all upper case letters and convert them to lower case using ctype.h";
cout<<"\n13. Find the length of the string without string.h";
cout<<"\n14. Find the length of the string without string.h";
cout<<"\n15. Find the length of the string with string.h";
cout<<"\n16. Add 2 strings into another third string without string.h";
cout<<"\n17. Add 2 strings into another third string with string.h";
cout<<"\n18. Copy one string to another string without string.h";
cout<<"\n19. Copy one string to another string with string.h";
cout<<"\n20. Convert the entire string to uppercase or lowercase with string.h";
do
{
cout<<"\n\nEnter a choice: ";
cin>>choice1;
switch(choice1)
{

226 | P a g e
case 1: cout<<"\nEnter the string: ";
gets(s1);
LOWER(s1);
break;
case 2: cout<<"\nEnter the string: ";
gets(s1);
LOWER1(s1);
break;
case 3: cout<<"\nEnter the string: ";
gets(s1);
LOWER2(s1);
break;
case 4: cout<<"\nEnter the string: ";
gets(s1);
LOWER3(s1);
break;
case 5: cout<<"\nEnter the string: ";
gets(s1);
LOWER4(s1);
break;
case 6: cout<<"\nEnter the string: ";
gets(s1);
LOWER5(s1);
break;
case 7: cout<<"\nEnter the string: ";
gets(s1);
UPPER(s1);
break;
case 8: cout<<"\nEnter the string: ";
gets(s1);

227 | P a g e
UPPER1(s1);
break;
case 9: cout<<"\nEnter the string: ";
gets(s1);
UPPER2(s1);
break;
case 10: cout<<"\nEnter the string: ";
gets(s1);
UPPER3(s1);
break;
case 11: cout<<"\nEnter the string: ";
gets(s1);
UPPER4(s1);
break;
case 12: cout<<"\nEnter the string: ";
gets(s1);
UPPER5(s1);
break;
case 13: cout<<"\nEnter the string: ";
gets(s1);
FUNCSTRLEN(s1);
break;
case 14: cout<<"\nEnter the string: ";
gets(s1);
len=FUNCSTRLEN1(s1);
cout<<"\nLength of string is "<<len;
break;
case 15: cout<<"\nEnter the string: ";
gets(s1);
len=FUNCSTRLEN2(s1);

228 | P a g e
cout<<"\nLength of string is "<<len;
break;
case 16: cout<<"\nEnter first string: ";
gets(s1);
cout<<"\nEnter second string: ";
gets(s2);
STRCONCAT1(s1, s2);
break;
case 17: cout<<"\nEnter first string: ";
gets(s1);
cout<<"\nEnter second string: ";
gets(s2);
STRCONCAT1(s1, s2);
break;
case 18: cout<<"\nEnter the string: ";
gets(s1);
STRCOPYSTR1(s1);
break;
case 19: cout<<"\nEnter the string: ";
gets(s1);
STRCOPYSTR2(s1);
break;
case 20: cout<<"\nEnter the string: ";
gets(s1);
STRCONVERT(s1);
break;
default: cout<<"\nWrong Choice!!";
}
cout<<"\n\nDo you wish to continue? (y or n) ";
cin>>choice2;

229 | P a g e
} while ((choice2=='Y')||(choice2=='y'));
getch();
}
void LOWER(char str1[]) //1
{
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='a')&&(str1[i]<='z'))
cout<<str1[i]<<" ";
}
void LOWER1(char str1[]) //2
{
int count=0;
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='a')&&(str1[i]<='z'))
count++;
cout<<"\nNumber of lowercase letters: "<<count;
}
void LOWER2(char str1[]) //3
{
for (int i=0; str1[i]!='\0'; i++)
if (islower(str1[i]))
cout<<str1[i]<<" ";
}
void LOWER3(char str1[]) //4
{
int count;
for (int i=0; str1[i]!='\0'; i++)
if (islower(str1[i]))
count++;
cout<<"\nNumber of lowercase letters: "<<count;

230 | P a g e
}
void LOWER4(char str1[]) //5
{
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='a')&&(str1[i]<='z'))
str1[i]-=32;
cout<<str1;
}
void LOWER5(char str1[]) //6
{
for (int i=0; str1[i]!='\0'; i++)
if (islower(str1[i]))
str1[i]=toupper(str1[i]);
cout<<str1;
}
void UPPER(char str1[]) //7
{
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='A')&&(str1[i]<='Z'))
cout<<str1[i]<<" ";
}
void UPPER1(char str1[]) //8
{
int count=0;
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='A')&&(str1[i]<='Z'))
count++;
cout<<"\nNumber of uppercase letters: "<<count;
}
void UPPER2(char str1[]) //9

231 | P a g e
{
for (int i=0; str1[i]!='\0'; i++)
if (isupper(str1[i]))
str1[i]=tolower(str1[i]);
cout<<str1;
}
void UPPER3(char str1[]) //10
{
int count;
for (int i=0; str1[i]!='\0'; i++)
if (isupper(str1[i]))
count++;
cout<<"\nNumber of uppercase letters: "<<count;
}
void UPPER4(char str1[]) //11
{
for (int i=0; str1[i]!='\0'; i++)
if ((str1[i]>='A')&&(str1[i]<='Z'))
str1[i]+=32;
cout<<str1;
}
void UPPER5(char str1[]) //12
{
for (int i=0; str1[i]!='\0'; i++)
if (isupper(str1[i]))
str1[i]=tolower(str1[i]);
cout<<str1;
}
void FUNCSTRLEN(char str1[]) //13
{

232 | P a g e
for (int l=0; str1[l]!='\0'; ++l);
cout<<"Length of the string is "<<l;
}
int FUNCSTRLEN1(char str1[]) //14
{
int len;
for (len=0; str1[len]!='\0'; ++len);
return len;
}
int FUNCSTRLEN2(char str1[]) //15
{
int len;
len=strlen(str1);
return len;
}
void STRCONCAT1(char str1[], char str2[]) //16
{
char str3[100];
for (int i=0;str1[i]!='\0';i++);
for (int j=0;str1[j]!='\0';j++);
for (int k=0; k<i ; k++)
{
str3[k]=str1[k];
}
for (int l=0; l<j; l++)
{
str3[k+l]= str2[l];
}
cout<<str3;
}

233 | P a g e
void STRCONCAT2(char str1[], char str2[]) //17
{
char str3[100];
strcat(str3, str1);
strcat(str3, str2);
cout<<"Concatenated string: "<<str3;
}
void STRCOPYSTR1(char str1[]) //18
{
char str2[50];
for (int i=0; str1[i]!='\0'; i++)
str2[i]=str1[i];
str2[i]='\0';
cout<<"Original String: ";
puts(str1);
cout<<"Altered String: ";
puts(str2);
}
void STRCOPYSTR2(char str1[]) //19
{
char str2[100];
cout<<"Enter String: ";
cin.getline(str2,100);
cout<<"Original String: "<<str2;
strcpy(str2, str1);
cout<<"Altered String: "<<str2;
}
void STRCONVERT(char str1[]) //20
{
for (int i=0; str1[i]!='\0'; i++)

234 | P a g e
{
if ((str1[i]>='A')&&(str1[i]<='Z'))
{
str1[i]+=32;
}
else if ((str1[i]>='a')&&(str1[i]<='z'))
{
str1[i]-=32;
}
}
cout<<str1;
}

235 | P a g e
OUTPUT:

236 | P a g e
237 | P a g e
LINKED
LISTS,
STACKS &
QUEUES

238 | P a g e
//Write a program in C++ that creates a complete linked list class.

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node //Declaration of node
{
int info;
struct node*next;
}*start;
class List //Class Declaration
{
public:
node*create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void display();
List()
{
start= NULL;
}
};
void main() //Main Menu
{
clrscr();
char ch;
int choice,nodes,element,position,i;

239 | P a g e
List l;
start=NULL;
do
{ clrscr();
cout<<"\n\t\t**************************";
cout<<"\n\t\t Operations on linked list " ;
cout<<"\n\t\t**************************";
cout<<"\n\t\t1. Insert Node at Beginning ";
cout<<"\n\t\t2. Insert Node at a Position";
cout<<"\n\t\t3. Insert Node at Last ";
cout<<"\n\t\t4. Delete a Node";
cout<<"\n\t\t5. Display Linked List ";
cout<<"\n\t\t6. Exit ";
cout<<"\n\n\t\t Enter your choice: ";
re:
cin>>choice;
switch(choice)
{
case 1: cout<<"\n\t Inserting Node at Beginning: ";
l.insert_begin();
cout<<"\n";
break;
case 2: cout<<"\n\t Inserting Node at a Position: ";
l.insert_pos();
cout<<"\n";
break;
case 3: cout<<"\n\t Inserting Node at Last: ";
l.insert_last();
cout<<"\n";
break;

240 | P a g e
case 4: cout<<"\n\t Deleting a Node: ";
l.delete_pos();
cout<<"\n";
break;
case 5: cout<<"\n\t Display the Linked List: ";
l.display();
cout<<"\n";
break;
case 6: cout<<"\n\t Program Over ";
goto end;
default: cout<<"\n\t Wrong choice!";
cout<<"\n\t Reenter Choice: ";
goto re;
}
cout<<"\n\t Do you wish to continue? (y or n) ";
cin>>ch;
} while((ch=='y')||(ch=='Y'));
end:
getch();
}
//Creating Node
node*List::create_node(int value)
{
struct node *temp, *s;
temp=new(struct node);
if(temp==NULL)
{
cout<<"\n Memory not allocated! ";
return 0;
}

241 | P a g e
else
{
temp->info=value;
temp->next=NULL;
return temp;
}
}
void List::insert_begin() // Inserting element in beginning
{
int value;
cout<<"\n\n\t Enter value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp= create_node(value);
if(start== NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
start=temp;
start->next=p;
}
cout<<"\n\n\t Element Inserted at Beginning. ";
}

//Inserting Node at Last


void List::insert_last()

242 | P a g e
{
int value;
cout<<"\n\n\t Enter value to be inserted: ";
cin>>value;
struct node *temp,*s;
temp=create_node(value);
s=start;
while(s->next !=NULL)
{
s=s->next;
}
temp->next=NULL;
s->next=temp;
cout<<"\n\n\t Element inserted.\n ";
}
//Insertion of node at a given position
void List::insert_pos()
{
int value,pos,counter=0;
cout<<"\n\n\t Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s,*ptr;
temp=create_node(value);
cout<<"\n\n\t Enter the position at which node is to be inserted: ";
cin>>pos;
int i;
s=start;
while(s!=NULL)
{
s=s->next;

243 | P a g e
counter++;
}
if(pos==1)
{
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{ ptr=start;
start=temp;
start->next=ptr;
}
}
else if((pos>1)&& (pos<=counter))
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=temp;
temp->next=s;
}
else
{cout<<"\n\n\t Position out of range.";
}
}

244 | P a g e
//Delete element at a given position
void List::delete_pos()
{
int pos,i,counter=0;
if(start==NULL)
{
cout<<"\n\n\t List is empty. ";
return;
}
cout<<"\n\n\t Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s=start;
if(pos==1)
{
start=s->next;
}
else
{
while(s!=NULL)
{
s=s->next;
counter++;
}
if((pos>0)&&(pos<=counter))
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;

245 | P a g e
s=s->next;
}}
else
{
cout<<"\n\n\t Position out of range. ";
}
free(s);
cout<<"\n\n\t Element Deleted. ";
}
}
//Display Elements of a link list
void List::display()
{
struct node *temp;
if(start==NULL)
{
cout<<"\n\n\t The List is Empty.";
return;
}
temp=start;
cout<<"\n\n\t Elements of list are: ";
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"\n\n\t NULL ";
}

246 | P a g e
OUTPUT:

247 | P a g e
248 | P a g e
249 | P a g e
//Write a program to implement the use of Queue class.

#include<iostream.h> //header file for console input and output


#include<conio.h> //header file for function to clear the screen
#include<process.h> //header file for function to exit program
class queue //class to implement queue
{
private:
int front,rear; //positions of first and last elements in queue
int a[5]; //the actual queue, with each data member in this array
public:
queue() //constructor
{
int front=0; //This signifies that
int rear=0; //the queue has no data member
}
void addQ(int item) //Function for inserting element
{
if(rear==5) /*Checking if queue is full
Maximum capacity of queue is 5 */
cout<<"\nQueue is full ";
else
a[rear++]=item; /*Adding element to the back of the queue
as per First In First Out(FIFO) rule*/
}
void delQ()
{
if(front==rear) //Checking if queue is empty
cout<<"\nQueue is empty ";
else

250 | P a g e
front++; /*front is incremented, thereby virtually deleting
front element as per FIFO rule*/
}
void display(); //Function to display queue
};
void queue::display()
{
if(front<rear)
for( int i=front;i<=rear;i++)
cout<<a[front++]<<" ";
}
void main() //start of main()
{
queue q1; //object of class queue
int item,choice; //Declaring necessary variables
char ch; //for do-while loop condition
do
{
clrscr(); //function to clear screen
cout<<"\n\t\tQueue Manipulation Menu ";
cout<<"\n1.Add item to queue ";
cout<<"\n2.Delete item from queue ";
cout<<"\n3.Display all items in queue ";
cout<<"\n4.Exit ";
cout<<"\nYour Choice : ";
cin>>choice; //taking user's choice
switch(choice)
{
case 1 : cout<<"\nEnter element ";
cin>>item;

251 | P a g e
q1.addQ(item);
break;
case 2 : q1.delQ();
cout<<"\nItem deleted ";
break;
case 3 : q1.display();
break;
case 4 : exit(0);
break;
default :cout<<"\nPlease enter valid choice ";
break;
}
cout<<"\nDo you wish to continue (y/n) : ";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
} //end of main

252 | P a g e
OUTPUT:

253 | P a g e
254 | P a g e
//Write a program to implement the use of Stack class

#include<iostream.h> //Header file for Console Input and Output Functions


#include<conio.h> //Header file that includes Function clrscr()
#include<process.h> //Header file that contains Function exit()
#define size 5 //Defines a constant named 'size' with value 5
class stack //Defines a new class named 'stack'
{
private: //Declares private members of class 'stack'
int A[size]; //An array of size 'size' (5)
int top; //Variable for knowing position of top element
public: //Declares public members of class 'stack'
stack() //Constructor of class 'stack'
{
top=-1;
}
void push(int item); //Function to add element to Array
int pop(); //Function to delete element from Array
void display(); //Function to display elements of Array
};
void stack::push(int item) //Definition of function push of class stack
{
if(top==size-1) //Checking if stack is full
cout<<"\nStack is Full ";
else
{
top++; //Increment position of top element
A[top]=item; //The new top element is initialized
}
}

255 | P a g e
int stack::pop() //Definition of function pop of class stack
{
int item;
if(top==-1) //Checking if stack is empty
{
cout<<"\nStack is Empty : ";
return(-1);
}
else
{
item=A[top]; //Storing value of top element for display purposes
top--; //Decrementing position of top element,
//Previous top element is effectively deleted,
//For it will not be displayed as part of stack
return item;
}
}
void stack::display() //Definition of function display of class stack
{
int i;
for(i=top;i>=0;i--)
cout<<A[i]<<" "; //Displaying each element of array in stack
}
void main() //Start of main()
{
clrscr(); //function to clear screen
stack s1; //Crating object of class 'stack'
int choice,val; //Declaring neccesary
char ch; //variables for program
/* 'choice' for switch case , 'val' for storing value

256 | P a g e
taken from user, 'ch' for do-while loop
*/
do //start of do-while loop
{
cout<<"\n\t\tStack Manipulation Menu "<<endl;
cout<<"\n\t1.Add Element (Push) "; // Menu for
cout<<"\n\t 2.Delete Element(Pop) "; // manipulation of
cout<<"\n\t 3.Display Elements "; // data stored in
cout<<"\n\t 4.Exit "; // stack
cout<<"\n\t Your Choice : ";
cin>>choice; //User's choice taken
switch(choice) //start of switch case statement
{
case 1 : cout<<"\nEnter value to be entered/pushed : ";
cin>>val;
s1.push(val); //Calling function 'push'
break;
case 2 : int x;
x=s1.pop(); //Calling function 'pop' and
cout<<"\nThe value deleted/popped is : "<<x;
break;
case 3 : cout<<"Elements in Stack :- "<<endl;
s1.display(); //Calling function 'display'
break;
case 4 : exit(0); //Exiting from program
break;
default : cout<<"\nPlease enter a valid choice "; //In case of wrong choice
break;
} //End of switch case
cout<<"\nDo you want to continue (y/n) : ";
cin>>ch; //Taking choice from user
257 | P a g e
}while((ch=='y')||(ch=='Y')); //End of do-while loop
} //End of main()

258 | P a g e
OUTPUT:

259 | P a g e
260 | P a g e
//Write a program in C++ to add, delete, search and display an element in a linked queue.

Input:
#include<iostream.h> //Header file for Console Input Output Functions
#include<conio.h> //Header file for Function clrscr()
#include<process.h> //Header file for Function exit()
struct node //Declaring structure 'node'
{
int data;
node *next; //Pointer to itself
};
class queue //Creating a new linked class 'queue'
{
private: //Declaring private members of class 'queue'
node *rear; //pointer to object of structure 'node'
node *front; //pointer to object of structure 'node'
public: //Declaring public members of class node
queue() //Constructor for class 'queue'
{
rear=NULL;front=NULL;
}
void qinsert(); //
void qdelete(); //Function
void qdisplay(); //Prototypes
void qsearch(); //
~queue(); //Destructor Prototype
};
void queue::qinsert() //Defining member function 'qinsert'
{
node *temp;

261 | P a g e
temp=new node;
cout<<"Data : ";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
void queue::qdelete() //Defining member function 'qdelete'
{
if(front!=NULL)
{
node*temp=front;
cout<<front->data<<"\nElement Deleted "<<endl;
front=front->next;
delete temp;
if(front==NULL)
rear=NULL;
}
else
cout<<"\nQueue Empty ";
}
void queue::qdisplay() //Defining member function 'qdisplay'

262 | P a g e
{
node*temp=front;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
queue::~queue() //Defining destructor for 'queue'
{
while(front!=NULL)
{
node*temp=front;
front=front->next;
delete temp;
}
}
void queue::qsearch() //Defining member function 'qsearch'
{
int item,fl=0,pos=0;
cout<<"\nEnter item to be searched for : " ;
cin>>item;
node*temp=front;
while(temp!=NULL)
{
pos++;
if(item==temp->data)
{
cout<<"\n"<<item<<" found at position "<<pos;
fl=1;

263 | P a g e
break;
}
else
temp=temp->next;
}
if(!fl)
cout<<"\nItem not found ";
}
void main() //Start of main()
{
clrscr(); //clearing the screen
queue q1; //creating object of class 'queue'
int choice; //Declaring necessary
char ch; //variables
/*choice for switch case,
ch for do-while loop*/
do //Start of do-while loop
{
cout<<"\n\t\tQueue Manipulation Menu "; //Menu
cout<<"\n\t1.Insert Element "; //for
cout<<"\n\t2.Delete Element "; //manipulation
cout<<"\n\t3.Search For Element "; //of
cout<<"\n\t4.Display All Elements "; //data
cout<<"\n\t5.Exit "; //in
cout<<"\n\tYour Choice : "; //queue
cin>>choice;
switch(choice) //start of switch case
{
case 1 : q1.qinsert(); //calling function qinsert()
break;

264 | P a g e
case 2 : q1.qdelete(); //calling function qdelete()
break;
case 3 : q1.qsearch(); //calling function qsearch()
break;
case 4 : q1.qdisplay(); //calling function qdisplay()
break;
case 5 : exit(0); //exits the program
break;
default : cout<<"\nPlease enter a valid choice "; //In case of wrong choice
break;
}
cout<<"\nDo you wish to continue (y/n) : ";
cin>>ch;
}while((ch=='y')||(ch=='Y')); //End of do-while loop
} //End of main()

265 | P a g e
OUTPUT:

266 | P a g e
267 | P a g e
//Write a complete menu driven program in C++ for the implementation of linked stack.

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
node*next;
}*top,*newptr,*save,*ptr; // TOP POINTS TO BEGINNING OF LINKED-STACK.
node*create_new_node(int); // RETURNS THE POINTER TO THE NEW NODE.
void push(node*); // INSERTS THIS NODE*TYPE POINTER IN THE BEGINNING.
void display(node*);
void pop();
void pushing()
{
top=NULL;
intinf;
charch='y';
gotoxy(25,3);
textcolor(10);
cputs(" PUSHING IN LINKED-STACK");
textcolor(14);
gotoxy(25,4);
cputs("*************************");
cout<<"\n";
while((ch=='y')||(ch=='Y'))
{

268 | P a g e
cout<<"\n Enter INFORMATION for the new node : ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr == NULL)
{
cout<<"\n Cannot create new node!!!Aborting!!\n";
exit(1);
}
push(newptr);
cout<<"\n Now the linked-stack is : ";
display(top);
cout<<"\n Press Y to enter more nodes, N to exit : ";
cin>>ch;
}
}
voidpoping()
{
top=NULL;
intinf;
charch='y';
gotoxy(23,3);
textcolor(10);
cputs(" POPPING IN LINKED-STACK");
textcolor(14);
gotoxy(23,4);
cputs("*************************");
cout<<"\n";
while((ch=='y')||(ch=='Y'))
{
cout<<"\n Enter INFORMATION for the new node : ";

269 | P a g e
cin>>inf;
newptr=create_new_node(inf);
if(newptr == NULL)
{
cout<<"\n Cannot create new node!!!Aborting!!\n";
system("pause");
exit(1);
}
push(newptr);
cout<<"\n Press Y to enter more nodes , N to exit : ";
cin>>ch;
}
do
{
cout<<"\n The stack now is : ";
display(top);
cout<<"\n Want to pop an element ? (Y/N) : ";
cin>>ch;
if(ch=='y'||ch=='Y')
pop();
}while(ch=='y'||ch=='Y');
}
void main()
{
mn:
textcolor(10);
clrscr();
int g;
cout<<"\n\n\n\n\n\n";
cout<<"\n\t\t\t MENU \n";

270 | P a g e
cout<<"\n\n\t\t\t 1. PUSHING LINKED-STACK"
<<"\n\n\t\t\t 2. POPPING LINKED-STACK"
<<"\n\n\t\t\t 3. EXIT";
gotoxy(70,25);
g=getch();
switch(g)
{
case '1' : textcolor(11);
clrscr();
pushing();
gotomn;
break;
case '2' : textcolor(13);
clrscr();
poping();
gotomn;
break;
case '3' : exit(-1);
default :clrscr();
gotoxy(20,13);
textcolor(14);
cputs("WRONG KEY PRESSED");
delay(1000);
gotomn;
}
getch();
}
void pop()
{
if(top==NULL)

271 | P a g e
cout<<"UNDERFLOW !!!\n";
else
{
ptr = top;
top = top -> next;
deleteptr;
}
}
node*create_new_node(int n)
{
ptr=new node;
ptr -> info = n;
ptr -> next = NULL;
returnptr;
}
void push(node*np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np -> next = save;
}
}
void display(node*np)
{
while(np!=NULL)
{

272 | P a g e
cout<<" "<<np -> info<<" -> ";
np=np -> next;
}
cout<<"!!!\n";
}

273 | P a g e
OUTPUT :

274 | P a g e
275 | P a g e
//Write an interactive menu driven C++ program to implement circular queue using an
array consisting of integer data values

#include<iostream.h>
#include<stdlib.h>
#include<process.h> //for exit()
#include<conio.h>
int insert_in_CQ(int[],int); //prototype
void display(int[],int,int); //prototype
int del_in_CQ(int CQueue[]); //prototype
/*---------------------------------------------------------------------
Function Insert_in_Q() receieves the circular queue-array and element
to be inserted into.
If overflow occurs, it returns -1, and if it successfully inserts the
element, it returns 0.
function del_in_CQ() deletes an element from circular queue.
function display() displays the CQueue after insertion.
----------------------------------------------------------------------*/
const int size=7;
int CQueue[size],front=-1,rear=-1;
void main()
{ clrscr();
int Item,res,ch;
char cont;
do
{ clrscr();
cout<<"\n\t\t\t Circular Queue Menu\n";
cout<<"\n\t\t\t\t1. Insert\n";
cout<<"\t\t\t\t2. Delete\n";
cout<<"\t\t\t\t3. Display\n";
cout<<"\t\t\t\t4. Exit\n";
cout<<"\n\n\t\t Enter your choice: ";
re:
cin>>ch;
switch(ch)
{ case 1: cout<<"\n\t\tEnter ITEM for insertion: ";
cin>>Item;
res=insert_in_CQ(CQueue,Item);
if(res==-1) cout<<"\n\t\t\tOVERFLOW!!\n";
else
{ cout<<"\nNow the Circular Queue is: \n";
display(CQueue,front,rear);
}
break;
case 2:Item=del_in_CQ(CQueue);
cout<<"\n\t\tElement deleted is: "<<Item<<endl;
276 | P a g e
display(CQueue,front,rear);
break;
case 3:display(CQueue,front,rear);
break;
case 4:cout<<"\n\n\t\t\t\tEXIT PROGRAM";
goto end;
default: cout<<"\n\t\t\t\tINVALID CHOICE";
cout<<"\n\n\t\tReenter choice: ";
goto re;
}
cout<<"\nDo you want to continue? (y or n) ";
cin>>cont;
}while((cont=='y')||(cont=='Y'));
end:
getch();
}
int insert_in_CQ(int CQueue[],int ele) //function to insert element
{ if((front==0&&rear==size-1)||(front==rear+1))
return-1;
else if(rear==-1)front=rear=0;
else if(rear==size-1)rear=0;
else rear++;
CQueue[rear]=ele;
return 0;
}
void display(int CQueue[],int front,int rear)
{ int i=0;
cout<<"\nCir_Queue:\n"
<<"(front shown as >>> ,rear as <<< AND free space as - )\n\n";
if(front==-1) return;
if(rear>=front)
{ for(i=0;i<front;i++)cout<<" - ";
cout<<" >>> ";
for(i=front;i<rear;i++) cout<<CQueue[i]<<" <- ";
cout<<CQueue[rear]<<" <<< "<<endl;
}
else
{ for(i=0;i<rear;i++)cout<<CQueue[i]<<" <- ";
cout<<CQueue[rear]<<" <<< ";
for(;i<front;i++)cout<<" - ";
cout<<" >>> ";
for(i=front;i<size;i++) cout<<CQueue[i]<<" <- ";
cout<<"\n\t...Wrap around...";
}
}
int del_in_CQ(int CQueue[]) //function to delete element
{ int ret;
277 | P a g e
if(front==-1)return-1;
else
{ ret=CQueue[front];
if(front==rear) front=rear=-1;
else if(front==size-1)
front=0; //wrap around
else front++;
}
return ret;
}

278 | P a g e
OUTPUT:

279 | P a g e
280 | P a g e
SQL

281 | P a g e
/* Write SQL commands for (a) to (f) on the basis of teacher relation given below:

Relation teacher

No. Name. Age Dept. Date of Salary Sex


join
1. Jugal 34 Computer 10/01/97 12000 M
2. Sharmila 31 History 24/03/98 20000 F
3. Sandeep 32 Math 12/12/96 30000 M
4. Sangeeta 35 History 01/07/99 40000 F
5. Rakesh 42 Math 05/09/97 25000 M
6. Shyam 50 History 27/06/98 30000 M
7. Shiv om 44 Computer 25/02/97 21000 M
8. Shalakha 33 Math’s 31/07/97 20000 F

(a) To show all information about the teachers of the history dept.

(b) To list the names of the female teachers who are in the Hindi Dept.

(c) To list all the teachers with their Date of joining in ascending order.

(d) To display teacher’s Name, Fee, Age for Male teachers only.

(e) To count the number of teachers with Age<23.

(f) To insert a new row in the TEACHER table .*/

a. SELECT * FROM teacher WHERE Dept. = 'History';


b. SELECT Name. FROM teacher WHERE Sex='F' AND Dept.='Hindi';
c. SELECT * FROM teacher ORDER BY Date of join;
d. SELECT Name,, Fee, Age FROM teacher WHERE Sex='M';
e. SELECT COUNT(*) FROM teacher WHERE Age<23;
f. INSERT INTO teacher VALUES (9,'Zaheer', 'Computer', '12/03/97',230,'M');

282 | P a g e
OUTPUT:

283 | P a g e
284 | P a g e
/* Consider the following tables WORKERS and DESIG.Write the SQL commands for the
statements (i) to (v)
WORKERS
W-ID FIRST NAME LAST NAME ADDRESS CITY
102 Sam Tones 33 Elm St. Paris
105 Sarah Ackerman 440 U.S. 110 New York
144 Manila Sengupta 24 Friends New Delhi
Street
210 George Smith 83 First Street Howard
255 Mary Jones 842 Vine Ave. Losantiville
300 Robert Samuel 9 Fifth cross Washington
335 Henry Williams 12 Moore Boston
Street
403 Ronny Lee 121Harrison New York
Street
451 Pat Thompson 11 Red road Paris

DESIG
W_ID SALARY BENEFITS DESIGNATION
102 75000 15000 Manager
105 85000 25000 Director
144 70000 15000 Manager
210 75000 12500 Manager
255 50000 12000 Clerk
300 45000 10000 Clerk
335 40000 10000 Clerk
403 32000 7500 Salesman
451 28000 7500 Salesman

(i) To display W_ID first name, Address and City Of all the employees
living in New York from the table workers.
(ii) To display the contents of WORKERS table in ascending order of
LASTNAME.
(iii) To display the First name, last name and total salary of all clerks
from the tables WORKERS and DESIG ,where Total Salary is calculated as
salary + benefits.
(iv) To display the minimum salary among mangers and clerks from the
table DESIG
(v) To display firstname, lastname and their designation who have their
salaries above 60000.*/

285 | P a g e
(i) SELECT W_ID, first name, Address , City
FROM WORKERS
WHERE City=’NEW YORK’;
(ii) SELECT * FROM WORKERS
ORDER BY LASTNAME;
(iii) SELECT FIRST NAME,LAST NAME,SALARY+BENEFITS
FROM WORKERS,DESIG
WHERE WORKERS.W_ID=DESIG.W_ID;
(iv) SELECT MIN(SALARY)
FROM DESIG
WHERE DESIGNATION=’CLERK’OR DESIGNATION=’MANAGER’;
(v) SELECT FIRST NAME,LAST NAME, DESIGNATION
FROM WORKERS,DESIG
WHERE DESIG.SALARY>60000 AND WORKERS.W_ID=DESIG.W_ID;

286 | P a g e
OUTPUT:

287 | P a g e
288 | P a g e
289 | P a g e
290 | P a g e
/* Consider the following table CONSIGNOR and CONSIGNEE. Write SQL commands
for following statements.
TABLE : CONSIGNOR
CnorID CnorName CnorAddress City

ND01 R Singhal Park avenue New Delhi

ND02 Amit Kumar 123, Palm Avenue New Delhi

MU15 R Kohli 5/A South Park Mumbai

MU50 S Kaur 27-K, westend Mumbai

TABLE : CONSIGNEE
CneeID CnrID CneerName CneeAddress City

MU05 ND01 Rahul Kishore ABC enclave Mumbai


ND08 ND02 P Dhingra 16/J, Moore enclave New Delhi
KO09 MU15 A P Roy 2A, Central Avenue Kolkata

MU32 ND02 S Mittal P 245, AB Colony Mumbai


ND48 MU50 B P Jain 13, Block D, A Vihar New delhi

1. To display names of all CONSIGNOR from MUMBAI.


2. To display the CneeName, CnorName where consignor city is same as
consignee city.
3. To display consignee detalils in ascending order of CneeName.
4. To display distinct city from consignor.
5. To display CneeName and CnorName Where city is Mumbai.*?

1. SELECT * FROM CONSIGNER WHERE City=”Mumbai”;


2. SELECT CneeName, CnoName FROM CONSIGNEE,CONSIGNOR WHERE
CONSIGNOR .City=CONSIGNEE .City;
3. SELECT * FROM CONSIGNEE ORDER BY CneeName;
4. SELECT DISTINCT City FROM CONSIGNER;
5. SELECT CneeName, CnorName FROM CONSIGNEE WHERE City =”Mumbai“;

291 | P a g e
OUTPUT :

292 | P a g e
293 | P a g e
294 | P a g e
295 | P a g e
296 | P a g e
/*Study the following tables FLIGHTS and FARES and write the SQL commands for the
questions that follow
FLIGHTS
FL_NO. STARTING ENDING NO_FLIGHTS NO_STOPS

IC301 MUMBAI DELHI 8 0

IC799 BANGLORE DELHI 2 1

MC101 INDORE MUMBAI 3 0

IC302 DELHI MUMBAI 8 0

AM812 KANPUR BANGALORE 3 1

IC899 MUMBAI KOCHI 1 4


AM501 DELHI TRIVANDRUM 1 5

MU499 MUMBAI MADRAS 3 3

IC701 DELHI AHEMDABAD 4 0

FARES

FL_NO AIRLINES FARE TAX

IC701 INDIAN AIRLINES 6500 10

MU499 SAHARA 9400 5

AM501 JET AIRWAYS 13450 8

IC899 INDIAN AIRLINES 8300 4

IC302 INDIAN AIRLINES 4300 10

IC799 INDIAN AIRLINES 10500 10

MC101 DECCAN AIRLINES 3500 4

Q1: Display FL_NO and NO_FLIGHTS from “KANPUR” to “BANGALORE”.


Q2: Display the total fare collected by INDIAN AIRLINES
Q3: Display the FL_NO and FARE to be paid for the flights from DELHI to MUMBAI

297 | P a g e
Q4: Display STARTING, ENDING, AIRLINES and FARE from FLIGHTS and FARES
where TAX greater than 5
Q5: Display FL_NO and DISTINCT AIRLINES from FLIGHTS and FARES*/

1. SELECT FLNO, NOFLIGHTS FROM FLIGHTS


WHERE START=’KANPUR’ AND END =’BANGALORE’;
2. SELECT SUM(FARE) AS ‘TOTAL FARE’ FROM FARES
WHERE AIRLINES = ‘INDIAN AIRLINES’;
3. SELECT FLIGHTS.FLNO, FARES.FARE FROM FLIGHTS, FARES
WHERE FLIGHTS.START=’DELHI’ AND FLIGHTS.END=’MUMBAI’ AND
FLIGHTS.FLNO=FARES.FLNO;
4. SELECT FLIGHTS.START, FLIGHTS.END, FARES.AIRLINES, FARES.FARE
FROM FLIGHTS, FARES
WHERE FARES.TAX>5 AND FLIGHTS.FLNO=FARES.FLNO;
5. SELECT FLIGHTS.FLNO, DISTINCT AIRLINES
FROM FLIGHTS, FARES
WHERE FLIGHTS.FLNO=FARES.FLNO;

298 | P a g e
OUTPUT:

299 | P a g e
300 | P a g e
301 | P a g e
302 | P a g e
303 | P a g e
This Program File haS been completed with the
help of :

 Computer science with C++ class XI by Sumita


arora

 Computer science with C++ class XII by


Sumita arora

You might also like