Click here to download assignment programs
https://drive.google.com/open?id=0B9zJxhTsrja5X3pOQnpxNWdBTEU
==========================================================================
LIST OF ASSIGNMENTS
https://drive.google.com/open?id=0B9zJxhTsrja5X3pOQnpxNWdBTEU
==========================================================================
LIST OF ASSIGNMENTS
A-1(Compulsory)
Install,
Configure 64 bit Linux Operating Systems, study basic architecture, memory
system, and learn basic administration.
Group
A
2.
Implement a class Complex which represents the Complex Number data type. Implement
the following operations: 1. Constructor (including a default constructor which
creates the complex number 0+0i). 2. Overloaded operator+ to add two complex
numbers. 3. Overloaded operator* to multiply two complex numbers. 4. Overloaded
<< and >> to print and read Complex Numbers.
3.
Implement a class Quadratic that represents degree two polynomials i.e.,
polynomials of type ax2+bx+c. The class will require three data members
corresponding to a, b and c. Implement the following operations: 1. A constructor
(including a default constructor which creates the 0 polynomial). 2. Overloaded
operator+ to add two polynomials of degree 2. 3. Overloaded << and
>> to print and read polynomials. To do this, you will need to decide
what you want your input and output format to look like. 4. A function eval
that computes the value of a polynomial for a given value of x. 5. A function
that computes the two solutions of the equation ax2+bx+c=0.
4. Implement a class CppArray which is
identical to a one-dimensional C++ array (i.e., the index set is a set of
consecutive integers starting at 0) except for the following : 1. It performs
range checking. 2. It allows one to be assigned to another array through the
use of the assignment operator (e.g. cp1= cp2) 3. It supports a function that
returns the size of the array. 4. It allows the reading or printing of array
through the use of cout and cin.
5. Write a C++ program create a calculator for
an arithmetic operator (+, -, *, /). The program should take two operands from
user and performs the operation on those two operands depending upon the
operator entered by user. Use a switch statement to select the operation.
Finally, display the result. Some sample interaction with the program might
look like this: Enter first number, operator, second number: 10 / 3 Answer =
3.333333 Do another (y/n)? y Enter first number, operator, second number: 12 +
100 Answer = 112 Do another (y/n)? n
6.
Develop an object oriented program in C++ to create a database of student
information system containing the following information: Name, Roll number,
Class, division, Date of Birth, Blood group, Contact address, telephone number,
driving license no. etc Construct the database with suitable member functions
for initializing and destroying the data viz constructor, default constructor,
Copy constructor, destructor, static member functions, friend class, this
pointer, inline code and dynamic memory allocation operators-new and delete.
7. Create a class template to represent a
generic vector. Include following member functions: · To
create the vector. · To modify the value of a given
element · To multiply by a scalar value · To
display the vector in the form (10,20,30,…)
8.
Create a class Rational Number (fractions) with the following capabilities: a)
Create a constructor that prevents a 0 denominator in a fraction, reduces or
simplifies fractions that are not in reduced form and avoids negative
denominators. b) Overload the addition, subtraction, multiplication and
division operators for this class. c) Overload the relational and equality
operators for this class.
9. Imagine a publishing company which does
marketing for book and audiocassette versions. Create a class publication that
stores the title (a string) and price (type float) of a publication. From this
class derive two classes: book, which adds a page count (type int), and tape,
which adds a playing time in minutes (type float). Write a program that
instantiates the book and tape classes, allows user to enter data and displays
the data members. If an exception is caught, replace all the data member values
with zero values.
10.
Write a function in C++ to count and display the number of lines not starting
with alphabet 'A' present in a text file "STORY.TXT". Example: If the
file "STORY.TXT" contains the following lines, The roses are red. A
girl is playing there. There is a playground. An aeroplane is in the sky. Numbers
are not allowed in the password. The function should display the output as 3.
11.
Write C++ Program with base class convert declares two variables, val1 and
val2, which hold the initial and converted values, respectively. It also
defines the functions getinit( ) and getconv( ), which return the initial value
and the converted value. These elements of convert are fixed and applicable to
all derived classes that will inherit convert. However, the function that will
actually perform the conversion, compute( ), is a pure virtual function that
must be defined by the classes derived from convert. The specific nature of
compute( ) will be determined by what type of conversion is taking place.
12. A
book shop maintains the inventory of books that are being sold at the shop. The
list includes details such as author, title, price, publisher and stock
position. Whenever a customer wants a book, the sales person inputs the title
and author and the system searches the list and displays whether it is
available or not. If it is not, an appropriate message is displayed. If it is,
then the system displays the book details and requests for the number of copies
required. If the requested copies book details and requests for the number of
copies required. If the requested copies are available, the total cost of the
requested copies is displayed; otherwise the message ―Required copies not in
stock‖ is displayed. Design a system using a class called books with suitable
member functions and Constructors. Use new operator in constructors to allocate
memory space required. Implement C++ program for the system.
13. Create employee bio-data using following
classes i) Personal record ii))Professional record iii) Academic record Assume
appropriate data members and member function to accept required data &
print bio-data. Create bio-data using multiple inheritance using C++.
Group B
14. Crete User defined exception to check the
following conditions and throw the exception if the criterion does not meet. a.
User has age between 18 and 55 b. User stays has income between Rs. 50,000 –
Rs. 1,00,000 per month c. User stays in Pune/ Mumbai/ Bangalore / Chennai d.
User has 4-wheeler Accept age, Income, City, Vehicle from the user and check
for the conditions mentioned above. If any of the condition not met then throw
the exception.
15.
Write a menu driven program that will create a data file containing the list of
telephone numbers in the following form John 23456 Ahmed 9876 ……….. ……… Use a
class object to store each set of data, access the file created and implement
the following tasks I. Determine the telephone number of specified person II.
Determine the name if telephone number is known III. Update the telephone
number, whenever there is a change.
16.
Write a C++ program that creates an output file, writes information to it,
closes the file and open it again as an input file and read the information
from the file.
17.
Write a C++ program using command line arguments to search for a word in a file
and replace it with the specified word. The usage of the program is shown
below. $ change <old word> <new word> <file name>
18.
Write a function template selection Sort. Write a program that inputs, sorts
and outputs an integer array and a float array.
19. You
are the owner of a hardware store and need to keep an inventory that can tell
you what different tools you have, how many of each you have on hand and the
cost of each one. Write a program that initializes the random-access file
hardware.dat to 100 empty records, lets you input the data concerning each
tool, enables you to list all your tools, lets you delete a record for a tool
that you no longer have and lets you update any information in the file. The
tool identification number should be the record number. Use the following
information to start your file: Record # Tool name Quantity Cost 3 Electric
sander 7 57.98 17 Hammer 76 11.99 24 Jig saw 21 11.00 39 Lawn mower 3 79.50 56
Power saw 18 99.99
Group C
20. Write C++ program using STL for
implementation of Singly, doubly and circular linked list.
21. Write C++ program using STL for implementation
of stack & queue using SLL
22.
Write C++ program using STL to add binary numbers (assume one bit as one
number); use STL stack.
23. Write C++ program using STL for Dqueue (Double
ended queue)
24.
Write C++ program using STL for Sorting and searching with user-defined records
such as Person Record (Name, birth date, telephone no), item record (item code,
item name, quantity and cost)
*********************************For Reference*************************************
/*****************************************************
ASSIGNMENT NO: 2
Implement a class Complex which represents the Complex Number data type.
Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.
*****************************************************/
#include<iostream> //including header files
using namespace std; //declaring the scope of program
class complex //class name "complex"
{
public:
float real,img; //declared variable of type float
complex() //default constructor
{
}
complex operator+ (complex);
complex operator* (complex);
friend ostream &operator<<(ostream &,complex&);
friend istream &operator<<(istream &,complex&);
};
complex complex:: operator + (complex obj)
{
complex temp;
temp.real=real+obj.real;
temp.img=img+obj.img;
return (temp);
}
istream &operator >> (istream &is,complex &obj)
{
is>>obj.real;
is>>obj.img;
return is;
}
ostream &operator<<(ostream &outt,complex &obj)
{
outt<<""<<obj.real;
outt<<"+"<<obj.img<<"i";
return outt;
}
complex complex :: operator * (complex obj)
{
complex temp;
temp.real=real*obj.real-img*obj.img;
temp.img=real*obj.img+img*obj.real;
return (temp);
}
int main()
{
complex a,b,c,d;
cout<<"\nEnter first complex number\n";
cout<<"\nEnter real and imaginary:\t";
cin>>a;
cout<<"Enter second complex number \n";
cout<<"\nEnter real and imaginary:\t";
cin>>b;
cout<<"\n\tArithmetic operations";
c=a+b;
cout<<"\n\tAddition =";
cout<<c;
d=a*b;
cout<<"\n\tMultiplication=";
cout<<d;
cout<<endl;
return 0;
}
/************************************************
OUTPUT:
gescoe@hadoopmaster:~/Desktop$ g++ aaa.cpp
gescoe@hadoopmaster:~/Desktop$./a.out
Enter first complex number
Enter real and imaginary: 5
5
Enter second complex number
Enter real and imaginary: 5
5
Arithmetic operations
Addition =10+10i
Multiplication=0+50i
************************************************/
/*****************************************************
ASSIGNMENT NO: 2
Implement a class Complex which represents the Complex Number data type.
Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.
*****************************************************/
#include<iostream> //including header files
using namespace std; //declaring the scope of program
class complex //class name "complex"
{
public:
float real,img; //declared variable of type float
complex() //default constructor
{
}
complex operator+ (complex);
complex operator* (complex);
friend ostream &operator<<(ostream &,complex&);
friend istream &operator<<(istream &,complex&);
};
complex complex:: operator + (complex obj)
{
complex temp;
temp.real=real+obj.real;
temp.img=img+obj.img;
return (temp);
}
istream &operator >> (istream &is,complex &obj)
{
is>>obj.real;
is>>obj.img;
return is;
}
ostream &operator<<(ostream &outt,complex &obj)
{
outt<<""<<obj.real;
outt<<"+"<<obj.img<<"i";
return outt;
}
complex complex :: operator * (complex obj)
{
complex temp;
temp.real=real*obj.real-img*obj.img;
temp.img=real*obj.img+img*obj.real;
return (temp);
}
int main()
{
complex a,b,c,d;
cout<<"\nEnter first complex number\n";
cout<<"\nEnter real and imaginary:\t";
cin>>a;
cout<<"Enter second complex number \n";
cout<<"\nEnter real and imaginary:\t";
cin>>b;
cout<<"\n\tArithmetic operations";
c=a+b;
cout<<"\n\tAddition =";
cout<<c;
d=a*b;
cout<<"\n\tMultiplication=";
cout<<d;
cout<<endl;
return 0;
}
/************************************************
OUTPUT:
gescoe@hadoopmaster:~/Desktop$ g++ aaa.cpp
gescoe@hadoopmaster:~/Desktop$./a.out
Enter first complex number
Enter real and imaginary: 5
5
Enter second complex number
Enter real and imaginary: 5
5
Arithmetic operations
Addition =10+10i
Multiplication=0+50i
************************************************/
--------------------------------------------------------------------------------------------------------------------------
/************************************************************
Assignment Number A:-5
Write a C++ program create a calculator for an arithmetic operator (+, -, *, /). The program
should take two operands from user and performs the operation on those two operands
depending upon the operator entered by user. Use a switch statement to select the operation.
Finally, display the result.
Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n
*************************************************************/
#include<iostream> //including header files
using namespace std; //standard library
int main() //initializing main function
{
char c; //variable declearation
float n1,n2;
cout<<"Enter the operation to be perform\n +\t-\t*\t/\t";//operation to be performed
cin>>c;
cout<<"Enter first number:\t"; //printing first number
cin>>n1; //scaning first number
cout<<"Enter second number:\t"; //printing second number
cin>>n2; //scaning second number
switch (c) //using switch case
{
case'+':cout<<n1+n2<<"\n";
break;
case'-':cout<<n1-n2<<"\n";
break;
case'*':cout<<n1*n2<<"\n";
break;
case'/':cout<<n1/n2<<"\n";
break;
default: cout<<"Enter your valid choice";
}
return 0;
}
/******************************************************
OUTPUT:-
gescoe@hadoopslave1:~/Desktop$ g++ assignment1.cpp
gescoe@hadoopslave1:~/Desktop$ ./a.out
Enter the operation to be perform
+ - * /
+
Enter first number: 5
Enter second number: 5
10
gescoe@hadoopslave1:~/Desktop/kunal$ ./a.out
Enter the operation to be perform
+ - * /
-
Enter first number: 10
Enter second number: 5
5
gescoe@hadoopslave1:~/Desktop/kunal$ ./a.out
Enter the operation to be perform
+ - * /
*
Enter first number: 11
Enter second number: 5
55
gescoe@hadoopslave1:~/Desktop/kunal$ ./a.out
Enter the operation to be perform
+ - * /
/
Enter first number: 25
Enter second number: 5
5
*******************************************************/
--------------------------------------------------------------------------------------------------------------------------
/*********************************************************************************
Assignment no : A7
Title : Implement C++ program to write a class template to represent a generic
vector. Include following member functions:
To create the vector.
To modify the value of a given element
To multiply by a scalar value
To display the vector in the form (10,20,30,...)
*********************************************************************************/
#include<iostream> // ANSI standard header file
#include<vector>
using namespace std; //declared scope of program
void display (vector<int>&v) //function declaration
{
for(int i=0;i<v.size();i++)
{
cout<<" "<<v[i];
}
cout<<" \n";
}
int main() // main function
{
vector<int> v;
cout<<"\n Initial Size: "<<v.size();
int x;
cout<<"\n Enter The 5 Element: ";
for(int i=0;i<5;i++)
{
cin>>x;
v.push_back(x);
}
cout<<"\n Size After Insertion: "<<v.size();
cout<<"\n Vector Element: ";
display(v);
cout<<"\nVector Element After insertion of 3 at End of Vector: ";
v.push_back(3);
display(v);
vector<int>::iterator itr=v.begin();
itr=itr+3;
v.insert(itr,9);
cout<<"\n Content After Insertion 9 at position 3rd: ";
display(v);
v.erase(v.begin()+3,v.begin()+5);
cout<<"\n After Erasing(3rd-4th position): ";
display(v);
}
/*=============================OUTPUT===============================================
gescoe@hadoopmaster:~/Desktop/g++ Assignment7.cpp
gescoe@hadoopmaster:~/Desktop/./a.out
Initial Size: 0
Enter The 5 Element: 5 4 3 2 1
Size After Insertion: 5
Vector Element: 5 4 3 2 1
Vector Element After insertion of 3 at End of Vector: 5 4 3 2 1 3
Content After Insertion 9 at position 3rd: 5 4 3 9 2 1 3
After Erasing(3rd-4th position): 5 4 3 1 3
=================================================================================*/
--------------------------------------------------------------------------------------------------------------------------
/***********************************************************************************************
ASSIGNMENT NO:A-6
TITLE : Develop an object oriented program in C++ to create a database of student information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete.
*********************************************************************/
using namespace std;
#include<iostream>
#include<string.h> //for handling string eg.strcpy()
#include<ctime> //current date and time of system
class person
{
private:
char *name,*blood,*address; //declared data variable
int dob;
long int insurance,tel,license;
float height,weight;
static int count; //static variables
public:
person() //default & dynamic constructor
{
count++;
cout<<"-----------------default information------------";
name=new char[13]; //allocate dynamic memory using new operator
strcpy(name,"default name");
blood=new char[3];
strcpy(blood,"O+");
address=new char[20];
strcpy(address,"Nasik");
insurance=542451;
tel=+45876112;
license=19122013;
height=5.5;
weight=100;
dob=1998;
display();
}
person(char n[],char b[],int yy,char add[],float w,
long int in,long int tn,long int ln,float h) //parameterized constructor
{
count++;
int len;
len=strlen(n);
name=new char[len+1];
strcpy(name,n);
len=strlen(b);
blood=new char[len+1];
strcpy(blood,b);
len=strlen(add);
address=new char[len+1];
strcpy(address,add);
dob=yy;
insurance=in;
tel=tn;
license=ln;
height=h;
weight=w;
}
void display(); //display function declaration
static int displaycount() //static member function
{
cout<<"\n NUMBER OF ENTRIES ARE:"<<count<<endl;
}
~person() //destructor having same name like construct
{
cout<<"\n destructor called...";
}
};
int person::count=0;
void person::display() //display function definition
{
cout<<"\n PERSON NAME:"<<name;
cout<<"\n BLOOD GROUP:"<<blood;
cout<<"\n DATE OF BIRTH:"<<dob;
cout<<"\n CONTACT ADDRESS:"<<address;
cout<<"\n INSURANCE NUMBER:"<<insurance;
cout<<"\n TELEPHONE NUMBER:"<<tel;
cout<<"\n LICENSE NUMBER:"<<license;
cout<<"\n HEIGHT:"<<height;
cout<<"\n WEIGHT:"<<weight<<endl;
}
int main()//initializing main function
{
int ch; //variable declaration
person *p[20],*temp;
char pname[15],pblood[5],paddress[20];
long int pinsurance,ptel,plicense;
float pheight,pweight;
int cnt=0,dd;
do
{
cout<<"\n --------MENU--------"<<endl;
cout<<"\n 1.DEFAULT CONSTRUCTER ";
cout<<"\n 2.PARAMETERIZED CONSTRUCTOR";
cout<<"\n 3.NUMBER OF ENTRIES";
cout<<"\n 4.DISPLAY";
cout<<"\n 5.EXIT";
cout<<"\n ENTER THE CHOICE:";
cin>>ch;
switch(ch)
{
case 1: //DEFAULT CONSTRUCTER INFORMATION
p[cnt]=new person();
cnt=cnt+1;
break;
case 2: //PARAMETERIZED CONSTRUCTOR INFORMATION
cout<<"\n ENTER THE PERSON NAME:";
cin>>pname;
cout<<"\n ENTER THE PERSON BLOOD GROUP:";
cin>>pblood;
cout<<"\n ENTER DATE OF BIRTH:";
cin>>dd;
cout<<"\n ENTER THE PERSON CONTACT ADDRESS:";
cin>>paddress;
cout<<"\n ENTER THE PERSON INSURANCE NUMBER:";
cin>>pinsurance;
cout<<"\n ENTER THE PERSON TELEPHONE NUMBER:";
cin>>ptel;
cout<<"\n ENTER THE PERSON LICENSE NUMBER:";
cin>>plicense;
cout<<"\n ENTER THE PERSON HEIGHT:";
cin>>pheight;
cout<<"\n ENTER THE PERSON WEIGHT:";
cin>>pweight;
p[cnt]=new person(pname,pblood,dd,paddress,pinsurance,ptel,plicense,pheight,pweight);
cnt=cnt+1;
break;
case 3: //NUMBER OF ENTRIES INFORMATION
person::displaycount();
break;
case 4:
for(int i=0;i<cnt;i++)
{
p[i]->display();
}
break;
case 5:
return 0;
}
} while(ch!=4);
}
/**************************************************************
OUTPUT:-
gescoe@hadoopmaster:~/Desktop$ g++ Assignment6.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
---------------------------MENU--------------------------
1.DEFAULT CONSTRUCTER
2.PARAMETERIZED CONSTRUCTOR
3.NUMBER OF ENTRIES
4.DISPLAY
5.EXIT
ENTER THE CHOICE:1
-----------------default information------------
PERSON NAME:default name
BLOOD GROUP:O+
DATE OF BIRTH:1998
CONTACT ADDRESS:Nasik
INSURANCE NUMBER:542451
TELEPHONE NUMBER:45876112
LICENSE NUMBER:19122013
HEIGHT:5.5
WEIGHT:100
---------------------------MENU--------------------------
1.DEFAULT CONSTRUCTER
2.PARAMETERIZED CONSTRUCTOR
3.NUMBER OF ENTRIES
4.DISPLAY
5.EXIT
ENTER THE CHOICE:2
ENTER THE PERSON NAME:KUNAL
ENTER THE PERSON BLOOD GROUP:O+
ENTER DATE OF BIRTH:1996
ENTER THE PERSON CONTACT ADDRESS:Dhule
ENTER THE PERSON INSURANCE NUMBER:4785669
ENTER THE PERSON TELEPHONE NUMBER:+613994152
ENTER THE PERSON LICENSE NUMBER:181162013
ENTER THE PERSON HEIGHT:5.8
ENTER THE PERSON WEIGHT:75
---------------------------MENU--------------------------
1.DEFAULT CONSTRUCTER
2.PARAMETERIZED CONSTRUCTOR
3.NUMBER OF ENTRIES
4.DISPLAY
5.EXIT
ENTER THE CHOICE:3
NUMBER OF ENTRIES ARE:2
---------------------------MENU--------------------------
1.DEFAULT CONSTRUCTER
2.PARAMETERIZED CONSTRUCTOR
3.NUMBER OF ENTRIES
4.DISPLAY
5.EXIT
ENTER THE CHOICE:4
PERSON NAME:KUNAL
BLOOD GROUP:O+
DATE OF BIRTH:1996
CONTACT ADDRESS:Dhule
INSURANCE NUMBER:613994152
TELEPHONE NUMBER:181162013
LICENSE NUMBER:5
HEIGHT:75
WEIGHT:4.78567e+06
---------------------------MENU--------------------------
1.DEFAULT CONSTRUCTER
2.PARAMETERIZED CONSTRUCTOR
3.NUMBER OF ENTRIES
4.DISPLAY
5.EXIT
ENTER THE CHOICE:5
**************************************************************/
--------------------------------------------------------------------------------------------------------------------------
/*******************************************************************
Write a function in C++ to count and display the number of lines not starting with alphabet 'A' present in a text file "STORY1.TXT".
Example:
If the file "STORY1.TXT" contains the following lines,
1 The rose is red.
2 A girl is playing there.
3 There is a playground.
4 An aeroplane is in the sky.
5 Numbers are not allowed in the password.
*********************************************************************/
#include<iostream> //ANSI standard header file
#include<fstream> // header file
using namespace std; //declared scope of the program
int main()
{
ifstream fin; //fin object created
fin.open("STORY1.TXT"); //open method
char str[100]; //lenth of string
int count=-1; //count initialized by zero
while(!fin.eof())
{
fin.getline(str,100);
if(str[0]!='A') //first position of line is not equal to 'A'
count++;
}
cout<<"Number of lines not starting with A are" <<"\t" <<count <<"\n";
fin.close(); //close method to close the file
return 0;
}
/*********************************************************************
OUTPUT
gescoe@hadoopmaster:~/Desktop$ g++ assign3.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
Number of lines not starting with A are 3
*********************************************************************/
-------------------------------------------------------------------------------------------------------------------------
/*************************************************************************
ASSIGNMENT NO: B-14
TITLE:- Crete User defined exception to check the following conditions
and throw the exception if thecriterion does not meet.
a. User has age between 18 and 55
b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month
c. User stays in Pune/ Mumbai/ Bangalore / Chennai
d. User has 4-wheeler
Accept age, Income, City, Vehicle from the user and
check for the conditions mentionedabove. If any of the
condition not met then throw the exception.
**************************************************************************/
#include<iostream> //standard input output header file
#include<stdexcept> //for handling exception
#include<typeinfo> //for variable datatype identification
#include<string> //string comparison purpose
#include<cstring> //for string manipulation in c/c++ for various fun. and array
using namespace std;
int main()
{
int i,n,age; //variable declaration
char car;
double income;
char city[20];
cout<<"\n Enter Age of person:";
cin>>age;
try
{
if(age<18||age>55)
throw runtime_error("\n Age is not between 18-55");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Enter income of person:";
cin>>income;
try
{
if(income<50000||income>100000)
throw runtime_error("\n Income is not int the range of 50000-100000");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Enter city of person:";
cin>>city;
try
{
if(strcmp(city, "Pune")&& strcmp(city, "Banglore")&& strcmp(city,"Chennai")&& strcmp(city, "Mumbai"))
throw runtime_error("\n Person is not staying in Pune/Mumbai/Banglore/Chennai");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Whether person holds car or not? (y/n):";
cin>>car;
try
{
if(car=='n')
throw runtime_error("\n Person should hold car");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Information of person is as follows:";
cout<<"\n Age:"<<age;
cout<<"\n Income:"<<income;
cout<<"\n Location:"<<city;
cout<<"\n Holding car or not?:"<<car;
return 0;
}
/*************************************************************
OUTPUT:-
gescoe@hadoopmaster:~/Desktop$ g++ Assignment14.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
Enter Age of person:50
Enter income of person:60000
Enter city of person:Mumbai
Whether person holds car or not? (y/n):y
Information of person is as follows:
Age:50
Income:60000
Location:Mumbai
Holding car or not?:y
**************************************************************/
--------------------------------------------------------------------------------------------------------------------------
/***********************Problem Statement******************************************
Assignment no:GroupB:15
Write a menu driven program that will create a data file containing the list of telephone numbers in the following form
John 23456
Ahmed 9876
........... .........
Use a class object to store each set of data, access the file created and implement the following tasks
I. Determine the telephone number of specified person
II. Determine the name if telephone number is known
III. Update the telephone number, whenever there is a change.
*******************************************************************/
#include<iostream> //invoke standard input/output libraries
#include<fstream> //invokes standard classes functions to read/write from/to the file
#include<string.h> //invokes standard string libraries
#include<iomanip> //invokes library functions to perform input/output manipulations
using namespace std; //invokes standard library functions
class person
{ //creation of class
public: //use of public access specifier
char name[10]; //declaration of variables
int phno;
void input_data() //creation of function
{
cout<<"\nEnter Name: "; //definition of function
cin>>name;
cout<<"\nEnter telephone number: ";
cin>>phno;
} //termination of function
void put_data()
{ //function definition
cout<<setw(20)<<name<<setw(10)<<phno<<endl; //setw() function allows i/o manipulations
}
}; //termination of class
int main()
{ //opening of main() function
person rec; //creating object
int phone, pos, ch, offset, i; //declaration of int variables
char nm[10];
char ans; //declaration of char variables
fstream fp; //creating object of class fstream
ifstream in; //creating object of class ifstream
ofstream out; //creating object of class ofstream
do
{ //opening of do-while loop
cout<<"\n\t\tMain Menu"; //display menu
cout<<"\n1.Read file\n2.Write file";
cout<<"\n3.Determine name if telephone no. is specified";
cout<<"\n4.Determine telephone no. if name is specified";
cout<<"\n5.Update telephone number";
cout<<"\nEnter your choice: ";
cin>>ch; //read choice
switch(ch)
{ //opening of switch case
case 1 :
in.open("test.txt", ios::in | ios::binary); //open file in integer & binary mode
cout<<"\nThe contents of file are: "; //displaying file contents
while(in.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
rec.put_data(); //function call
}
in.close(); //file closing function
break;
case 2 :
rec.input_data(); //function call
char ch;
cin.get(ch); //call get()
out.open("test.txt", ios::out | ios::app | ios::binary ); //open file in binary mode
out.write((char *)&rec, sizeof(rec)); //standard write function write(memory block, size block)
out.close(); //file closing function
break;
case 3 :
cout<<"\nEnter telephone no.: ";
cin>>phone; //read phone
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to set the offset & refernce pointer
//here offset is 0 & reference pointer points to the beginning
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{ //opening of while loop
if(phone==rec.phno)
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //termination of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); /seekp() used to get pointer of specific location for writing into file
fp.read((char *)&rec, sizeof(rec)); //standard read function read(memory block, size block)
cout<<"\nName is: "<<rec.name; //display name
fp.close(); //file closing function
break;
case 4 :
cout<<"\nEnter name: ";
cin>>nm; //read nm
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to set the offset & reference pointer
//here offset is 0 & reference pointer points to the beginning
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{ //opening of while loop
if((strcmp(nm,rec.name))==0)
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //termination of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); //seekp() used to get pointer of specific location for writing into file
fp.read((char *)&rec, sizeof(rec));
cout<<"\nTelephone number is: "<<rec.phno;
fp.close(); //closing of file
break;
case 5 :
cout<<"\nEnter name: ";
cin>>nm; //read nm
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to get pointer of specific location
//ios::beg is flag for beginning location
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
if((strcmp(nm,rec.name))==0) //compare name using string function
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //closing of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); //seekp() used to get pointer of specific location for writing into file
cout<<"\nCurrent phone no.: "<<rec.phno;
cout<<"\nEnter new phone no.: ";
cin>>phone; //read new phone no.
rec.phno=phone;
fp.write((char *)&rec, sizeof(rec))<<flush; //flush is used to clear the previous data
cout<<"\nRecord is updated";
fp.seekg(0); //seekg() used to get pointer of specific location
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
rec.put_data(); //function call
}
fp.close(); //file closing function
break;
} //termination of switch case
cout<<"\nDo you want to continue ? ";
cin>>ans; //read ans
}while(ans=='y' || ans =='Y'); //termination of do-while loop
return 0;
} //termination of main() function
/*********************Output***************************************************
student@student-OptiPlex-3020:~$ cd Desktop
student@student-OptiPlex-3020:~/Desktop$ g++ grpb.cpp
student@student-OptiPlex-3020:~/Desktop$ ./a.out
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 1
The contents of file are:
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 2
Enter Name: Mina
Enter telephone number: 1234
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 2
Enter Name: Rina
Enter telephone number: 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 1
The contents of file are: Mina 1234
Rina 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 3
Enter telephone no.: 1234
Name is: Mina
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 4
Enter name: Rina
Telephone number is: 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 5
Enter name: Mina
Current phone no.: 1234
Enter new phone no.: 9010
Record is updated Mina 9010
Rina 5678
Do you want to continue ? n
student@student-OptiPlex-3020:~/Desktop$
*/
-------------------------------------------------------------------------------------------------------------------------
/*************************************************************************
Write a C++ program that creates an output employee, writes information to it, closes the employee and
open it again as an input employee and read the information from the employee.
**************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
class employee
{
char name[20];
int emp_id;
float salary;
public:
void accept()
{
cin>>name;
cin>>emp_id;
cin>>salary;
}
void display()
{
cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;
}
};
int main()
{
employee o[5];
fstream f;
int i,n;
f.open("input.txt"); //create employee
cout<<"\n How many employee information wanted to store:";
cin>>n;
cout<<"\n Enter information of 3 employees (Enter name, emp_id, salary)";
for(i=0;i<n;i++)
{
cout<<"\n Enter information of "<<i<<" employee";
o[i].accept(); //accept input from user
f.write((char *)&o[i], sizeof(o[i])); //write object in employee
}
f.close();
f.open("input.txt", ios::in);
cout<<"\n Information of employee is as follows";
for(i=0;i<n;i++)
{
f.read((char*)&o[i], sizeof(o[i])); //read data from employee
o[i].display();
}
f.close();
return 0;
}
/******************************OUTPUT*****************************************
srl@srl-Lenovo-G550:~/Desktop/OOPL$ g++ b16.cpp
srl@srl-Lenovo-G550:~/Desktop/OOPL$ ./a.out
How many employee information wanted to store:3
Enter information of 3 employees (Enter name, emp_id, salary)
Enter information of 0 employeea
1
100
Enter information of 1 employeeb
2
200
Enter information of 2 employeec
3
300
Information of employee is as follows
a 1 100
b 2 200
c 3 300
*/
ASSIGNMENT NO: B-14
TITLE:- Crete User defined exception to check the following conditions
and throw the exception if thecriterion does not meet.
a. User has age between 18 and 55
b. User stays has income between Rs. 50,000 – Rs. 1,00,000 per month
c. User stays in Pune/ Mumbai/ Bangalore / Chennai
d. User has 4-wheeler
Accept age, Income, City, Vehicle from the user and
check for the conditions mentionedabove. If any of the
condition not met then throw the exception.
**************************************************************************/
#include<iostream> //standard input output header file
#include<stdexcept> //for handling exception
#include<typeinfo> //for variable datatype identification
#include<string> //string comparison purpose
#include<cstring> //for string manipulation in c/c++ for various fun. and array
using namespace std;
int main()
{
int i,n,age; //variable declaration
char car;
double income;
char city[20];
cout<<"\n Enter Age of person:";
cin>>age;
try
{
if(age<18||age>55)
throw runtime_error("\n Age is not between 18-55");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Enter income of person:";
cin>>income;
try
{
if(income<50000||income>100000)
throw runtime_error("\n Income is not int the range of 50000-100000");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Enter city of person:";
cin>>city;
try
{
if(strcmp(city, "Pune")&& strcmp(city, "Banglore")&& strcmp(city,"Chennai")&& strcmp(city, "Mumbai"))
throw runtime_error("\n Person is not staying in Pune/Mumbai/Banglore/Chennai");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Whether person holds car or not? (y/n):";
cin>>car;
try
{
if(car=='n')
throw runtime_error("\n Person should hold car");
}
catch(const exception& e)
{
cout<<"Caught exception:"<<e.what()<<'\n';
}
cout<<"\n Information of person is as follows:";
cout<<"\n Age:"<<age;
cout<<"\n Income:"<<income;
cout<<"\n Location:"<<city;
cout<<"\n Holding car or not?:"<<car;
return 0;
}
/*************************************************************
OUTPUT:-
gescoe@hadoopmaster:~/Desktop$ g++ Assignment14.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
Enter Age of person:50
Enter income of person:60000
Enter city of person:Mumbai
Whether person holds car or not? (y/n):y
Information of person is as follows:
Age:50
Income:60000
Location:Mumbai
Holding car or not?:y
**************************************************************/
--------------------------------------------------------------------------------------------------------------------------
/***********************Problem Statement******************************************
Assignment no:GroupB:15
Write a menu driven program that will create a data file containing the list of telephone numbers in the following form
John 23456
Ahmed 9876
........... .........
Use a class object to store each set of data, access the file created and implement the following tasks
I. Determine the telephone number of specified person
II. Determine the name if telephone number is known
III. Update the telephone number, whenever there is a change.
*******************************************************************/
#include<iostream> //invoke standard input/output libraries
#include<fstream> //invokes standard classes functions to read/write from/to the file
#include<string.h> //invokes standard string libraries
#include<iomanip> //invokes library functions to perform input/output manipulations
using namespace std; //invokes standard library functions
class person
{ //creation of class
public: //use of public access specifier
char name[10]; //declaration of variables
int phno;
void input_data() //creation of function
{
cout<<"\nEnter Name: "; //definition of function
cin>>name;
cout<<"\nEnter telephone number: ";
cin>>phno;
} //termination of function
void put_data()
{ //function definition
cout<<setw(20)<<name<<setw(10)<<phno<<endl; //setw() function allows i/o manipulations
}
}; //termination of class
int main()
{ //opening of main() function
person rec; //creating object
int phone, pos, ch, offset, i; //declaration of int variables
char nm[10];
char ans; //declaration of char variables
fstream fp; //creating object of class fstream
ifstream in; //creating object of class ifstream
ofstream out; //creating object of class ofstream
do
{ //opening of do-while loop
cout<<"\n\t\tMain Menu"; //display menu
cout<<"\n1.Read file\n2.Write file";
cout<<"\n3.Determine name if telephone no. is specified";
cout<<"\n4.Determine telephone no. if name is specified";
cout<<"\n5.Update telephone number";
cout<<"\nEnter your choice: ";
cin>>ch; //read choice
switch(ch)
{ //opening of switch case
case 1 :
in.open("test.txt", ios::in | ios::binary); //open file in integer & binary mode
cout<<"\nThe contents of file are: "; //displaying file contents
while(in.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
rec.put_data(); //function call
}
in.close(); //file closing function
break;
case 2 :
rec.input_data(); //function call
char ch;
cin.get(ch); //call get()
out.open("test.txt", ios::out | ios::app | ios::binary ); //open file in binary mode
out.write((char *)&rec, sizeof(rec)); //standard write function write(memory block, size block)
out.close(); //file closing function
break;
case 3 :
cout<<"\nEnter telephone no.: ";
cin>>phone; //read phone
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to set the offset & refernce pointer
//here offset is 0 & reference pointer points to the beginning
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{ //opening of while loop
if(phone==rec.phno)
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //termination of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); /seekp() used to get pointer of specific location for writing into file
fp.read((char *)&rec, sizeof(rec)); //standard read function read(memory block, size block)
cout<<"\nName is: "<<rec.name; //display name
fp.close(); //file closing function
break;
case 4 :
cout<<"\nEnter name: ";
cin>>nm; //read nm
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to set the offset & reference pointer
//here offset is 0 & reference pointer points to the beginning
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{ //opening of while loop
if((strcmp(nm,rec.name))==0)
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //termination of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); //seekp() used to get pointer of specific location for writing into file
fp.read((char *)&rec, sizeof(rec));
cout<<"\nTelephone number is: "<<rec.phno;
fp.close(); //closing of file
break;
case 5 :
cout<<"\nEnter name: ";
cin>>nm; //read nm
fp.open("test.txt", ios::ate | ios::in | ios::out | ios:: binary); //open file in int & binary mode
fp.seekg(0, ios::beg); //seekg() used to get pointer of specific location
//ios::beg is flag for beginning location
pos=-1; i=0;
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
if((strcmp(nm,rec.name))==0) //compare name using string function
{ //opening of if statement
pos=i;
break;
} //closing of if statement
i++;
} //closing of while loop
offset=pos*sizeof(rec); //setting of offset
fp.seekp(offset); //seekp() used to get pointer of specific location for writing into file
cout<<"\nCurrent phone no.: "<<rec.phno;
cout<<"\nEnter new phone no.: ";
cin>>phone; //read new phone no.
rec.phno=phone;
fp.write((char *)&rec, sizeof(rec))<<flush; //flush is used to clear the previous data
cout<<"\nRecord is updated";
fp.seekg(0); //seekg() used to get pointer of specific location
while(fp.read((char *)&rec, sizeof(rec))) //standard read function read(memory block, size block)
{
rec.put_data(); //function call
}
fp.close(); //file closing function
break;
} //termination of switch case
cout<<"\nDo you want to continue ? ";
cin>>ans; //read ans
}while(ans=='y' || ans =='Y'); //termination of do-while loop
return 0;
} //termination of main() function
/*********************Output***************************************************
student@student-OptiPlex-3020:~$ cd Desktop
student@student-OptiPlex-3020:~/Desktop$ g++ grpb.cpp
student@student-OptiPlex-3020:~/Desktop$ ./a.out
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 1
The contents of file are:
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 2
Enter Name: Mina
Enter telephone number: 1234
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 2
Enter Name: Rina
Enter telephone number: 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 1
The contents of file are: Mina 1234
Rina 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 3
Enter telephone no.: 1234
Name is: Mina
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 4
Enter name: Rina
Telephone number is: 5678
Do you want to continue ? y
Main Menu
1.Read file
2.Write file
3.Determine name if telephone no. is specified
4.Determine telephone no. if name is specified
5.Update telephone number
Enter your choice: 5
Enter name: Mina
Current phone no.: 1234
Enter new phone no.: 9010
Record is updated Mina 9010
Rina 5678
Do you want to continue ? n
student@student-OptiPlex-3020:~/Desktop$
*/
-------------------------------------------------------------------------------------------------------------------------
/*************************************************************************
Write a C++ program that creates an output employee, writes information to it, closes the employee and
open it again as an input employee and read the information from the employee.
**************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
class employee
{
char name[20];
int emp_id;
float salary;
public:
void accept()
{
cin>>name;
cin>>emp_id;
cin>>salary;
}
void display()
{
cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;
}
};
int main()
{
employee o[5];
fstream f;
int i,n;
f.open("input.txt"); //create employee
cout<<"\n How many employee information wanted to store:";
cin>>n;
cout<<"\n Enter information of 3 employees (Enter name, emp_id, salary)";
for(i=0;i<n;i++)
{
cout<<"\n Enter information of "<<i<<" employee";
o[i].accept(); //accept input from user
f.write((char *)&o[i], sizeof(o[i])); //write object in employee
}
f.close();
f.open("input.txt", ios::in);
cout<<"\n Information of employee is as follows";
for(i=0;i<n;i++)
{
f.read((char*)&o[i], sizeof(o[i])); //read data from employee
o[i].display();
}
f.close();
return 0;
}
/******************************OUTPUT*****************************************
srl@srl-Lenovo-G550:~/Desktop/OOPL$ g++ b16.cpp
srl@srl-Lenovo-G550:~/Desktop/OOPL$ ./a.out
How many employee information wanted to store:3
Enter information of 3 employees (Enter name, emp_id, salary)
Enter information of 0 employeea
1
100
Enter information of 1 employeeb
2
200
Enter information of 2 employeec
3
300
Information of employee is as follows
a 1 100
b 2 200
c 3 300
*/
--------------------------------------------------------------------------------------------------------------------------
/****************************************************
Assignment No:- 18
Write a function template selection Sort. Write a program that inputs, sorts and outputs aninteger array and a float array.
*****************************************************/
#include<iostream> //including header files
#define size 10
using namespace std; //standard library
int n;
template<class T> //template class
void selection(T A[size]) //function declearation
{
int i,j,min;
T temp;
for(i=0;i<=n-2;i++)
{ min=i;
for(j=i+1;j<=n-1;j++)
{ if(A[j]<A[min])
min=j;
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
cout<<"\n The sorted list is:...\n"; //printing sorted array of integer & float nos.
for(i=0;i<n;i++)
cout<<"\t"<<A[i];
}
int main() //initializing main function
{
int i,A[size]; //declear variable of type integer
float B[size]; //declear variable of type float
cout<<"\n\t\tSelection Sort\n";
cout<<"\n\tHandling Interger Elements\n";
cout<<"\nHow Many Elements Are There???\n";
cin>>n;
cout<<"\nEnter the integer numbers";
for(i=0;i<n;i++)
cin>>A[i]; //scanning array of integer nos
selection(A);
cout<<"\n\t\tHandling Float Elements\n";
cout<<"\nHow Many Elements Are There???\n";
cin>>n;
cout<<"\n Enter the Float numbers";
for(i=0;i<n;i++)
cin>>B[i]; //scanning array of float nos
selection(B);
cout<<"\n";
return 0;
}
/********************************************************
OUTPUT:-
gescoe@hadoopmaster:~/Desktop$ g++ Assignment18.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
Selection Sort
Handling Interger Elements
How Many Elements Are There???
5
Enter the integer numbers10
99
53
87
5
The sorted list is:...
5 10 53 87 99
Handling Float Elements
How Many Elements Are There???
5
Enter the Float numbers10.10
99.99
53.53
5.5
87.78
The sorted list is:...
5.5 10.1 53.53 87.78 99.99
*********************************************************/
-------------------------------------------------------------------------------------------------------------------------
/*********************************************************
ASSIGNMENT NO:- C23
Write C++ program using STL for Dqueue (Double ended queue)
*********************************************************/
#include<deque> // Header that defines the deque container class
#include<stdio.h> //standard input output header file
#include<iostream> //standard input output stream header file
using namespace std; //Decleared scope of the program
int main() //main function starts
{
deque <int>dq; //creates deque
deque <int>::iterator p;//iterator is an object that enables a programmer to traverse contents of container
char cc; //variable declearation
int val,ch;
do //do while loop starts
{
cout<<"\n\nMENU:";
cout<<"\n1. Enter element at front\n";
cout<<"\n2. Enter element at Rear \n";
cout<<"\n3. Delet element at front\n";
cout<<"\n4. Delet element at Rear\n";
cout<<"\n5. Display front element\n";
cout<<"\n6. Display back element\n";
cout<<"\n7. Display the element of Deque\n\n";
cout<<"\nEnter choice:";
cin>>ch; //Accepting choice from user
switch(ch) //using switch case
{
case 1:
cout<<"\n\nEnter the element to be inserted:";
cin>>val;
dq.push_front(val);
cout<<"\nElement inserted at front:"<<dq.front();
break;
case 2:
cout<<"\n\nEnter the element to be inserted:";
cin>>val;
dq.push_back(val);
cout<<"\nElement inserted at rear end:"<<dq.back();
break;
case 3:
cout<<"\nElement to be deleted at front:"<<dq.front();
dq.pop_front();
break;
case 4:
cout<<"\nElement to be deleted at rear:"<<dq.back();
dq.pop_back();
break;
case 5:
cout<<"\nElement at front:"<<dq.front();
break;
case 6:
cout<<"\nElement at rear:"<<dq.back();
break;
case 7:
if(dq.empty()==1)
cout<<"\n\nDEQUE is EMPTY..";
else
{
cout<<"\n\nDisplaying the elements of DEQUE...\n";
for(p=dq.begin();p<dq.end();p++)
cout<<" "<<*p<<"\t";
}
break;
}
cout<<"\n\nCONTINUE MENU:";
cin>>cc;
}while(cc=='y');
return 0;
}
/*****************************************************
OUTPUT:
gescoe@hadoopmaster:~/Desktop$ g++ C23.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:1
Enter the element to be inserted:1
Element inserted at front:1
CONTINUE MENU:1
gescoe@hadoopmaster:~/Desktop/kunal$ clear
gescoe@hadoopmaster:~/Desktop/kunal$ g++ C23.cpp
gescoe@hadoopmaster:~/Desktop/kunal$ ./a.out
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:
1
Enter the element to be inserted:1
Element inserted at front:1
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:1
Enter the element to be inserted:2
Element inserted at front:2
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:2
Enter the element to be inserted:3
Element inserted at rear end:3
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:2
Enter the element to be inserted:4
Element inserted at rear end:4
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:7
Displaying the elements of DEQUE...
2 1 3 4
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:5
Element at front:2
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:6
Element at rear:4
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:3
Element to be deleted at front:2
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:4
Element to be deleted at rear:4
CONTINUE MENU:y
MENU:
1. Enter element at front
2. Enter element at Rear
3. Delet element at front
4. Delet element at Rear
5. Display front element
6. Display back element
7. Display the element of Deque
Enter choice:7
Displaying the elements of DEQUE...
1 3
CONTINUE MENU:n
*****************************************************/
-------------------------------------------------------------------------------------------------------------------------
/* Write C++ program using STL to add binary numbers (assume one bit as one number); use
STL stack.
***************************************************************************
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
stack <int> read();
void display(stack<int> &s);
stack <int>add(stack<int> &s1,stack<int> &s2);
int main()
{
stack<int> s1,s2,s3;
int ch;
do
{
cout<<"\n1.Addition of two binary numbers\n2.Quit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: s1=read();
s2=read();
s3=add(s1,s2);
display(s3);
break;
}
}while(ch!=2);
return 0;
}
stack<int> read()
{
char a[40];
stack<int> s;
cout<<"\n Enter a binary number: ";
cin>>a;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='1')
s.push(1);
else
if(a[i]=='0')
s.push(0);
}
return s;
}
stack<int> add(stack<int> &s1,stack<int> &s2)
{
stack<int> s;
int sum,carry=0,bit1,bit2;
while(!s1.empty() || !s2.empty())
{
bit1=bit2=0;
if(!s1.empty())
{
bit1=s1.top();
s1.pop();
}
if(!s2.empty())
{
bit2=s2.top();
s2.pop();
}
sum=(bit1+bit2+carry)%2;
carry=(bit1+bit2+carry)/2;
s.push(sum);
}
if(carry==1)
s.push(1);
return s;
}
void display(stack<int> &s)
{
cout<<"\n Sum= ";
while(!s.empty())
{
cout<<s.top();
s.pop();
}
}
/*****************************OUTPUT*****************************************
1.Addition of two binary numbers
2.Quit
Enter your choice:1
Enter a binary number: 1010101011
Enter a binary number: 1110001011
Sum= 11000110110
1.Addition of two binary numbers
2.Quit
Enter your choice:2
*************************************************************************/
---------------------------------------------------------------------------------------------------------------------
/**********************************************
ASSIGNMENT NO : C24
TITLE : Write a C++ program to implement
searching & Sorting on Item record using STL.
**********************************************/
#include <iostream> //standard input output stream header file
#include <algorithm> //The STL algorithms are generic because they can operate on a variety of data structures
#include <vector> //The header file for the STL vector library is vector.
using namespace std;
class Item //Item class is created
{
public: char name[10]; //varible declaration
int quantity;
int cost;
int code;
bool operator==(const Item& i1) //Boolean operators allow you to create more complex conditional statements
{
if(code==i1.code) //operator will return 1 if the comparison is true, or 0 if the comparison is false
return 1;
return 0;
}
bool operator<(const Item& i1)
{
if(code<i1.code) //operator will return 1 if the comparison is true, or 0 if the comparison is false
return 1;
return 0;
}
};
vector<Item> o1;
void print(Item &i1);
void display();
void insert();
void search();
void dlt();
bool compare(const Item &i1, const Item &i2)
{
//if (i1.name != i2.name) return i1.cost < i2.cost;
return i1.cost < i2.cost;
}
int main()
{
int ch;
do
{
cout<<"\n***** Menu *****";
cout<<"\n1.Insert";
cout<<"\n2.Display";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Delete";
cout<<"\n6.Exit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: display();
break;
case 3: search();
break;
case 4: sort(o1.begin(),o1.end(),compare);
cout<<"\n\n Sorted on Cost";
display();
break;
case 5: dlt();
break;
case 6: exit(0);
}
}while(ch!=7);
return 0;
}
void insert()
{
Item i1;
cout<<"\nEnter Item Name:";
cin>>i1.name;
cout<<"\nEnter Item Quantity:";
cin>>i1.quantity;
cout<<"\nEnter Item Cost:";
cin>>i1.cost;
cout<<"\nEnter Item Code:";
cin>>i1.code;
o1.push_back(i1);
}
void display()
{
for_each(o1.begin(),o1.end(),print);
}
void print(Item &i1)
{
cout<<"\n";
cout<<"\nItem Name:"<<i1.name;
cout<<"\nItem Quantity:"<<i1.quantity;
cout<<"\nItem Cost:"<<i1.cost;
cout<<"\nItem Code:"<<i1.code;
}
void search()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to search:";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
cout<<"\nNot found.";
else
{
cout<<"\nFound.";
}
}
void dlt()
{
vector<Item>::iterator p;
Item i1;
cout<<"\nEnter Item Code to delete:";
cin>>i1.code;
p=find(o1.begin(),o1.end(),i1);
if(p==o1.end())
cout<<"\nNot found.";
else
{
o1.erase(p);
cout<<"\nDeleted.";
}
}
/******************************************
OUTPUT:
gescoe@hadoopmaster:~/Desktop$ g++ AssignmentC24.cpp
gescoe@hadoopmaster:~/Desktop$ ./a.out
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1
Enter Item Name:mouse
Enter Item Quantity:5
Enter Item Cost:100
Enter Item Code:101
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1
Enter Item Name:cpu
Enter Item Quantity:5
Enter Item Cost:15000
Enter Item Code:102
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1
Enter Item Name:monitor
Enter Item Quantity:5
Enter Item Cost:8000
Enter Item Code:103
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1
Enter Item Name:keyboard
Enter Item Quantity:5
Enter Item Cost:500
Enter Item Code:104
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:1
Enter Item Name:speaker
Enter Item Quantity:5
Enter Item Cost:1000
Enter Item Code:105
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:2
Item Name:mouse
Item Quantity:5
Item Cost:100
Item Code:101
Item Name:cpu
Item Quantity:5
Item Cost:15000
Item Code:102
Item Name:monitor
Item Quantity:5
Item Cost:8000
Item Code:103
Item Name:keyboard
Item Quantity:5
Item Cost:500
Item Code:104
Item Name:speaker
Item Quantity:5
Item Cost:1000
Item Code:105
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:3
Enter Item Code to search:103
Found.
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:4
Sorted on Cost
Item Name:mouse
Item Quantity:5
Item Cost:100
Item Code:101
Item Name:keyboard
Item Quantity:5
Item Cost:500
Item Code:104
Item Name:speaker
Item Quantity:5
Item Cost:1000
Item Code:105
Item Name:monitor
Item Quantity:5
Item Cost:8000
Item Code:103
Item Name:cpu
Item Quantity:5
Item Cost:15000
Item Code:102
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:5
Enter Item Code to delete:105
Deleted.
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:2
Item Name:mouse
Item Quantity:5
Item Cost:100
Item Code:101
Item Name:keyboard
Item Quantity:5
Item Cost:500
Item Code:104
Item Name:monitor
Item Quantity:5
Item Cost:8000
Item Code:103
Item Name:cpu
Item Quantity:5
Item Cost:15000
Item Code:102
***** Menu *****
1.Insert
2.Display
3.Search
4.Sort
5.Delete
6.Exit
Enter your choice:6
******************************************/
No comments:
Post a Comment