Beruflich Dokumente
Kultur Dokumente
Name: HEMONESH
Class: XII sec: A
Session: 2019-2020
Board roll no.: _______________
Hardware and Software
Requirements
HARDWARE:
Processor : Intel(R) Core™ i5 @ 1.80 GHz 2.80
GHz
Installed Memory (RAM): 8.00 GB
System Type: 64-bit Operating System, x64 based
processor
Hard Disk Drive(HDD) : No
Solid State Drive(SSD) : 256 GB
SOFTWARE:
System: Mac OS Catalina
Software used: Turbo C++ and Pages
LIST OF PROGRAMS
STRUCTURES
(1)Q1. 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
4. Display the name and the prize amount of the biggest prize amount winner
FUNCTION OVERLOADING
(3)Q 1.Write a program to illustrate working of function overloading. Calculate interest
amount using function overloading.
(4)Q 2.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.
(6)Q 2.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’
(7)Q 3. 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:
a. Read a string
b. Display the string
c. Reverse the string
d. Copy the string into an empty string
e. Concatenate two strings
(8)Q 4. 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
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 and less than 40 2
Equal to 40 or more than 40 3
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.
INHERITANCE
(11)Q 1.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.
(13)Q 2.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 .
(14)Q 3. 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
(15)Q4. 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.
ii. Reads the objects of class from binary file and display 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.
(16)Q 5 . 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( );
};
(17)Q 6. 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.
(18)Q 7. 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
(19)Q 8. 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.
(20)Q9. 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.
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.
(33)Q4. Write a program in C++ to add, delete, search and display an element in a linked
queue.
(34)Q5. Write a complete menu driven program in C++ for the implementation of linked
stack.
(35)Q6. Write an interactive menu driven C++ program to implement circular queue using an
array consisting of integer data values.
SQL
(36)Q1. 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
joining
1. Jugal 34 Computer 10/01/97 12000 M
(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 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 .
(37)Q2 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 Street New Delhi
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 Street Boston
403 Ronny Lee 121Harrison Street New York
451 Pat Thompson 11 Red road Paris
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.
TABLE : CONSIGNEE
CneeID CnrID CneerName CneeAddress city
FARES
1 STRUCTURES
2 FUNCTION OVERLOADING
5 INHERITANCE
9 SQL
· Relation Teacher
void main()
{
clrscr();
char ch;
do
{
cout<<"Enter the number of competitions conducted: ";
cin>>N; //the no of objects to be created
fillarr(); //calling all functions
display();
max();
winner();
cout<<"\n Do you wish to continue ? (Press y -yes ,n-no)? ";
cin>>ch;
}
while((ch=='y')||(ch=='Y'));
cout<<"\n THANKS ;') ";
getch();
}
void fillarr() //function to take all details from the user
{
for(int i =0; i<N;i++)
{
else
continue;
}
if(ch=='n')
cout<<"N.A.";
}
PROGRAM TO IMPLEMENT A STRUCTURE ELECTRIC
//Program to calculate bill in a structure ELECTRIC
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct ELECTRIC //defining structure ELECTRIC
{
int CUST_No;
char Name[20];
long double unitsconsumed;
float bill;
};
void Sort(ELECTRIC e[],int n); //function for sorting
void Cbill(ELECTRIC e[],int n); //function for calculating bill
void main()
{
clrscr();
int n,i;
ELECTRIC e[20];
cout<<"\n Please enter the number of customers you want to enter details for ";
cin>>n; //Inputting information
for(i=0;i<n;i++)
{
cout<<"\n Please enter the following details for Customer ";
cout<<"\n \n Please enter the customer number: ";
cin>>e[i].CUST_No;
cout<<"\n Please enter the customer name: ";
gets(e[i].Name);
cout<<"\n Please enter the units consumed: ";
cin>>e[i].unitsconsumed;
}
char ch;
goodname: //User driven menu
cout<<"\n Please choose from the following menu ";
cout<<"\n 1) Sorting records ";
cout<<"\n 2) Calculating bill ";
cout<<"\n 3) Sorting records and Calculating bills ";
cout<<"\n 4) Exit ";
cin>>ch;
switch(ch) //Using switch case
{
case'1':
Sort(e,n);
char ch2; //Invoking functions
cout<<"\n Do you want to continue using the program...Press y for yes and n
for no ";
cin>>ch2;
if((ch2=='y')||(ch2=='Y'))
goto goodname;
else
goto goodname1;
break;
case'2':
Cbill(e,n);
char ch3;
cout<<"\n Do you want to continue using the program...Press y for yes and n
for no ";
cin>>ch3;
if((ch3=='y')||(ch3=='Y'))
goto goodname;
else
goto goodname1;
break;
case'3':
Sort(e,n);
Cbill(e,n);
char ch4;
cout<<"\n Do you want to continue using the program...Press y for yes and n
for no ";
cin>>ch4;
if((ch4=='y')||(ch4=='Y'))
goto goodname;
else
goto goodname1;
break;
case'4':
goodname1:
char ch1;
cout<<"\n Are you sure you want to exit the program ? Press Y for yes or any
key for no ";
cin>>ch1;
if((ch1=='y')||(ch1=='Y'))
exit(0);
else
{
cout<<"\n Welcome Back!! " ;
goto goodname;
}
break;
default:
cout<<"\n Please enter a valid choice ";
goto goodname;
}
getch();
}
void Sort(ELECTRIC e[], int n)
{
ELECTRIC t; //Sorting via bubble sort
for(int i=0;i<n;i++)
{
for(int j=0;j<(n-1)-i;j++)
{
if(e[j].CUST_No>e[j+1].CUST_No)
{
t=e[j+1];
e[j+1]=e[j];
e[j]=t;
}
}
}
cout<<"\n Sorted records are ";
for(i=0;i<n;i++)
{ //Displaying result
cout<<"\n Customer number: ";
cout<<e[i].CUST_No;
cout<<"\n Customer name: ";
puts(e[i].Name);
cout<<" Units consumed: ";
cout<<e[i].unitsconsumed;
}
}
void Cbill(ELECTRIC e[],int n)
{
cout<<"\n Calculating Bill "; //Calcutaion of bill
for(int i=0;i<n;i++)
{
long double amt=0.0;
if(e[i].unitsconsumed<=100)
{
amt= amt+(e[i].unitsconsumed*0.40);
}
if((e[i].unitsconsumed>100)&&(e[i].unitsconsumed<=300))
{
amt= amt+(100*0.40);
amt= amt+((e[i].unitsconsumed-100)*0.50);
}
if((e[i].unitsconsumed>300)&&(e[i].unitsconsumed<=600))
{
amt= amt+(100*0.40);
amt= amt+(200*0.50);
amt= amt+((e[i].unitsconsumed-300)*0.75);
}
if((e[i].unitsconsumed>600)&&(e[i].unitsconsumed<=1000))
{
amt= amt+(100*0.40);
amt= amt+(200*0.50);
amt= amt+(300*0.75);
amt= amt+((e[i].unitsconsumed-600)*1.00);
}
if(e[i].unitsconsumed>1000)
{
amt= amt+(100*0.40);
amt= amt+(200*0.50);
amt= amt+(300*0.75);
amt= amt+(400*1.00);
amt= amt+((e[i].unitsconsumed-1000)*1.50);
}
e[i].bill=amt;
}
cout<<"\n The Details and the bill for customer is ";
for(i=0;i<n;i++) //Displaying result
{
cout<<"\n \n Details for Customer is: ";
cout<<"\n Customer number: ";
cout<<e[i].CUST_No;
cout<<"\n Customer name: ";
puts(e[i].Name);
cout<<" Units consumed: ";
cout<<e[i].unitsconsumed;
cout<<"\n Total Amount Payable(in Rs) ";
cout<<e[i].bill;
}
}
TO CALCULATE SIMPLE INTEREST
#include<iostream.h> //invoking header files
#include<conio.h>
#include<ctype.h>
#include<process.h>
void calci(long double p,int r,int t); //all 3 parameters given by user
void calci(long double p,int t); //only principal and time period given
void calci(long double p); //only principal given
void main()
{
clrscr();
int ti,ra;
long double pr; //declaring actual parameters
char ch, ch1; //for switch and do-while respectively
do
{
cout<<"CALCULATION OF SIMPLE INTEREST\n";
cout<<"\nChoose to enter any of the following:"
<<"\n1.Only Principal Amount"
<<"\n2.Principal and time period"
<<"\n3.Principal, interest and Time Period"
<<"\n4.Exiting the program.";
cin>>ch;
switch(ch)
{
case '1': label1:
cout<<"\n Enter Principal Amount: "; //PA entered
cin>>pr;
if((pr<0)||(isalpha(pr)))
{
cout<<"\n Please enter a valid input ";
goto label1;
}
calci(pr);
break;
case '2': label2:
cout<<"\n Enter the Principal Amount: "; //PA and Time
Period entered
cin>>pr;
cout<<"\n Enter the Time Period: ";
cin>>ti;
if((ti<0)||(pr<0)||(isalpha(pr))||(isalpha(ti)))
{
cout<<"\n Please enter a valid input ";
goto label2;
}
calci(pr,ti);
break;
case '3': label3:
cout<<"\n Enter the Principal Amount: "; //all three
parameters entered
cin>>pr;
cout<<"\n Enter the Time Period: ";
cin>>ti;
cout<<"\n Enter Rate of Interest: ";
cin>>ra;
if((pr<0)||(ti<0)||(ra<0)||(isalpha(ti)||(isalpha(pr))||(isalpha(ra))))
{
cout<<"\n Please enter a valid input ";
goto label3;
}
calci(pr,ra,ti);
break;
case '4':cout<<"\n Thank you !";
exit(0);
break;
default: cout<<"\n INVALID"; //incase user enters none of 1,2,3
break;
}
cout<<"\nPress Y to continue and N to leave: ";//condition for begining again
cin>>ch1;
}
while((ch1=='y')||(ch1=='Y'));
cout<<"\nThanks a lot"; //do while ends
getch();
}
void calci(long double p,int r,int t)
{
long double si;
si=(p*r*t)/100; //by formula of SI
long double amt=si+p;
cout<<"\n The Simple interest : "<<si;
cout<<"\n Amount : "<<amt;
}
void calci(long double p,int r)
{
long double si,t=10;
si=(p*r*t)/100;
long double amt=si+p;
cout<<"\n The Simple interest( default rate:10% ): "<<si;
cout<<"\n Amount : "<<amt;
}
void calci(long double p)
{
long double si,r=10,t=10;
si=(p*r*t)/100;
long double amt=si+p;
cout<<"\n The Simple interest( default rate:10% , default time period:10yrs ): "<<si;
cout<<"\n Amount : "<<amt;
}
PRORGRAM TO CALCULATE AREA FOR VARIOUS FIGURES
case 2:
label2:
cout<<"\n Please enter your account number ";
cin>>ac;
i=Lsearch(b,ac,n);
if(i>=0)
b[i].Display();
else
goto label2;
char ch2;
cout<<"\n Do you want to continue using the program...Press y for yes and n
for no ";
cin>>ch2;
if((ch2=='y')||(ch2=='Y'))
goto goodname;
else
goto goodname1;
break;
case 3:
goodname1:
char ch3;
cout<<"\n Are you sure you want to exit the program ? Press Y for yes or any
key for no ";
cin>>ch3;
if((ch3=='y')||(ch3=='Y'))
exit(0);
else
{
cout<<"\n Welcome Back!! " ;
goto goodname;
}
break;
default:
cout<<"\n Please enter a valid choice ";
goto goodname;
}
getch();
}
int Lsearch( Bank b[], int ac, int n) //Function to search in an array
{
int x;
for(int i=0;i<n;i++)
{
if(b[i].Retbno()==ac)
x=i;
}
if((x<n)&&(x>=0))
{
return x;
}
else
{cout<<"\n Please enter a valid account number ";
return -1;
}
}
PROGRAM TO IMPLEMENT RESULT PREPARATION USING
CLASSES
//Program to prepare system for calculating result
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<stdio.h>
class RESULT //Defining class
{
int rollno;
char name[20];
float marks_s1;
float marks_s2;
float marks_s3;
float avgmarks;
char grade;
public:
RESULT()
{
rollno=1;
strcpy("ABC",name);
marks_s1=0;
marks_s2=0;
marks_s3=0;
avgmarks=0;
}
void READ();
void SHOW();
void CAL();
};
void RESULT::READ() //Function to read
{
jump:
cout<<"\n Roll no: ";
cin>>rollno;
if(rollno<=0)
{
cout<<"\n Please enter a valid Roll no ";
goto jump;
}
cout<<"\n Name: ";
gets(name);
label:
cout<<" \n Marks in three subjects(each out of 100): ";
cin>>marks_s1>>marks_s2>>marks_s3;
if((marks_s1>100)||(marks_s2>100)||(marks_s3>100))
{
cout<<"\n Please enter all marks less than 100 ";
goto label;
}
CAL();
}
void RESULT::SHOW() //Function to show
{
cout<<"\n Roll no: "<<rollno;
cout<<"\n Name: ";
puts(name);
cout<<" Marks in three subjects are: "<<marks_s1<<" \t "<<marks_s2<<" \t
"<<marks_s3;
cout<<"\n Percentage Marks are: "<<avgmarks;
cout<<"\n Grade: "<<grade;
}
void RESULT::CAL() //Calculating grade
{
avgmarks=(marks_s1+marks_s2+marks_s3)/3;
if(avgmarks<50)
grade='E';
if((avgmarks>=50)&&(avgmarks<60))
grade='D';
if((avgmarks>=60)&&(avgmarks<75))
grade='C';
if((avgmarks>=75)&&(avgmarks<90))
grade='B';
if((avgmarks>=90)&&(avgmarks<100))
grade='A';
}
void main()
{ //Function main
clrscr();
char ch;
do
{
RESULT r[20];
int n;
cout<<"\n Please enter the number of students you want to calculate result for:
";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\n Enter details for student "<<i+1;
r[i].READ();
}
for(i=0;i<n;i++)
{
cout<<"\n Details entered for student "<<i+1<<" is ";
r[i].SHOW();
}
cout<<"\n Do you want to continue ?...Press Y for yes and N for no ";
cin>>ch;
}
while((ch=='y')||(ch=='Y'));
getch();
}
PROGRAM TO PERFORM VARIOUS OPERATION ON A STRING
USING CLASSES
//Program for a string class
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
class String //defining class
{
char ch[100];
public:
String() ////Function to initialize data members
{
strcpy(ch,"NULL");
cout<<"Ctor at work\n";
}
~String()
{
cout<<"\nDtor at work\n";
}
void READ() //Function to read the string
{
cout<<"Please enter the string\n";
gets(ch);
}
void Display() //Function to display the string
{
cout<<"\nThe entered string is:\n";
puts(ch);
}
void Reverse(); //Functions to do operations on the string
void Copy();
void Concat();
};
void String::Reverse() //Function to reverse the string
{
}
void String::Concat() //Function to concatenate two strings
{
char a[100],b[100];
cout<<"Enter second string\n";
gets(a);
int i,j,k;
cout<<"second string is:\n";
puts(a);
for(i=0;ch[i]!='\0';++i)
{
b[i]=ch[i];
}
for(j=0,k=i;a[j]!='\0';++j,++k)
{
b[k]=a[j];
}
b[k]='\0';
cout<<"the concatenated string is:\n";
puts(b);
}
void main()
{
clrscr();
char Z;
int n;
String S; //declaring an object of string
S.READ();
S.Display();
jump:
cout<<"Enter the number for desired operation\n" //asking user to choose an
operation from the menu
<<"1. Reverse a string\n"
<<"2. Copy a string\n"
<<"3. Concatenate two strings\n"
<<"4. Exit\n";
cin>>n;
if(n==1) //if statements to execute operation accordind to user's input
{
String S1=S;
S1.Reverse();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if(n==2)
{
S.Copy();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if (n==3)
{
S.Concat();
cout<<"\nDo you wishto continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if (n==4)
{ tag:
cout<<"\nDo you wish to exit? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
exit(-1);
else
{
cout<<"\nWelcome Back\n";
goto jump;
}
}
else
{
cout<<"Wrong entry, please try again\n"; //Error handling
goto jump;
}
cout<<"\nDo you wish to continue? Press Y for yes\n"; //asking user if they wish to
continue
cin>>Z;
getch();
}
PROGRAM TO PERFORM VARIOUS OPERATION ON A VECTOR
USING CLASSES
#include<iostream.h>
#include<conio.h>
#include<process.h>
class vector //defining class
{
int x;
int ar[10];
public:
void create(); //function to accept values in the vector
void Mod(); //fnction to modify an element
void large(); //function to display largest value
void Small(); //function to display smallest value
void Display(); //function to display entire vector
};
void vector::create()
{ cout<<"\nEnter the no. of elements: ";
cin>>x;
cout<<"Enter the elements: \n";
for(int i=0;i<x;++i)
cin>>ar[i];
}
void vector::Mod()
{
int z,n;
cout<<"\nEnter the element to be modified: ";
cin>>n;
cout<<"\nEnter the value you want to replace with: ";
cin>>z;
for(int q=0;q<x;++q)
{ if(ar[q]==n)
ar[q]=z;
}
}
void vector:: large()
{
int lar=ar[0];
for(int i=0;i<x;++i)
{ if(ar[i]>lar)
lar=ar[i];
}
cout<<"\nThe largest element is:\n";
cout<<lar;
}
void vector::Small()
{
int small=ar[0];
for(int i=0;i<x;i++)
{
if(ar[i]<small)
small=ar[i];
}
cout<<"\nThe smallest element is:\n";
cout<<small;
}
void vector:: Display()
{
cout<<"\nThe vector is:\n";
for(int l=0;l<x;++l)
cout<<ar[l]<<' ';
}
void main()
{ clrscr();
char Z;
int n;
vector V;
V.create(); //declaring an object of vector
jump:
cout<<"Enter the number for desired operation\n" //asking user to choose an
operation from the menu
<<"1. Modify an element\n"
<<"2. To display the largest value in vector\n"
<<"3. To display the smallest value in vector\n"
<<"4. To display the entire vector\n"
<<"5. Exit\n";
cin>>n;
if (n==1) //if statements to execute operation according to user's input
{
V.Mod();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if (n==2)
{
V.large();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if (n==3)
{
V.Small();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if (n==4)
{
V.Display();
cout<<"\nDo you wish to continue? Press Y to continue and any other key to
exit\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
goto jump;
else
goto tag;
}
else if(n==5)
{
tag:
cout<<"\nDo you wish to exit? Press Y to exit and any other key to
continue\n";
cin>>Z;
if((Z=='y')||(Z=='Y'))
exit(-1);
else
{
cout<<"\nWelcome Back\n";
goto jump;
}
}
else //error handling
{
cout<<"Wrong entry, please try again\n"; //Error handling
goto jump;
}
getch();
}
PROGRAM TO IMPLEMENT CLASS TRAVELPLAN
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class TravelPlan //defining class
{
long int PlanCode; //declaring memebers
char Place[100];
int Number_of_travellers;
int Number_of_buses;
public:
TravelPlan() //Function to initialize data members
{
PlanCode=1001;
strcpy(Place,"Agra");
Number_of_travellers=5;
Number_of_buses=1;
cout<<"CTOR at work";
}
~TravelPlan() //Destructor
{
cout<<"Dtor at work\n";
}
void Newplan() //Function to take data from the user
{
cout<<"\nEnter the trip details ";
cout<<"\nEnter your Plan Code:\n";
cin>>PlanCode;
cout<<"Enter your Destination:\n";
gets(Place);
cout<<"\nEnter the number of travellers?\n";
cin>>Number_of_travellers;
Bus(); //Calling Bus
}
void Bus() //Function to calculate No. of buses required for
the trip
{
if(Number_of_travellers<20)
Number_of_buses=1;
else if(Number_of_travellers>=20 && Number_of_travellers<40)
Number_of_buses=2;
else
Number_of_buses=3;
}
void Show() //Showing the data of the trip
{
cout<<"\nThe Plan code is\n"<<PlanCode;
cout<<"\nThe Destination is:\n";
puts(Place);
cout<<"\nThe number of travellers is:\n"<<Number_of_travellers;
cout<<"\nTotal buses are:\t"<<Number_of_buses;
}
};
void main()
{
clrscr();
char ch;
do{
TravelPlan T; // declaring an object of class TravelPlan
T.Newplan();
T.Show();
cout<<"\nDo you wish to run the program again? Press Y for yes and any other key to
exit\n";
cin>>ch;
}
while((ch=='y')||(ch=='Y'));
getch();
}
PROGRAM TO IMPLEMENT A CLASS OUTFIT
//Program to calculate cost of items bought in a clothing store
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
class outfit //Defining a class
{
char ocode[15];
char otype[15];
int osize;
char ofabric[15];
float oprice;
void initprice();
public: //Access Specifier
outfit() //Constructor
{
strcpy(ocode,"Not Initialised");
strcpy(otype,"Not Initialised");
osize=0;
strcpy(ofabric,"Not Initialised");
oprice=0.0;
}
~outfit() //Destructor
{
}
float retprice()
{
return oprice;
}
void input();
void display();
};
void outfit::input() //Member Function
{
int i;
char err1='y',err2='y';
cout<<"Enter code (a string):";
gets(ocode);
t:
cout<<"Enter type:\nJacket\tTrouser\nType:";
gets(otype);
for(i=0;otype[i]!='\0';i++)
{
err1='y';
if((otype[i]<65)||((otype[i]>90)&&(otype[i]<97))||(otype[i]>122))
{
err1='n';
break;
}
}
if((strcmpi(otype,"Jacket")!=0)&&(strcmpi(otype,"Trouser")!=0))
{
cout<<"Invalid string entered.Please enter again.\n";
goto t;
}
if(err1=='n')
{
cout<<"Invalid type entered.Please enter again.\n";
goto t;
}
s:
cout<<"Enter size(28,30,32,34,36):";
cin>>osize;
if((osize!=28)&&(osize!=30)&&(osize!=32)&&(osize!=34)&&(osize!=36))
{
cout<<"Invalid size.Please enter again.\n";
goto s;
}
f:
cout<<"Enter fabric:\nDenim\tAny other\nFabric:";
gets(ofabric);
for(i=0;ofabric[i]!='\0';i++)
{
err2='y';
if((ofabric[i]<65)||((ofabric[i]>90)&&(ofabric[i]<97))||(ofabric[i]>122))
{
err2='n';
break;
}
}
if(err2=='n')
{
cout<<"Invalid fabric entered.Please enter again.\n";
goto f;
}
initprice(); //Function Call
}
void outfit::display() //Member Function
{
cout<<"Code:";
puts(ocode);
cout<<"Type:";
puts(otype);
cout<<"Size:"<<osize;
cout<<"\nFabric:";
puts(ofabric);
cout<<"Price of the entered fabric:";
cout<<oprice;
}
void outfit::initprice()
{
if((strcmpi(ofabric,"DENIM")==0))
{
if((strcmpi(otype,"trouser")==0))
{
oprice=1500;
}
if((strcmpi(otype,"jacket")==0))
{
oprice=2500;
}
}
else
{
if((strcmpi(otype,"trouser")==0))
{
oprice=1125;
}
if((strcmpi(otype,"jacket")==0))
{
oprice=1875;
}
}
}
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
int num,totalcost;
outfit o1[10];
again:
cout<<"Enter the no. of items to bought (max10):";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Please enter the details of item "<<i+1<<":\n";
o1[i].input(); //Calling function to read data
}
for(i=0;i<num;i++)
{
totalcost+=o1[i].retprice();
cout<<"\nDisplaying Data for item "<<i+1<<":\n";
o1[i].display(); //Function call to display data
}
cout<<"\nThe total cost is:"<<totalcost;
label: //goto label
cout<<"\nDo you wish to continue? Enter Y for Yes and N for No.";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"Invalid character entered.Please enter again.\n";
goto label;
}
}while((ch=='y')||(ch=='Y'));
getch();
}
PROGRAM TO IMPLEMENT INHERITANCE OF CLASSES
//Program to inherit classes
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
class Employee
{
char Empno;
char eName[20];
char eAddress[30];
public: //Access Specifier
void eRead();
void eShow();
Employee() //Constructor
{
Empno='0';
strcpy(eName,"Not Assigned");
strcpy(eAddress,"Not Assigned");
}
};
void Employee::eRead() //Member Function
{
char err1='y';
empno:
cout<<"Employee number:";
cin>>Empno;
if((Empno<48)||(Empno>57))
{
cout<<"Invalid employee number.Please enter again.\n";
goto empno;
}
ename:
cout<<"Name:";
gets(eName);
for(int i=0;eName[i]!='\0';i++)
{
err1='y';
if((eName[i]<65)||((eName[i]>90)&&(eName[i]<97))||(eName[i]>122))
{
cout<<"Invalid string entered.Please enter again.\n";
err1='n';
break;
}
}
if(err1=='n')
{
goto ename;
}
cout<<"Address:";
gets(eAddress);
}
void Employee::eShow()
{
cout<<"Employee Number:"<<Empno;
cout<<"\nName:";
puts(eName);
cout<<"Address:";
puts(eAddress);
}
class Manager:public Employee //Inheriting publicly from Employee
{
char mName[20];
char mDepartment[30];
char mAddress[20];
public:
int empnum;
void mRead();
void mShow();
Manager()
{
strcpy(mName,"Not Assigned");
strcpy(mDepartment,"Not Assigned");
strcpy(mAddress,"Not Assigned");
}
};
void Manager::mRead()
{
cout<<"Name:";
gets(mName);
cout<<"Department:";
gets(mDepartment);
cout<<"Address:";
gets(mAddress);
cout<<"Number of employees:";
cin>>empnum;
}
void Manager::mShow() //Function to display data
{
cout<<"Name:";
puts(mName);
cout<<"Department:";
puts(mDepartment);
cout<<"Address:";
puts(mAddress);
cout<<"Number of employees:"<<empnum;
}
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
Manager M[10];
int i,j,mannum;
no:
cout<<"Please enter the number of Managers in the company:";
cin>>mannum;
if(isdigit(mannum)!=0)
{
cout<<"Invalid character.Please enter again.";
goto no;
}
for(i=0;i<mannum;i++)
{
cout<<"Please enter the details of manager "<<i+1<<":\n";
M[i].mRead(); //Function Cally
for(j=0;j<M[i].empnum;j++)
{
cout<<"Please enter the details of employee "<<j+1<<":\n";
M[i].eRead();
}
}
cout<<"\nDisplaying the data:\n";
for(i=0;i<mannum;i++)
{
cout<<"Manager "<<i+1<<":\n";
M[i].mShow();
for(j=0;j<M[i].empnum;j++)
{
cout<<"\nData of employee "<<j+1<<":\n";
M[i].eShow();
}
}
rep: //goto label
cout<<"Do you wish to continue? Enter y for yes and n for no:";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"Invalid character entered.Please enter again.\n";
goto rep;
}
}while((ch=='y')||(ch=='Y'));
getch();
PROGRAM TO PERFORM VARIOUS OPERATIONS ON TEXT FILE
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void createfile(); //Function Prototype
void displayfile();
void vowelcheck();
void spacecount();
void alphacount();
void wordscount();
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
char choice;
cout<<"Please choose among the following:\n"<<
"1.To write to the file\n"<<
"2.To read from the file\n"<<
"3.To count the number of vowels in the file\n"<<
"4.To count the number of spaces in the file\n"<<
"5.To count the number of alphabets in the file\n"<<
"6.To count the number of words in the file\n"<<
"7.To exit\n";
again:
cin>>choice;
switch(choice)
{
case '1': createfile(); //Function Call
break;
case '2': displayfile();
break;
case '3': vowelcheck();
break;
case '4': spacecount();
break;
case '5': alphacount();
break;
case '6': wordscount();
break;
case '7': cout<<"Thank you for using the program!";
exit(0);
break;
default : cout<<"Invalid character entered. Please enter again.";
goto again;
}
label:
cout<<"\nDo you wish to continue? Enter Y for Yes and N for No.";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
goto label; //Jump statement
}while((ch=='y')||(ch=='Y'));
getch();
}
void createfile() //Function Definition
{
char str[20];
ofstream fout("text.txt");
cout<<"Please enter the data:";
gets(str);
fout<<str; //Writing in the file
cout<<"Written succesfully to the file.";
fout.close();
}
void displayfile()
{
char str[20];
ifstream fin("text.txt");
if(!fin) //Checking the existence of the file
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin>>str;
cout<<str;
}
fin.close(); //Closing the file
}
void vowelcheck()
{
char ch;
int count=0;
ifstream fin;
fin.open("text.txt");
if(!fin) //Checking the existence of a file
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(ch); //Reading from the file
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': count++;
break;
}
}
cout<<"The no. of vowels are :"<<count;
fin.close(); //Closing the file
}
void spacecount()
{
char ch;
int count=0;
ifstream fin;
fin.open("text.txt"); //Opening the file
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(ch); //Reading character from file
if(ch==' ')
count++;
}
cout<<"The no. of spaces are :"<<count;
fin.close(); //Closing the file
}
void alphacount()
{
char ch;
int count=0;
ifstream fin;
fin.open("text.txt"); //Opening the file
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(ch); //Reading character from the file
if(((ch>=65)&&(ch<=90))||((ch>=97)&&(ch<=122)))
count++;
}
cout<<"The no. of alphabets are :"<<count;
fin.close();
}
void wordscount()
{
char ch;
int count=1; //Initialising count
ifstream fin;
fin.open("text.txt");
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
count++;
}
cout<<"The no. of words are :"<<count; //Displaying the result
fin.close();
PROGRAM TO PERFORM VARIOUS OPERATIONS ON BINARY
FILE
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
#include<ctype.h>
class Phonelist //Declaring the class
{
char Name[20];
char Address[30];
char Areacode[5];
char PhoneNum[15];
public:
Phonelist() //Constructor
{
strcpy(Name,"NOT ASSIGNED");
strcpy(Address,"NOT ASSIGNED");
strcpy(Areacode,"DEL");
strcpy(PhoneNum,"Null");
}
~Phonelist() //Destructor
{
cout<<"Thank you for using the program.";
}
void Register();
void Show(); //Member Function
void Transfer();
int CheckCode(char AC[])
{
return strcmp(Areacode,AC);
}
}phone;
void Phonelist::Register()
{
cout<<"Please enter the details of the entry:\n";
name:
cout<<"Name: ";
gets(Name);
for(int i=0;Name[i]!='\0';i++)
{
if(!isalpha(Name[i]))
{
cout<<"Please enter a valid name\n";
goto name;
}
}
cout<<"Address: ";
gets(Address);
arcode:
cout<<"Area code (3 character string): ";
gets(Areacode);
for(i=0;Areacode[i]!='\0';i++)
{
if(!isalpha(Areacode[i]))
{
cout<<"Please enter a valid area code\n";
goto arcode;
}
}
num:
cout<<"Phone number (10 digit): ";
cin>>PhoneNum;
for(i=0;i<10;i++)
{
if(!isdigit(PhoneNum[i])||(PhoneNum[10]!='\0'))
{
cout<<"Please enter a valid phone number\n";
goto num;
}
}
fstream fout;
fout.open("PHONE.DAT",ios::binary|ios::out|ios::app);
if(!fout) //Checking for file existence
{
cout<<"\nThe file does not exist";
exit(-1);
}
fout.write((char*)&phone,sizeof(phone));//Writing to the file
fout.close(); //Closing the file
}
void Phonelist::Show()
{
cout<<"\nDisplaying the details: ";
cout<<"\nName: ";
puts(Name);
cout<<"Address: ";
puts(Address);
cout<<"Area code: "<<Areacode;
cout<<"\nPhone number: "<<PhoneNum<<"\n";
}
void Phonelist::Transfer()
{
fstream fin;
fin.open("PHONE.DAT",ios::binary|ios::in);
fstream fo;
fo.open("NEW_PHONE.DAT",ios::binary|ios::out);
if(!fin)
{
cout<<"\nThe file does not exist";
exit(-1);
}
while(!fin.eof())
{
fin.read((char*)&phone,sizeof(phone));
if(CheckCode("DEL")==0)
{
fo.write((char*)&phone,sizeof(phone));
}
}
fo.close();
fin.close();
}
void new_display()
{
fstream f("NEW_PHONE.dat",ios::in);
if(!f)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!f.eof())
{
f.read((char*)&phone,sizeof(phone));
phone.Show();
}
f.close();
}
void main()
{
char ch,op;
menu: //goto label
clrscr();
cout<<"Please select the option you want to choose:\n";
cout<<"1. Enter records to the file\n2. Display records from the file\n"<<
"3. To display the records of the file having areacode 'DEL'\n4. Exit\n";
cin>>op;
switch(op)
{
case '1': phone.Register(); //Function Call
phone.Transfer();
break;
case '2': ifstream fin("PHONE.dat",ios::binary);
if(!fin)
{cout<<"The file does not exist.”;
exit(-1);
}
while(!fin.eof())
{
fin.read((char*)&phone,sizeof(phone));
phone.Show();
}
fin.close();
break;
case '3': cout<<"Displaying the records:\n";
new_display();
break;
case '4': cout<<"Thank you for using the program!";
exit(0);
break;
default: cout<<"\nPlease enter a valid option.\n";
goto menu;
}
exit:
cout<<"\nDo you want to continue?[y/n]: ";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"\nPlease enter a valid option";
goto exit;
}
if((ch=='y')||(ch=='Y')) //For multiple executions
goto menu; else
cout<<"\nThank you for using the program";getch();}
PROGRAM TO PERFORM VARIOUS OPERATIONS ON BINARY
FILE APPLICANT.DAT
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<ctype.h>
#include<process.h>
class Applicant //Declaring the class
{
char A_Rno[10];
char A_Name[30];
int A_Score;
public:
void Enrol() //Inline Member Function
{
cout<<"Please enter the roll no:";
cin>>A_Rno;
cout<<"Please enter the name:";
gets(A_Name);
cout<<"Please enter the score:";
cin>>A_Score;
}
void Status()
{
cout<<"\nRoll no:"<<A_Rno;
cout<<"\nName:";
puts(A_Name);
cout<<"Score:"<<setw(3)<<A_Score;
}
int return_score()
{
return A_Score;
}
}A1,A[10];
void write(); //Function Prototypes
void read();
void select();
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
char choice;
cout<<"Please choose among the following:\n"<<
"1.To write data to the file\n"<<
"2.To read data from the file\n"<<
"3.To display details of students who have scored 70+\n"<<
"4.To exit\n";
label: //goto label
cin>>choice;
switch(choice)
{
case '1': write();
break;
case '2': read(); //Function Call
break;
case '3': select();
break;
case '4': exit(0);
break;
default : cout<<"Invaild character entered.Please enter again.\n";
goto label;
}
again:
cout<<"\nDo you wish to continue ? Enter Y for Yes and N for No.";
cin>>ch;
if((toupper(ch)!='Y')&&(toupper(ch)!='N'))
{
cout<<"Invalid character entered.Please enter again.\n";
goto again;
}
}while(toupper(ch)=='Y');
getch();
}
void write() //Function Definition
{
int i,num;
ofstream fout("APPLY.DAT",ios::binary|ios::app); //Opening the file
cout<<"Please enter the number of students whose details you want to enter:";
cin>>num;
for(i=0;i<num;i++)
{
cout<<"Please enter the details of student"<<i+1<<":\n";
A[i].Enrol();
fout.write((char*)&A[i],sizeof(A[i]));
}
fout.close(); //Closing the file
}
void read()
{
ifstream fin("APPLY.DAT",ios::binary);
if(!fin) //Checking the existence of file
{
cout<<"The file does not exist.";
exit(-1);
}
cout<<"\nDisplaying the details:\n";
while(!fin.eof())
{
fin.read((char*)&A1,sizeof(A1));
A1.Status(); //Function Call
}
fin.close();
}
void select() //Function Definition
{
int pos;
fstream f("APPLY.DAT",ios::binary|ios::in);
if(!f)
{
cout<<"The file does not exist.";
exit(-1);
}
cout<<"\nDisplaying the details of students whose score is 70+:\n";
while(!f.eof())
{
f.read((char*)&A1,sizeof(A1)); //Reading from file
if(A1.return_score()>70)
{
A1.Status();
}
}
f.close(); //Closing the file
}
PROGRAM TO PERFORM VARIOUS OPERATIONS ON BINARY
FILE APPL.DAT
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
class Applicant //Declaring a class
{
char A_Rno[10];
char A_Name[20];
int A_Score;
public: //Access specifier
void Read();
void Display();
void Search();
void Modify(); //Member Function
}apl;
void Applicant::Read()
{
cout<<"Please enter all the details: ";
cout<<"\nRoll number: ";
cin>>A_Rno;
cout<<"Name: ";
gets(A_Name);
cout<<"Score: ";
cin>>A_Score;
fstream fout;
fout.open("APPL.DAT",ios::binary|ios::out|ios::app);
if(!fout) //Checking for file existence
{
cout<<"\nThe file does not exist";
exit(-1);
}
fout.write((char*)&apl,sizeof(apl));
fout.close(); //Closing the file
}
void Applicant::Display()
{
fstream fin;
fin.open("APPL.DAT",ios::binary|ios::in);
if(!fin)
{
cout<<"\nThe file does not exist";
exit(-1);
}
cout<<"Displaying the details of the records:\n";
while(!fin.eof())
{ fin.read((char*)&apl,sizeof(apl)); //Reading from the file
cout<<"\nRoll number: "<<A_Rno;
cout<<"\nName: ";
puts(A_Name);
cout<<"Score: "<<A_Score<<"\n";
}
fin.close();
}
void Applicant::Search()
{
char Rno[10],found='n';
fstream fin;
fin.open("APPL.DAT",ios::binary|ios::in); //Opening the file
if(!fin)
{
cout<<"\nThe file does not exist";
exit(-1);
}
cout<<"Enter the roll number of the entry you want to search: ";
gets(Rno);
while(!fin.eof())
{
fin.read((char*)&apl,sizeof(apl)); //Reading from the file
if(strcmpi(Rno,A_Rno)==0)
{
found='y';
cout<<"Displaying the details of the records:\n";
cout<<"\nRoll number: "<<A_Rno;
cout<<"\nName: ";
puts(A_Name);
cout<<"Score: "<<A_Score<<endl;
break;
}
}
if(found=='n')
cout<<"Record not found";
fin.close(); //Closing the file
}
void Applicant::Modify()
{
char Rno[10];
char Name[20];
int Score;
int pos;
char found='n';
fstream fio;
fio.open("APPL.DAT",ios::binary|ios::in|ios::out);
if(!fio)
{
cout<<"\nThe file does not exist";
exit(-1);
}
cout<<"Enter the roll number of the entry you want to modify: ";
gets(Rno);
while(!fio.eof())
{
fio.read((char*)&apl,sizeof(apl));
if(strcmpi(Rno,A_Rno)==0)
{
found='y';
pos=fio.tellg();
cout<<"Enter the new details";
cout<<"\nName: ";
gets(Name);
cout<<"Score: ";
cin>>Score;
strcpy(A_Name,Name);
A_Score=Score;
fio.seekg(pos-sizeof(apl));
fio.write((char*)&apl,sizeof(apl));
cout<<"\nData has been modified successfully\n";
cout<<"Displaying the details of the records:\n";
fio.seekg(ios::beg);
while(!fio.eof())
{
fio.read((char*)&apl,sizeof(apl));
cout<<"\nRoll number: "<<A_Rno;
cout<<"\nName: ";
puts(A_Name);
cout<<"Score: "<<A_Score<<endl;
}
break;
}
}
if(found=='n')
cout<<"Record not found";
fio.close(); //Closing the file
}
void main()
{
char ch;
do
{
char op;
clrscr();
menu:
cout<<"Please select the option you want to choose:\n";
cout<<"1. Enter records to the file\n2. Display records from the file\n"<<
"3. Search for a roll number\n4. Modify the entered data\n"<<
"5. Exit\n";
cin>>op;
switch(op)
{
case '1': apl.Read(); //Function Call
break;
case '2': apl.Display();
break;
case '3': apl.Search();
break;
case '4': apl.Modify();
break;
case '5': exit(0);
break;
default: cout<<"\nPlease enter a valid option.\n";
goto menu; //Jump statement
}
exit:
cout<<"\nDo you want to continue?[y/n]: ";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"\nPlease enter a valid option";
goto exit;
}
}
while((ch=='y')||(ch=='Y'));
cout<<"\nThank you for using the program";
getch();
}
PROGRAM TO READ, DISPLAY, APPEND, AND DELETE RECORDS
IN THE FILE MOVIE.DAT
#include<fstream.h>
#include<ctype.h>
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
class movie //Declaring a class
{
int movie_id;
char movie_name[20];
int no_of_seats;
int ticketcode;
int hallno;
public: //Access Specifier
movie() //Constructor
{
movie_id=0;
strcpy(movie_name,"Not Assigned");
no_of_seats=0;
ticketcode=0;
hallno=0;
}
~movie()
{
cout<<"\nThank you for using the program.";
}
void readdata(); //Member Functions
void displaydata();
void DELETE();
int getmovieid();
}M;
void movie::readdata()
{
cout<<"Enter Movie ID:";
cin>>movie_id;
cout<<"Enter Movie name:";
gets(movie_name);
cout<<"Enter no of seats:";
cin>>no_of_seats;
cout<<"Enter Ticket code:";
cin>>ticketcode;
cout<<"Enter Hall number:";
cin>>hallno;
}
void movie::displaydata()
{
cout<<"\nMovie Id:"<<movie_id;
cout<<"\nMovie name:";
puts(movie_name);
cout<<"No of seats:"<<no_of_seats;
cout<<"\nTicket Code:"<<ticketcode;
cout<<"\nHall No:"<<hallno;
}
int movie::getmovieid()
{
return(movie_id);
}
void WRITE() //To put records in the binary file MOVIE.dat
{
ofstream f("MOVIE.dat", ios::binary|ios::app);
char reply;
do
{
M.readdata();
f.write((char*)&M,sizeof(M));
rep:
cout<<"Do you wish to enter more records ? Enter Y for yes and N for no.:";
cin>>reply;
if((toupper(reply)!='Y')&&(toupper(reply)!='N'))
{
cout<<"Invalid character entered.Please enter again.";
goto rep;
}
}while(toupper(reply)=='Y');
f.close();
}
void READ() //To display records from the file MOVIE.dat
{
ifstream f("MOVIE.DAT",ios::binary);
if(!f)
{
cout<<"\nThe File does not exist!!";
exit(-1);
}
cout<<"Displaying the records:\n";
while(!f.eof())
{
f.read((char*)&M,sizeof(M));
M.displaydata();
}
f.close();
}
void movie::DELETE() //Delete a particular record as per user's wish
{
ifstream fmain("MOVIE.DAT",ios::binary);
ofstream ftemp("temp.dat",ios::binary);
int movid;
char found='n';
cout<<"Enter the movie id of the movie whose records are to be deleted:";
cin>>movid;
while(!fmain.eof())
{
fmain.read((char*)&M,sizeof(M)); //Reading from the file
if(M.getmovieid()==movid)
found='y';
else
ftemp.write((char*)&M,sizeof(M));
}
if(found=='n')
cout<<"Record not found!";
if(found=='y')
cout<<"Record successfully deleted.";
fmain.close();
ftemp.close();
remove("movie.dat");
rename("temp.dat","movie.dat");
}
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
char choice;
menu:
cout<<"MENU:\n";
cout<<"1.Append Record\n";
cout<<"2.Display all Records\n";
cout<<"3.Deletion of a Record\n";
cout<<"4.Exit\n";
cout<<"Please enter your choice:";
cin>>choice;
switch(choice)
{
case '1': WRITE();
break;
case '2': READ();
break;
case '3': M.DELETE();
break;
case '4': exit(0);
break;
default : cout<<"Invalid character entered.Please enter again.\n";
goto menu;
}
again:
cout<<"\nDo you wish to continue? Enter Y for Yes and N for No.:";
cin>>ch;
if((toupper(ch)!='Y')&&(toupper(ch)!='N'))
{
cout<<"Invalid character entered. Please enter again.";
goto again;
}
}while((toupper(ch)=='Y'));
getch();
}
4
PROGRAM TO SEGREGATE LETTERS IN A TEXT FILE
#include<fstream.h>
#include<conio.h>
#include<process.h>
void LOWER(char chr)
{
fstream fout;
fout.open("LOWER.TXT",ios::out|ios::app); //Opening a file
if(!fout) //Checking the existence of a file
{
cout<<"\nThe file does not exist";
exit(-1);
}
fout<<chr; //Writing to the file
fout.close();
}
void UPPER(char chr)
{
fstream fout;
fout.open("UPPER.TXT",ios::out|ios::app);
if(!fout)
{
cout<<"\nThe file does not exist";
exit(-1);
}
fout<<chr;
fout.close(); //Closing the file
}
void OTHERS(char chr)
{
fstream fout;
fout.open("OTHERS.TXT",ios::out|ios::app);
if(!fout)
{
cout<<"\nThe file does not exist";
exit(-1);
}
fout<<chr; //Writing to the file
fout.close();
}
void DisplayL()
{
char c;
cout<<"\nThe data in the file \"LOWER\" is:\n";
fstream fin;
fin.open("LOWER.TXT",ios::in);
if(!fin)
{
cout<<"\The file does not exist";
exit(-1);
}
while(!fin.eof())
{
fin>>c; //Reading from the file
cout<<c<<"\n";
}
fin.close(); //Closing the file
}
void DisplayU()
{
char c;
cout<<"\nThe data in the file \"UPPER\" is:\n";
fstream fin;
fin.open("UPPER.TXT",ios::in);
if(!fin) //checking the existence of a file
{
cout<<"\The file does not exist";
exit(-1);
}
while(!fin.eof())
{
fin>>c;
cout<<c<<"\n";
}
fin.close(); //Closing the file
}
void DisplayO()
{
char c;
cout<<"\nThe data in the file \"OTHERS\" is:\n";
fstream fin;
fin.open("OTHERS.TXT",ios::in); //Opening the file
if(!fin)
{
cout<<"\The file does not exist";
exit(-1);
}
while(!fin.eof())
{
fin>>c;
cout<<c<<"\n";
}
fin.close();
}
void main()
{
char ch,op,choice;
do //do while loop for multiple executions
{
clrscr();
menu:
cout<<"Please choose an option:\n"<<
"1. Enter a character\n2. Display the file \"LOWER\"\n"<<
"3. Display the file \"UPPER\"\n4. Display the file \"OTHER\"\n"<<
"5. Exit\n";
cin>>choice;
switch(choice)
{
case '1':do
{
cout<<"Enter the character(enter '0' to terminate): ";
cin>>ch;
if((ch>=65)&&(ch<=90))
LOWER(ch);
else if((ch>=97)&&(ch<=122))
UPPER(ch);
else
OTHERS(ch);
if(ch!='0')
cout<<"Character updated successfully\n";
}
while(ch!='0');
break;
case '2':DisplayL(); //Function Call
break;
case '3':DisplayU();
break;
case '4':DisplayO();
break;
case '5':exit(0);
break;
default: cout<<"Please enter a valid option\n";
goto menu;
}
exit: //goto label
cout<<"\nDo you wish to continue?[Y/N]: ";
cin>>op;
if((op!='N')&&(op!='n')&&(op!='Y')&&(op!='y'))
{
cout<<"\nPlease enter a valid option";
goto exit;
}
}
while((op=='Y')||(op=='y'));
getch();
}
5
PROGRAM TO PERFORM VARIOUS FUNCTIONS ON A TEXT FILE
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
void createfile(); //Function Prototype
void displayfile();
void digitcount();
void alphacount();
void wordscount();
void linecount();
void spacecount();
void vowelcount();
void conscount();
void uppercount();
void lowercount();
void thecount();
void threecount();
char c,str[20];
int count;
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
int choice;
cout<<"Please choose among the following:\n"<<
"1. To write to the file\n"<<
"2. To read from the file\n"<<
"3. To count the number of digits in the file\n"<<
"4. To count the number of alphabets in the file\n"<<
"5. To count the number of words in the file\n"<<
"6. To count the number of lines in the file\n"<<
"7. To count the number of spaces in the file\n"<<
"8. To count the number of vowels in the file\n"<<
"9. To count the number of consonants in the file\n"<<
"10.To count the number of uppercase letters in the file\n"<<
"11.To count the number of lowercase letters in the file\n"<<
"12.To count the number of times 'the' occurs in the file\n"<<
"13.To count the number of all thre lettered words in the file\n"<<
"14.To exit\n";
again:
cin>>choice;
switch(choice)
{
case 1 : createfile(); //Function Call
break;
case 2 : displayfile();
break;
case 3 : digitcount();
break;
case 4 : alphacount();
break;
case 5 : wordscount();
break;
case 6 : linecount();
break;
case 7 : spacecount();
break;
case 8 : vowelcount();
break;
case 9 : conscount();
break;
case 10: uppercount();
break;
case 11: lowercount();
break;
case 12: thecount();
break;
case 13: threecount();
break;
case 14: exit(0);
break;
default: cout<<"Invalid character entered. Please enter again.";
goto again;
}
label: //goto label
cout<<"\nDo you wish to continue? Enter Y for Yes and N for No.";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
goto label; //Jump statement
}while((ch=='y')||(ch=='Y'));
getch();
}
void createfile() //Function Definition
{
ofstream fout("text.txt",ios::app);
cout<<"Please enter the data:";
gets(str);
fout<<str; //Writing in the file
cout<<"Written succesfully to the file.";
fout.close();
}
void displayfile()
{
ifstream fin("text.txt");
if(!fin) //Checking the existence of the file
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin>>str;
cout<<str;
}
fin.close(); //Closing the file
}
void digitcount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if((c>=48)&&(c<=57))
count++;
}
cout<<"The number of digits in the file are:"<<count;
fin.close();
}
void alphacount()
{
count=0;
ifstream fin;
fin.open("text.txt");
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if(((c>=65)&&(c<=90))||((c>=97)&&(c<=122)))
count++;
}
cout<<"The no. of alphabets are :"<<count;
fin.close();
}
void wordscount()
{
count=1;
ifstream fin;
fin.open("text.txt");
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if(c==' ')
count++;
}
cout<<"The no. of words are :"<<count;
fin.close();
}
void linecount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.getline(str,'20','.');
count++;
}
cout<<"The number of lines in the file are:"<<count;
fin.close();
}
void spacecount()
{
count=0;
ifstream fin;
fin.open("text.txt"); //Opening the file
if(!fin)
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c); //Reading character from file
if(c==' ')
count++;
}
cout<<"The no. of spaces are :"<<count;
fin.close();
}
void vowelcount()
{
count=0;
ifstream fin;
fin.open("text.txt");
if(!fin) //Checking the existence of a file
{
cout<<"File does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c); //Reading from the file
switch(c)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': count++;
break;
}
}
cout<<"The no. of vowels are :"<<count;
fin.close();
}
void conscount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if((isalpha(c)==0)&&(toupper(c)!='A')&&(toupper(c)!='E')&&(toupper(c)!='I')&&(to
upper(c)!='O')&&(toupper(c)!='U'))
count++;
}
cout<<"The number of consonants in the file are:"<<count;
fin.close();
}
void uppercount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if((c>=65)&&(c<=90))
count++;
}
cout<<"The number of uppercase letters in the file are:"<<count;
fin.close();
}
void lowercount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin.get(c);
if((c>=97)&&(c<=122))
count++;
}
cout<<"The number of lowercase letters in the file are:"<<count;
fin.close();
}
void thecount()
{
count=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin>>str;
if(strcmpi(str,"the")==0)
count++;
}
cout<<"The number of times 'the' occurs in the file are:"<<count;
fin.close();
}
void threecount()
{
count=0;
int len=0;
ifstream fin("text.txt");
if(!fin)
{
cout<<"The file does not exist.";
exit(-1);
}
while(!fin.eof())
{
fin>>str;
len=strlen(str);
if(len==3)
count++;
}
cout<<"The number of three lettered words in the file are:"<<count;
fin.close();
}
PROGRAM TO REPLACE CONSECUTIVE SPACES BY A SINGLE
SPACE IN A TEXT FILE
//Program to read a text file and create another text file by
//replacing a sequence of 2 consective space by a single space
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
void create();
void display(); //Function Prototype
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
char choice;
menu: //goto label
cout<<"Please choose from the following:\n"<<
"1.To append data to the ORIG file.\n"<<
"2.To display the ORIG and NEW file.\n"<<
"3.To exit.\n";
cin>>choice;
switch(choice)
{
case '1' : create(); //Function Call
break;
case '2' : display();
break;
case '3' : cout<<"Thank you for using the program!";
exit(0);
default : cout<<"Invalid character entered.Please enter again.\n";
goto menu;
}
again:
cout<<"\nDo you wish to continue? Please enter Y for Yes and N for No:";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"Invalid character entered!Please enter again.\n";
goto again;
}
}while((ch=='y')||(ch=='Y'));
getch();
}
void create() //Function Definition
{
char str[20],c,x;
ofstream fout("ORIG.TXT",ios::app);
cout<<"Please enter the data:";
gets(str);
fout<<str; //Writing to the file
fout.close();
ifstream fin("ORIG.TXT");
ofstream fo("NEW.TXT"); //Opening the file
while(!fin.eof())
{
fin.get(c);
if(c==' ')
{
fin.get(x);
if(x==' ')
fo<<' ';
else
{
fin.seekg(-1,ios::cur);
fo<<c;
}
}
else
fo<<c;
}
fin.close();
fo.close(); //Closing the file
}
void display()
{
ifstream f;
char c;
f.open("ORIG.TXT"); //Opening the file
if(!f)
{
cout<<"The file does not exist.";
exit(-1);
}
cout<<"\nDisplaying the ORIG file:\n";
while(!f.eof())
{
f.get(c); //Reading from the file
cout<<c;
}
f.close();
f.open("NEW.TXT");
cout<<"\nDisplaying the NEW file:\n";
while(!f.eof())
{
f.get(c);
cout<<c;
}
f.close();
}
PROGRAM TO PERFORM VARIOUS FUNCTIONS ON A BINARY
FILE BUS.DAT
#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 :";
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;
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)
{
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()
{
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();
}
PROGRAM TO PERFORM VARIOS FUNCTIONS ON A BINARY FILE
BUS.DAT
#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;
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);
}
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 ";
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();
}
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'));
getch();
}
PROGRAM TO PERFORM VARIOUS OPERATIONS ON MATRICES
#include<iostream.h>
#include<conio.h>#include<stdlib.h>
void add(int mat[10][10],int mat2[10][10],int ro1,int col1); //Function Prototypes
void sub(int mat[10][10],int mat2[10][10],int row1,int col1);
void prod(int mat[10][10],int mat2[10][10],int row1,int col1);
void main() //Main begins
{
int mat[10][10],mat2[10][10],col1,col2,row1,row2,i,j,k,l; //Variable Declarations
char choice,ch;
do
{
clrscr();
menu:
cout<<"The menu is:"
<<"\n1.Addition "
<<"\n2.Subtraction "
<<"\n3.Multiplication "
<<"\n4.Exit"
<<"\nEnter choice:";
cin>>choice; //Display Menu and Take Choice with Switch Case
switch(choice)
{
case '1': cout<<"Please enter the number of rows and columns in
matrix 1 respectively:\n"; //Matrix Row column Input
cin>>row1>>col1;
cout<<"Please enter the number of rows and columns in
matrix 2 respectively:\n";
cin>>row2>>col2;
if((row1!=row2)||(col1!=col2)) //Checking compatibility
{
cout<<"The matrices cannot be added.";
break;
}
cout<<"Please enter elements of matrix 1:\n";
for(i=0;i<row1;i++) //Matrix 1 Input loop
{
for(j=0;j<col1;j++)
{
cin>>mat[i][j];
}
}
cout<<"Please enter elements of matrix 2:\n";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>mat2[i][j];
}
}
add(mat,mat2,row1,col1); //Function Call
break;
case '2': cout<<"Please enter the number of rows and columns in
matrix 1 respectively:\n"; //Matrix Row column Input
cin>>row1>>col1;
cout<<"Please enter the number of rows and columns in
matrix 2 respectively:\n";
cin>>row2>>col2;
if((row1!=row2)||(col1!=col2))
{
cout<<"The matrices cannot be subtracted.";
break;
}
cout<<"Please enter elements of matrix 1:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>mat[i][j];
}
}
cout<<"Please enter elements of matrix 2:\n";
for(i=0;i<row2;i++) //Reading values of matrix 2
{
for(j=0;j<col2;j++)
{
cin>>mat2[i][j];
}
}
sub(mat,mat2,row1,col1);
break;
case '3': cout<<"Please enter the number of rows and columns in
matrix 1 respectively:\n"; //Matrix Row column Input
cin>>row1>>col1;
cout<<"Please enter the number of rows and columns in
matrix 2 respectively:\n";
cin>>row2>>col2;
if((row1!=row2)||(col1!=col2))
{
cout<<"The matrices cannot be multiplied.";
break;
}
cout<<"Please enter elements of matrix 1:\n";
for(i=0;i<row1;i++) //Reading values of matrix 1
{
for(j=0;j<col1;j++)
{
cin>>mat[i][j];
}
}
cout<<"Please enter elements of second matrix:\n";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>mat2[i][j];
}
}
prod(mat,mat2,row1,col1); //Function call
break;
case '4': cout<<"\nThank you for using the program!";
exit(0);
break;
default : cout<<"Invalid character entered.Please enter again.";
goto menu;
}
again:
cout<<"\nDo you want to continue? Enter y for yes and n for no:";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"Invalid character entered.Please enter again.";
goto again;
}
}
while((ch=='y')||(ch=='Y'));
cout<<"\nTHANK YOU FOR USING THE PROGRAM!";
getch();
}
void add(int mat[10][10], int mat2[10][10], int row1, int col1) //Function Definition
{
int i,j,mat3[10][10];
for(i=0;i<row1;i++)
{ //Loop to add matrices
for(j=0;j<col1;j++)
{
mat3[i][j]=mat[i][j]+mat2[i][j];
}
cout<<"\n";
}
cout<<"Displaying matrix 1:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat[i][j]<<"\t";
cout<<"\n";
}
cout<<"Displaying matrix 2:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat2[i][j]<<"\t";
cout<<"\n";
}
cout<<"Displaying the added matrix:\n";
for(i=0;i<row1;i++)
{ //Loop to display the added matrix
for(j=0;j<col1;j++)
cout<<mat3[i][j]<<"\t";
cout<<"\n";
}
}
void sub(int mat[10][10],int mat2[10][10],int row1,int col1)
{
int i,j,mat3[10][10];
for(i=0;i<row1;i++)
{ //Loop to subtract matrices
for(j=0;j<col1;j++)
{
mat3[i][j]=mat[i][j]-mat2[i][j];
}
}
cout<<"Displaying matrix 1:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat[i][j]<<"\t";
cout<<"\n";
}
cout<<"Displaying matrix 2:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat2[i][j]<<"\t";
cout<<"\n";
}
cout<<"Displaying the subtracted matrix:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat3[i][j]<<"\t";
cout<<"\n";
}
}
void prod(int mat[10][10],int mat2[10][10],int row1,int col1)
{
int i,j,mat3[10][10];
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
mat3[i][j]=mat[i][j]*mat2[i][j];
}
}
cout<<"Displaying matrix 1:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat[i][j]<<"\t";
cout<<"\n";
}
cout<<"Displaying matrix 2:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat2[i][j]<<"\t";
cout<<"\n";
}
cout<<"Dipslaying the multiplied matrix:\n";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
cout<<mat3[i][j]<<"\t";
cout<<"\n";
}
}
PROGRAM TO PERFORM VARIOUS OPERATIONS ON A MATRIX
/*PROGRAM TO OPERATE ON A MATRIX*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
void TRANSPOSE(int[][20],int,int); //Function Prototype
void SUMR(int[][20],int,int);
void SUMC(int[][20],int,int);
void diagonal(int arr[][20],int r1,int c1);
void SUMBELOW(int[][20],int,int);
void SUMABOVE(int[][20],int,int);
void main()
{
char ch;
do //do while loop for multiple executions
{
clrscr();
int A[20][20],r,c,i,j,choice;
menu:
cout<<"\n\t\t\t\tMATRIX MENU : \n";
cout<<"\n1.To transpose a matrix"
<<"\n2.Find row sum"
<<"\n3.Find column sum"
<<"\n4.Find sum of the diagonal elements"
<<"\n5.Find sum of elements below main diagonal"
<<"\n6.Find sum of elements above main diagonal"
<<"\n7.Exit";
cout<<"\nPlease enter your choice:";
cin>>choice;
if((choice<1)||(choice>7))
{
cout<<"\nInvalid choice! Please enter a valid choice.";
goto menu;
}
if(choice==7)
{
cout<<"Thank you for using the program!";
exit(0);
}
cout<<"Please enter the number of rows and columns in the matrix:\n";
cin>>r>>c;
cout<<"Please enter the elements of the matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>A[i][j];
}
cout<<"Displaying the matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<A[i][j]<<"\t";
cout<<"\n";
}
switch(choice)
{
case 1 : TRANSPOSE(A,r,c);
break;
case 2 : SUMR(A,r,c);
break;
case 3 : SUMC(A,r,c);
break;
case 4 : if(r!=c)
{
cout<<"No diagonal elements present.";
break;
}
diagonal(A,r,c);
break;
case 5 : SUMBELOW(A,r,c);
break;
case 6 : SUMABOVE(A,r,c);
break;
default: ;
}
again:
cout<<"\nDo you wish to continue? <y/n> ";
cin>>ch;
if((ch!='y')&&(ch!='Y')&&(ch!='n')&&(ch!='N'))
{
cout<<"Invalid character entered.Please enter again.";
goto again;
}
}
while((ch=='y')||(ch=='Y'));
cout<<"THANK YOU FOR USING THE PROGRAM!";
}
void TRANSPOSE(int A[20][20],int m,int n)
{
int i,j;
cout<<"\nTranspose of matrix is:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<A[j][i]<<"\t";
cout<<"\n";
}
}
void SUMR(int A[20][20],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
sum+=A[i][j];
cout<<"\nSum of elements of row"<<i+1<<" is:"<<sum;
}
}
void SUMC(int A[20][20],int m,int n)
{
int i,j,sum=0;
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<m;i++)
sum+=A[i][j];
cout<<"\nSum of elements of column"<<j+1<<" is:"<<sum;
}
}
void diagonal(int A[20][20],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if((i==j)||(i==m-j-1))
sum+=A[i][j];
}
}
cout<<"The sum of diagonal elements is:"<<sum;
}
void SUMBELOW(int A[20][20],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
sum+=A[i][j];
}
}
cout<<"\nSum of elements below the main diagonal="<<sum;
}
void SUMABOVE(int A[20][20],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
sum+=A[i][j];
}
}
cout<<"\nSum of elements above the main diagonal="<<sum;
}
PROGRAM TO ILLUSTRATE THE TWO TYPES OF SEARCHES ON
AN INTERGER ARRAY
/*PROGRAM TO PERFORM LINEAR AND BINARY SEARCH*/
#include<process.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int Lsearch(int[],int,int); /*FUNCTION PROTOTYPE*/
int Bsearch(int[],int,int);
void main()
{
int ch;
char choice;
int ARR[10],i,j,k,found;
clrscr();
do
{
cout<<"CHOOSE:1.LINEAR SEARCH"
<< "\n2.BINARY SEARCH"
<<"\n3.Exit\n";
cin>>ch;
cout<<"\nHow many elements do you want to enter?";
cin>>i;
cout<<"Enter elements of array:";
for(j=0;j<i;j++)
{
cin>>ARR[j];
}
cout<<"Which element do you want to find?";
cin>>k;
switch(ch)
{
case 1: found=Lsearch(ARR,i,k); /*FUNCTION CALL*/
if(found==-1)
cout<<"\nYour element was not found.";
else
cout<<"\nElement found at position:"<<found+1;
break;
case 2:found=Bsearch(ARR,i,k);
if(found==-1)
cout<<"\nYour element was not found";
else
cout<<"\nElement found at position:"<<found+1;
break;
case 3: cout<<"THANK YOU;')";
exit(-1);
default:cout<<"INVALID";
}
cout<<"\nDo you wish to continue? (PRESS Y/y )";
cin>>choice;
}
while((choice=='y')||(choice=='Y'));
cout<<"THANKS";
getch();
}
int Lsearch(int ARR[],int size,int it) /*FUNCTION DEFINITION*/
{
for(int p=0;p<size;p++)
{
if(ARR[p]==it)
return p;
}
return -1;
}
int Bsearch(int ARR[],int size,int it)
{
int beg,last,mid;
beg=0;last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(it==ARR[mid])
return(mid);
else if(it>ARR[mid])
beg=mid+1;
else
last=mid-1;
}
return(-1);
}
PROGRAM TO ILLUSTRATE THE THREE TYPES OF SORTS ON AN ARRAY
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
char ch;
int ARR[50],size,n;
do //do while loop
{
cout<<"Enter number of elements:\n";
cin>>size;
cout<<"\nWhich type of sorting technique do you want?\n"
//MENU
<<"1)Insertion Sort\n"
<<"2)Selection Sort\n"
<<"3)Bubble Sort\n"
<<"4)Exit";
cout<<"\nPlease enter your choice \t: ";
cin>>n;
cout<<"\n";
switch(n)
{
case 1:
cout<<"You have selected Insertion Sort";
cout<<"\n\nEnter the elements:\n";
for(int i=0;i<size;i++)
cin>>ARR[i];
InsertionSort(ARR,size); //Function call
break;
case 2:
cout<<"You have selected Selection Sort";
cout<<"\n\nEnter the elements:\n";
for(i=0;i<size;i++)
cin>>ARR[i];
SelectionSort(ARR,size); //Function call
break;
case 3:
cout<<"You have selected Bubble Sort";
cout<<"\n\nEnter the elements:\n";
for(i=0;i<size;i++)
cin>>ARR[i];
BubbleSort(ARR,size); //Function call
break;
case 4:
exit(0);
break;
}while((ch=='y')||(ch=='Y'));
cout<<"\nThank you for using the program";
getch();
}
int l,j,k,temp,MAT1[100];
cout<<"\n\nSteps followed in this sort \nInitial array :";
for(int p=0;p<a;p++)
{ cout<<MAT[p]<<" "; }
for(l=1;l<a;l++)
{
temp=MAT[l];
j=l-1;
while((temp<MAT[j])&&(j>=0))
{
MAT[j+1]=MAT[j];
j--;
}
MAT[j+1]=temp;
for(k=0;k<a;k++)
MAT1[k]=MAT[k];
cout<<" \nStep "<<l<<" : ";
for( p=0;p<a;p++)
{ cout<<MAT1[p]<<" "; }
cout<<endl<<endl;
}
cout<<"\nASCENDING ORDER:\n"; //To display array in ascending
for(k=0;k<a;k++)
cout<<MAT1[k]<<"\t";
cout<<"\nDESCENDING ORDER:\n"; //To display array in descending
for(k=a-1;k>=0;k--)
cout<<MAT1[k]<<"\t";
}
void SelectionSort(int MAT[],int a) //Selection sort
{
int j,k,small,pos,temp,MAT2[50];
cout<<"\n\nSteps followed in this sort \nInitial array :";
for(int p=0;p<a;p++)
{ cout<<MAT[p]<<" "; }
for(j=0;j<a-1;j++)
{
small=MAT[j];
pos=j;
for(k=j+1;k<a;k++)
{
if(MAT[k]<small)
{
small=MAT[k];
pos=k;
}
}
temp=MAT[j];
MAT[j]=MAT[pos];
MAT[pos]=temp;
for(k=0;k<a;k++)
MAT2[k]=MAT[k];
cout<<"\n Step "<<j+1<<" : ";
for(int p=0;p<a;p++)
{ cout<<MAT2[p]<<" "; }
cout<<endl<<endl;
}
cout<<"\nASCENDING ORDER:\n"; //To display array in ascending
for(k=0;k<a;k++)
cout<<MAT2[k]<<"\t";
cout<<"\nDESCENDING ORDER:\n"; //To display array in descending
for(k=a-1;k>=0;k--)
cout<<MAT2[k]<<"\t";
}
void BubbleSort(int MAT[],int a) //Bubble sort
{
int temp,j,k,MAT3[50];
cout<<"\n\nSteps followed in this sort \nInitial array :";
for(int p=0;p<a;p++)
{ cout<<MAT[p]<<" "; }
for(j=0;j<a-1;j++)
{
for(k=0;k<a-1;k++)
{
if(MAT[k]>MAT[k+1])
{
temp=MAT[k];
MAT[k]=MAT[k+1];
MAT[k+1]=temp;
}
}
for(int l=0;l<a;l++)
MAT3[l]=MAT[l];
cout<<"\n Step "<<j+1<<" : ";
for(int p=0;p<a;p++)
{ cout<<MAT3[p]<<" "; }
cout<<endl<<endl;
}
void main()
{
clrscr();
char ch;
int ARR1[50],ARR2[50],size1,size2,i,l;
do //do while loop
{
cout<<"\nEnter size of Array-1:\n"; //asking user to enter
both the arrays
cin>>size1;
cout<<"\nEnter elements of Array-1:\n";
for(i=0;i<size1;i++)
cin>>ARR1[i];
cout<<"\nEnter size of Array-2:\n";
cin>>size2;
cout<<"\nEnter elements of Array-2:\n";
for(i=0;i<size2;i++)
cin>>ARR2[i];
cout<<"\nARRAY-1: ";
for(i=0;i<size1;i++)
cout<<ARR1[i]<<" ";
cout<<"\nARRAY-2: ";
for(i=0;i<size2;i++)
cout<<ARR2[i]<<" ";
cout<<"\nChoose a type of combination:\n" //Menu
<<"1)Ascending-Ascending\n"
<<"2)Ascending-Descending\n"
<<"3)Descending-Ascending\n"
<<"4)Descending-Descending\n"
<<"5)Exit\n";
cout<<"Please enter your choice: \t ";
cin>>l;
switch(l)
{
case 1:
cout<<"\nYou Have selected Ascending-Ascending option";
AAcombination(ARR1,ARR2,size1,size2);
break;
case 2:
cout<<"\nYou Have selected Ascending-Descending
option";
ADcombination(ARR1,ARR2,size1,size2);
break;
case 3:
cout<<"\nYou Have selected Descending-Ascending
option";
DAcombination(ARR1,ARR2,size1,size2);
break;
case 4:
cout<<"\nYou Have selected Descending-Descending
option";
DDcombination(ARR1,ARR2,size1,size2);
break;
case 5:exit(0);
break;
default:cout<<"Wrong choice.....Kindly enter from 1 to 5\n";
}
cout<<"\nDo you want to continue?Press y for yes and n for
no\n";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
cout<<"\nThankyou";
getch();
}
for(j=0;j<a2;j++)
{
for(l=0;l<a2-1;l++)
{
if(MAT2[l]>MAT2[l+1])
{
temp=MAT2[l];
MAT2[l]=MAT2[l+1];
MAT2[l+1]=temp;
}
}
for(k=0;k<a2;k++)
A2[k]=MAT2[k];
}
for(l=0,k=0;l<a1;l++,k++)
R[k]=A1[l];
for(j=0;j<a2;j++,k++)
R[k]=A2[j];
cout<<"\nNew Array:\n"; //displaying the merged array
for(k=0;k<(a1+a2);k++)
cout<<R[k]<<" ";
for(j=0;j<a2;j++)
{
for(l=0;l<a2-1;l++)
{
if(MAT2[l]>MAT2[l+1])
{
temp=MAT2[l];
MAT2[l]=MAT2[l+1];
MAT2[l+1]=temp;
}
}
for(k=0;k<a2;k++)
A2[k]=MAT2[k];
}
for(l=0,k=0;l<a1;l++,k++)
R[k]=A1[l];
for(j=a2-1;j>=0;j--,k++)
R[k]=A2[j];
cout<<"\nNew Array:\n"; //displaying merged array
for(k=0;k<(a1+a2);k++)
cout<<R[k]<<" ";
for(j=0;j<a2;j++)
{
for(l=0;l<a2-1;l++)
{
if(MAT2[l]>MAT2[l+1])
{
temp=MAT2[l];
MAT2[l]=MAT2[l+1];
MAT2[l+1]=temp;
}
}
for(k=0;k<a2;k++)
A2[k]=MAT2[k];
}
for(l=a1-1,k=0;l>=0;l--,k++)
R[k]=A1[l];
for(j=a2-1;j>=0;j--,k++)
R[k]=A2[j];
cout<<"\nNew Array:\n"; //Displaying merged array
for(k=0;k<(a1+a2);k++)
cout<<R[k]<<" ";
}
OUTPUTS
PROGRAM TO PERFORM VARIOUS FUNCTIONS ON ARRAY
//Program to perform various functions on ARRAY
#include<iostream.h>
#include<conio.h>
#include<process.h>
void PAIRS(int ARRAY[], int n); //Function Prototype
void ODD(int ARRAY[], int n);
void EVEN(int ARRAY[], int n);
void DIVBY30(int ARRAY[], int n);
void LARGEST(int ARRAY[], int n);
void SMALLEST(int ARRAY[], int n);
void AVERAGE(int ARRAY[], int n);
void main()
{
int n,i,choice;
char ch;
int ARRAY[20];
AGAIN: //goto label
clrscr();
menu:
cout<<"Welcome to the program. Please select a required function.\n"<<
"1. Find pairs of integers stored in ARRAY having sum equal to 50.\n"<<
"2. Find the number of elements which are odd.\n"<<
"3. Find the number of elements which are even.\n"<<
"4. Find the number of elements which are divisible by 30.\n"<<
"5. Find the largest of all elements.\n"<<
"6. Find the smallest of all elements.\n"<<
"7. Find the average of all elements.\n"<<
"8. Exit.\n";
cout<<"Please enter your choice.(Number corresponding).\n";
cin>>choice;
if(choice==8)
{
exit(0);
}
if((choice<1)||(choice>8))
{
cout<<"Please enter a valid choice.\n";
goto menu;
}
value: //goto label
cout<<"Please enter the number of elements you want in the ARRAY(MAX 20).\n";
cin>>n;
if((n>20)||(n<1))
{
cout<<"Please enter a number between 1 and 20.\n";
goto value;
}
cout<<"Please enter the ARRAY (all positive elements).\n";
for(i=0;i<n;i++)
{
AGAIN1:
cin>>ARRAY[i];
if(ARRAY[i]<0)
{
cout<<"The element you entered is negative. Please re-enter.\n";
goto AGAIN1;
}
}
switch(choice)
{
case 1: PAIRS(ARRAY, n);
break;
case 2: ODD(ARRAY, n);
break;
case 3: EVEN(ARRAY, n);
break;
case 4: DIVBY30(ARRAY, n);
break;
case 5: LARGEST(ARRAY, n);
break;
case 6: SMALLEST(ARRAY, n);
break;
case 7: AVERAGE(ARRAY, n);
break;
case 8:
break;
default: ;
}
cout<<"\nDo you wish to continue? Type Y for yes and N for N.\n";
cin>>ch;
if((ch=='Y')||(ch=='y')||(ch=='N')||(ch=='n'))
{
if((ch=='Y')||(ch=='y'))
{
goto AGAIN;
}
else
{
cout<<"Thank you for using the program.\n";
}
}
else
{
cout<<"Invalid choice. Please enter the correct choice.\n";
}
getch();
}
void PAIRS(int ARRAY[20], int n) //Function definition
{
int i, j, sum=0, count=1;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
sum=ARRAY[i]+ARRAY[j];
if(sum==50)
{
cout<<"\n"<<count<<" pair is
("<<ARRAY[i]<<","<<ARRAY[j]<<")";
count++;
}
}
}
if(count==1)
{
cout<<"No pair found";
}
}
void ODD(int ARRAY[20], int n)
{
int i, count=0;
for(i=0;i<n;i++)
{
if(ARRAY[i]%2!=0) //Checking whether the element is odd
{
count++;
}
}
cout<<"Total number of odd elements is equal to "<<count;
}
void EVEN(int ARRAY[20], int n)
{
int i, count=0;
for(i=0;i<n;i++)
{ //Checking whether the element is even
if(ARRAY[i]%2==0)
{
count++;
}
}
cout<<"Total number of even elements is equal to "<<count;
}
void DIVBY30(int ARRAY[20], int n)
{
int i, count=0;
for(i=0;i<n;i++)
{
if(ARRAY[i]%30==0) //Checking divisibility by 30
{
count++;
}
}
cout<<"Total number of elements divisible by 30 is equal to "<<count;
}
void LARGEST(int ARRAY[20], int n)
{
int i, max;
max=ARRAY[0]; //Initialising the largest element
for(i=1;i<n;i++)
{
if(max<ARRAY[i]) //Finding the largest element
{
max=ARRAY[i];
}
}
cout<<"The element having maximum value is "<<max;
}
void SMALLEST(int ARRAY[20], int n)
{
int i, min;
min=ARRAY[0]; //Initialising the smallest element
for(i=1;i<n;i++)
{
if(min>ARRAY[i])
{
min=ARRAY[i];
}
}
cout<<"The element having minimum value is "<<min;
}
void AVERAGE(int ARRAY[20], int n)
{
int i, sum=0;
float avg;
for(i=0;i<n;i++)
{ //Loop to calculate the sum of all elements
sum+=ARRAY[i];
}
avg=sum/n; //Calculating average
cout<<"The average of all elements is "<<avg;
}
PROGRAM TO PERFORM VARIOUS OPERATIONS ON TWO
DIMENSIONAL ARRAYS
//Header Files
#include<iostream.h>
#include<conio.h>
#include<process.h>\
//Function Prototypes
void NONZERO(int[][20],int,int);
void SUMALL(int[][20],int,int);
void SUMBELOW(int[][20],int,int);
void SUMABOVE(int[][20],int,int);
void PRODDIAG(int[][20],int,int);
void main()
{
char ch;
//Do-while loop to continue the program as long as the user demands
do
{
clrscr();
int A[20][20],r,c,i,j,choice;
}
}
cout<<"The product of diagonal elements:"<<product;
}
else
{
cout<<"No diagonal elements are there!";
}
}
void OFF_diagonal(int arr2[10][10],int r1,int c1)
{
cout<<"The elements of off the main diagonal are:\n";
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
if(i==r1-j-1)
cout<<arr2[i][j]<<"\t";
}
}
}
void sumdiagonal(int arr2[10][10],int r1,int c1)
{
int sum=0;
if(r1==c1)
{
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
if(i==j)
sum+=arr2[i][j];
else if(i==r1-j-1)
sum+=arr2[i][j];
}
}
}
else
{
cout<<"No diagonal elements are there!";
}
cout<<"The sum of diagonal elements:"<<sum;
}
void FINDCHAR1(char ca)
{
if((ca>=48)&&(ca<=57))
cout<<"The character entered is a number.\n";
else if((ca>=65)&&(ca<=90))
cout<<"The character entered is a uppercase alphabet.\n";
else if((ca>=97)&&(ca<=122))
cout<<"The character entered is an lowercase alphabet.\n";
else
cout<<"The character entered is a special character.\n";
}
void FINDCHAR2(char ca)
{
if(isdigit(ca)==0)
{
cout<<"The character entered is a digit,\n";
}
else if(isupper(ca)==0)
{
cout<<"The character entered is an uppercase alphabet,\n";
}
else if(islower(ca)==0)
{
cout<<"The character entered is a lowercase alphabet.\n";
}
else
{
cout<<"The character entered is a special character.\n";
}
}
void LOWERCHAR1(char ca)
{
if((ca>=65)&&(ca<=90))
{
cout<<"The character entered is a uppercase alphabet.\n";
}
else if((ca>=97)&&(ca<=122))
{
cout<<"The character entered is an lowercase alphabet.\n";
}
else
{
cout<<"It is not an alphabet.\n";
}
}
void LOWERCHAR2(char ca)
{
if(islower(ca)==0)
{
cout<<"The character entered is a lowercase alphabet.\n";
}
else if(isupper(ca)==0)
{
cout<<"The character entered is an uppercase alphabet\n";
}
else
{
cout<<"The character entered is not an alphabet.\n";
}
}
void REV(int arr1[10],int lim) //Function Definition
{
cout<<"The reversed vector is:\n";
for(int i=lim-1;i>=0;i--)
cout<<arr1[i]<<"\t";
}
void ROWSUM(int arr2[10][10],int r1,int c1)
{
int sum=0;
for(int i=0;i<r1;i++)
{
sum=0;
for(int j=0;j<c1;j++) //Loop to calculate sum
{
sum+=arr2[i][j];
}
cout<<"The sum of elements of row"<<i+1<<"is:"<<sum<<"\n";
}
}
void COLSUM(int arr2[10][10],int r1,int c1)
{
int sum=0;
for(int j=0;j<c1;j++)
{
sum=0;
for(int i=0;i<r1;i++)
{
sum+=arr2[i][j];
}
cout<<"The sum of elements of column"<<j+1<<"is:"<<sum<<"\n";
}
}
void ROWPROD(int arr2[10][10],int r1,int c1)
{
int product=1;
for(int i=0;i<r1;i++) //Loop to calculate product
{
product=1;
for(int j=0;j<c1;j++)
{
product*=arr2[i][j];
}
cout<<"The product of elements of row"<<i+1<<"is:"<<product<<"\n";
}
}
void COLPROD(int arr2[10][10],int r1,int c1)
{
int product=1;
for(int j=0;j<c1;j++)
{
product=1;
for(int i=0;i<r1;i++)
{
product*=arr2[i][j];
}
cout<<"The product of elements of column"<<j+1<<"is:"<<product<<"\n";
}
}
void diagonal2(int arr2[10][10],int r1,int c1)
{
int product=1;
if(r1==c1)
{
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
if(i==j)
product*=arr2[i][j];
else if(i==r1-j-1)
product*=arr2[i][j];
}
}
cout<<"The product of diagonal elements:"<<product;
}
else
{
cout<<"No diagonal elements are there!";
}
}
void ADDFUNC100(int arr1[10],int lim)
{
for(int i=0;i<lim;i++)
{
arr1[i]=arr1[i]+100;
}
cout<<"The vector after adding 100 to each elements:\n";
for(i=0;i<lim;i++) //Loop to print the modified vector
{
cout<<arr1[i]<<"\t";
}
}
void ADDFUNC100_m(int arr2[10][10],int r1,int c1)
{
int i,j;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
arr2[i][j]=arr2[i][j]+100;
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<arr2[i][j]<<"\t";
}
cout<<"\n";
}
}
void COUNTSPACE(char str[10])
{
int count=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
count++;
}
cout<<"The number of spaces in the entered string is:"<<count;
}
void equal(int arr2[10][10],int r1,int c1,int arr3[10][10],int r2,int c2)
{
if((r1==r2)&&(c1==c2))
{
for(int i=0;i<r1;i++) //Checking the equality
{
for(int j=0;j<c1;j++)
{
if(arr2[i][j]!=arr3[i][j])
{
cout<<"The two matrices are identical but not
equal\n";
goto label;
}
}
}
cout<<"The two matrices are equal\n";
}
else
{
cout<<"The two matrices are not equal\n";
}
label:
}
void swap(int arr1[10],int lim)
{
int temp;
temp=arr1[0];
arr1[0]=arr1[lim-1];
arr1[lim-1]=temp;
cout<<"The vector after swapping first and last value is:\n";
for(i=0;i<lim;i++) //Loop to print the desired result
{
cout<<arr1[i]<<"\t";
}
}
void double_m(int arr2[10][10],int r1,int c1)
{
int i,j;
for(i=0;i<r1;i++) //Loop to double each element
{
for(j=0;j<c1;j++)
{
arr2[i][j]=arr2[i][j]*2;
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<arr2[i][j]<<"\t";
}
cout<<"\n";
}
}
void ADDMAT(int arr2[10][10],int r1,int c1,int arr3[10][10],int r2,int c2)
{
int arr4_8[10][10],i,j;
if((r1==r2)&&(c1==c2))
{
for(i=0;i<r1;i++) //Loop to add the matrices
{
for(j=0;j<c1;j++)
{
arr4_8[i][j]=arr2[i][j]+arr3[i][j];
}
}
cout<<"The added matrix is as follows:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<arr4_8[i][j]<<"\t";
}
cout<<"\n";
}
}
else
{
cout<<"The matrices cannot be added.\n";
}
}
void SUBTRMAT(int arr2[10][10],int r1,int c1,int arr3[10][10],int r2,int c2)
{
int arr4[10][10],i,j;
if((r1==r2)&&(c1==c2))
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
arr4[i][j]=arr2[i][j]-arr3[i][j];
}
}
cout<<"The subtracted matrix is as follows:\n";
for(i=0;i<r1;i++) //Loop to print the result
{
for(j=0;j<c1;j++)
{
cout<<arr4[i][j]<<"\t";
}
cout<<"\n";
}
}
else
{
cout<<"The matrices cannot be subtracted.\n";
}
}
void PRODMAT(int arr2[10][10],int r1,int c1,int arr3[10][10],int r2,int c2)
{
int arr4[10][10],i,j;
if((r1==r2)&&(c1==c2))
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
arr4[i][j]=arr2[i][j]*arr3[i][j];
}
}
cout<<"The multiplied matrix is as follows:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<arr4[i][j]<<"\t";
}
cout<<"\n";
}
}
else
{
cout<<"The matrices cannot be multiplied.\n";
}
}
void TRANSFORM(int arr2[10][10],int r1,int c1)
{
cout<<"The transposed matrix is as follows:";
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
cout<<arr2[j][i]<<"\t";
}
cout<<“\n";}
PROGRAM TO CREATE A COMPLETE LINKED LIST CLASS
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node //Declaration of node
{
int info;
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();
int choice,nodes,element,position,i;
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 Last ";
cout<<"\n\t\t3. Delete a Node";
cout<<"\n\t\t4. Display Linked List ";
cout<<"\n\t\t5. 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();
l.display();
cout<<"\n";
break;
case 2: cout<<"\n\t Inserting Node at Last: ";
l.insert_last();
l.display();
cout<<"\n";
break;
case 3: cout<<"\n\t Deleting a Node: ";
l.delete_pos();
l.display();
cout<<"\n";
break;
case 4: cout<<"\n\t Display the Linked List: ";
l.display();
cout<<"\n";
break;
case 5: cout<<"\n\t Program Over ";
getch();
break;
default: cout<<"\n\t Wrong choice!";
cout<<"\n\t Reenter Choice: ";
goto re;
}
getch();
} while(choice!=5);
}
//Creating Node
node*List::create_node(int value)
{
node *temp;
temp=new node;
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;
temp= create_node(value);
if(start== NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
cout<<"\n\n\t Element Inserted at Beginning. ";
}
start=s->next;
cout<<"\n\n\tElement deleted is"<<s->info;
delete s;
}
else
{
while(s->next!=NULL)
{
s=s->next;
counter++;
}
if((pos>0)&&(pos<=counter))
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=s->next;
}
else
{
cout<<"\n\n\t Position out of range.Enter the valid position ";
goto position;
}
}
}
//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<<"NULL";
}
OUTPUTS
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=-1; //This signifies that
int rear=-1; //the queue has no data member
}
void addQ(int item) //Function for inserting element
{
/*Checking if queue is full
Maximum capacity of queue is 5 */
if(rear==4)
cout<<"\nQueue is full .No element could be added";
else if(rear==-1)
{
};
public:
};
stack::~stack()
{
while(top!=NULL)
{
node*temp=top;
top=top->next;
delete temp;
}
}
void main()
{
clrscr();
stack s;
int ch;
do
{ label:
cout<<"\n***********MAIN MENU************\n"
<<"1)Push Element\n"
<<"2)Pop Element\n"
<<"3)Display Stack\n"
<<"4)Exit\n"
<<"Enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:s.push(); //Function push() called
s.display();
break;
case 2:s.pop(); //Function pop() called
s.display();
break;
case 3:s.display(); //Function display() called
break;
case 4: cout<<"\n Program Over ";
exit(0);
break;
default:cout<<"Wrong choice, Please choose a correct choice \n";
goto label;
exit(0);
}
}while(ch<4);
cout<<"\n Program Over ";
getch();
}
QUERIES OF RELATION ‘TEACHER’
a) SELECT*
FROM RelationTeacher
WHERE Dept=’History’;
--Display all the information of teachers of the history department
b) SELECT*
FROM RelationTeacher
WHERE Dept=’Hindi” AND Sex=’F’;
--Display the names of all the female teachers of the Hindi department
c) SELECT*
FROM RelationTeacher
ORDER BY DateOfJoin;
--Display the list of teachers with their date of joining in the ascending
order
(d) SELECT*
FROM RelationTeacher
WHERE Sex=’M’;
e) SELECT COUNT(*)
FROM RelationTeacher
WHERE Age<23;
--To count the number of teachers having age less than 23
2. SELECT SUM(FARE)
FROM FARES
WHERE AIRLINES=”INDIAN AIRLINES”;
594
BIBLIOGRAPHY
By Sumita Arora
● Object Oriented Programming with C++
By E. Balagurusamy
● Object Oriented Programming in C++
By Robert Lafore
595