You are on page 1of 33

DATE:27/11/2014

NO:29
AIM:write a program that demonstrates input output streams and functions
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char c,st[20];
clrscr();
cout<<"\nUSINGgetline() AND write() FUNCTIONS\n";
cout<<"enter the string\n";
cin.getline(st,10);
cout<<"string is :";
cout.write(st,10);
cout<<"\nUSING get() AND put() FUNCTIONS\n";
cout<<"enter a single character\n";
cin.get(c);
cout<<"character is :";
cout.put(c);
cout.precision(3);
cout<<"\nusing width(),precision(),AND fill() FUCTIONS\n";

cout.fill('*');
cout<<"the string is :";
cout.width(10);
cout<<st;
cout<<"\n";
cout.precision(3);
cout<<"\nfloat value :"<<123.6783;
getch();
}

OUTPUT
USINGgetline() AND write() FUNCTIONS
enter the string
simi
string is :vibi J>
USING get() AND put() FUNCTIONS
enter a single character
v
character is :r
using width(),precision(),AND fill() FUCTIONS
the string is :******vibi
float value :123.678

DATE:27/11/2014
NO:30
AIM: write a program that uses object for string manipulation function
#include <iostream>
using namespace std;
int main()
{
string s1;
string s2("New");
string s3("Delhi");
s1=s2;
cout<<"\nS1 is "<<s1;
s1="Standard C++";
cout<<"\nNew S1 becomes "<<s1;
string s4(s1);
cout<<"\nNew string "<<s4;
cout<<"\nEnter a string : ";

cin>>s4;
cout<<"Now the string is "<<s4;
s1=s2+s3;
cout<<"\nFirst string "<<s2;
cout<<"\nSecond string "<<s3;
cout<<"\nAfter concatenation "<<s1;
//Modifying string objects,,,,,Inserting a string into another
cout<<"\nPlace second string into first one ";
s1.insert(1,s2);
cout<<"\nModified string is "<<s1;
//Removing character in a string
cout<<"\nRemove 5 characters from first string";
s1.erase(1,5);
cout<<"\nNow the first string is "<<s1;
//Replacing characters in a string
cout<<"\nReplace middle # characters in second string with first one";
s2.replace(1,3,s1);
cout<<"\nNow the second string is "<<s2;
//Relational operations on string objects
cout<<"\nTwo new strings are created";
string s5("ABC");
string s6("XYZ");

cout<<s5<<" and "<<s6;


if(s5!=s6)
cout<<"\nFirst is not equal to second";
if(s5>s6)
cout<<"\nFirst is greater than second";
else
cout<<"\nSecond is greater than first";
int x=s5.compare(s2); //Using compare function
if(x=0)
cout<<"\nFirst = Second";
else if(x>0)
cout<<"\nFirst > Second";
else
cout<<"\nFirst < Second";
//Characteristics of strings
cout<<"\nSize of first string is "<<s5.size();
cout<<"\nLength of first string is "<<s5.length();
//Find a specified sub string
x=s5.find("BC");
cout<<"\nBC is found at "<<x;
//Find location of first occurence of the specified character
x=s5.find_first_of('C');

cout<<"\nC is found at "<<x;


cout<<"\nBefore swapping ";
and s6
cout<<s5<<s6;
s5.swap(s6);
cout<<"\nAfter swapping ";
cout<<s5<<s6;

// your code goes here

return 0;
}

OUTPUT
S1 is New
New S1 becomes Standard C++
New string Standard C++
Enter a string :kerala
Now the string is kerala
First string New
Second string Delhi
After concatenation NewDelhi
Place second string into first one
Modified string is NNewewDelhi
Remove 5 characters from first string
Now the first string is NDelhi
Replace middle # characters in second string with first one

//Swap the contents of s5

Now the second string is NNDelhi


Two new strings are createdABC and XYZ
First is not equal to second
Second is greater than first
First < Second
Size of first string is 3
Length of first string is 3
BC is found at 1
C is found at 2
Before swapping ABCXYZ
After swapping XYZABC

DATE:28/11/2014
NO:31
AIM:Define and overload function templates to search an item in an array
#include<iostream.h>
#include<conio.h>
void ser(int,int);
//void ser(float,float,int);
void linear(int,int);
template<class T>
//template<class D>
void ser(T a[],int n)
{

int i,j,mi,pos,f=0,lb,ub,k;
cout<<"Enter the elements to be searched : ";
cin>>k;
lb=0;
ub=n;
for(i=lb;i<=ub;i++)
{
mi=a[(lb+ub)/2];
pos=(lb+ub)/2;
if(mi==k)
{
f=1;
}
if(k<mi)
{
ub=pos;
continue;
}
if(k>mi)
{
lb=pos;
continue;
}

}
if(f==1) cout<<"Element is present";
else cout<<"Not present";
}
template<class T>
void linear(T a[],int n)
{
int k,i,f;
//cout<<"\nlinear\n";
cout<<"Enter the elements to be searched : ";
cin>>k;
for(i=0;i<n;i++)
{
if(a[i]==k)
f=1;
//p=i+1;
}
if(f==1)
{
cout<<"\nThe element is present";
}
else cout<<"Not present";
}

void main()
{
int a[10];
float b[10];//={1,2,3,4,5};
//T=a;
int n,i,k;
float h;
clrscr();
cout<<"Enter the limit : ";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nUsing binary search\n";
ser(a,n);
/*cout<<"\nFloat type value\n";
cout<<"\nEnter the elements : \n";
for(i=0;i<n;i++)
{
cin>>b[i];
}
cout<<"Enter the element to be searched";
cin>>h;

ser(b,h,n);

*/

cout<<"\nUsing linear search\n";


linear(a,n);
getch();
}

OUTPUT
Enter the limit : 3
Enter the elements
20
60
70
Using binary search
Enter the elements to be searched : 60
Element is present
Using linear search
Enter the elements to be searched : 80
Not present

DATE:01/12/2014
NO:32
AIM: Program to define a class template stack that represents a generic stack.
The member functions are create, push, pop and display.

OUTPUT

DATE:01/12/2014
NO:33
AIM: Create to files first and second. Store text information in the first file and
copy the contents of the first file to second file in reverse order.

OUTPUT

Date:02/12/2014
No:34
AIM: Create a file that stores the details of employees such as employee id,
employee name and salary. Write a menu driven program to perform the
following operations
a)
b)
c)
d)
e)

Add a record
Display all the records in that file
Modify a particular record
Search and display a particular record
Delete a record

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class employee
{
int empno;
char name[30];
float salary;
public:void getdata()
{

cout<<"Employee details"<<"\n";
cout<<"Number, Name, Salary"<<"\n";
cin>>empno>>name>>salary;
}
void putdata()
{
cout<<"The details are"<<"\n";
cout<<empno<<"\n"<<name<<"\n"<<salary<<"\n";
}
int getempno()
{
return empno;
}};
void add();
void show();
void modify();
void delet(int);
void search(int);
int main()
{
int choice;
do
{
clrscr();

cout<<"Main Menu"<<"\n";
cout<<"1. Add"<<"\n";
cout<<"2. Show"<<"\n";
cout<<"3. Modify"<<"\n";
cout<<"4. Delete "<<"\n";
cout<<"5. Search "<<"\n";
cout<<"6. Exit "<<"\n";
cout<<"Enter your choice"<<"\n";
cin>>choice;
switch(choice)
{
case 1: add();
break;
case 2: show();
break;
case 3: modify();
break;
case 4: cout<<"Enter the employee number to be
deleted"<<"\n";
int n;
cin>>n;
delet(n);
break;

case 5: cout<<"Enter the employee number to be


searched"<<"\n";
int x;
cin>>x;
search(x);
break;
case 6: cout<<"Terminating Program"<<"\n";
break;
default: cout<<"Enter a valid choice"<<"\n";
}
getch();
} while(choice !=6);
return 0;
}
void add()
{
ofstream ofile;
ofile.open("emp", ios::app | ios:: binary);
employee e;
cout<<"enter the data to be added/appended"<<"\n";
e.getdata();
ofile.write((char *) & e, sizeof e);
cout<<"The record is added"<<"\n";
}
void show()

{
ifstream ifile;
ifile.open("emp", ios::in | ios::binary);
employee e;
ifile.seekg(0);
cout<<"The records are"<<"\n";
while(ifile.read((char *) & e, sizeof e))
{
e.putdata();
}}
void modify()
{
fstream iofile;
iofile.open("emp",ios::in | ios::out | ios::ate | ios::binary);
employee e;
int total = iofile.tellg();
int num=total/sizeof(e);
cout<<"Total number of records

"<<num<<"\n";

cout<<"Enter the number of record to be updated"<<"\n";


int rno;
cin>>rno;
int pos=(rno -1)*sizeof(e);
iofile.seekp(pos);
cout<<"Enter new values for this record"<<"\n";

e.getdata();
iofile.write((char *) & e, sizeof e);
cout<<"Modified"<<"\n";
}
void search (int n)
{
employee e;
ifstream fp1;
fp1.open("emp",ios::binary);
while(fp1.read((char*)&e,sizeof(e)))
{
if(e.getempno()==n)
e.putdata();
}
fp1.close();
}
void delet(int n)
{
employee e;
ifstream fp1;
fp1.open("emp",ios::binary);
ofstream fp2;
fp2.open("Temp",ios::out|ios::binary);
while(fp1.read((char*)&e,sizeof(e)))
{
if(e.getempno()!=n)
fp2.write((char*)&e,sizeof(e));
}
fp1.close();

fp2.close();
remove("emp");
rename("Temp","emp");
cout<<"The record is successfully deleted";
}

OUTPUT
Main Menu
1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
1
enter the data to be added/appended
Employee details
Number, Name, Salary
1 sonu 15000
The record is added
Main Menu
1. Add
2. Show

3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
2
The records are
The details are
1
sonu
15000
Main Menu
1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
3
Total number of records

Enter the number of record to be updated


1

Enter new values for this record


Employee details
Number, Name, Salary
1 ashok 13000
Modified
Main Menu
1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
2
The records are
The details are
1
ashokh
13000
Main Menu
1. Add
2. Show
3. Modify

4. Delete
5. Search
6. Exit
Enter your choice
4
Enter the employee number to be deleted
1
The record is successfully deleted
Main Menu
1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
1
enter the data to be added/appended
Employee details
Number, Name, Salary
2 manju 13000
The record is added
Main Menu

1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
5
Enter the employee number to be searched
2
The details are
2
manju
13000
Main Menu
1. Add
2. Show
3. Modify
4. Delete
5. Search
6. Exit
Enter your choice
6

Terminating Program

DATE:03/12/2014
NO:35
AIM: pgm to enter 5 numbers in an integer array. display the largest number. if
the array contains null element, throw the exception.
#include <iostream>
using namespace std;
class ex
{
int max[5];
public:
void getn()
{
max[0]=1;
max[1]=0;
max[2]=3;
max[3]=4;
max[4]=5;
}
void largest()
{
int lar=max[10];
for (int i=0;i<5;i++)
{
if(max[i]==0)

throw i;
else
if(lar<max[i])
lar=max[i];
}
cout<<"largest"<<lar;// find the largest no.
}
void putn()
{
cout<<"the array is"<<"\n";
for(int i=0;i<5;i++)
cout<<"\t"<<max[i]<<"\n";
}};
int main()
{
ex e;
e.getn();
e.putn();
try
{
e.largest();
}
catch(int k)
{
cout<<"Caught a null value";
}
return 0;
}

OUTPUT

the array is
1
0
3
4
5
Caught a null value

DATE:03/12/2014
NO:36
AIM: Input 2 two digit positive numbers and display the prime
numbers in between them
possible errors
1.no. of digits is 2
2.n2>n1
3.check negative value for an operand
4. range <n1 and >n2
5.n1 and n2 must not be same
#include <iostream>
using namespace std;
class number
{
int n1,n2;
public:

class check_number{};
class check_negative{};
class check_large{};
class check_range{};
class check_same{};
number()
{}
number(int x,int y)
{
if (x<0 || y<0)
throw check_negative();
if (x<10 || x>99 || y<10 || y>99)
throw check_number();
if (x==y)
throw check_same();
if (y<x)
throw check_large();
else
n1=x; n2=y;
}
void prime()

{
cout<<"PRIME NUMBERS ARE"<<"\n";
for(int i=n1+1; i<n2; i++)
{
int prime = 1;
for(int j=2; j<i; j++)
if(i%j == 0)
{
prime = 0;
break;
}
if(prime)
{
if (i<n1 || i>n2)
throw check_range();
}
else
cout<<"\n"<< i;
}}};
int main() {
try

{
number n(10,20);
n.prime();
}
catch(number:: check_number)
{
cout<<"Exception caught number that is not 2 digit";
}
catch(number:: check_negative)
{
cout<<"Number is negative";
}
catch(number:: check_large)
{
cout<<"second number is less than first number";
}
catch(number::check_range)
{
cout<<"Prime numbers are out of range";
}
catch(number:: check_same)

{
cout<<"The numbers are same";
} // your code goes here
return 0;
}

OUTPUT
PRIME NUMBERS ARE
12
14
15
16
18

You might also like