27.8.15

Class template to represent a generic vector

Write a class template to represent a generic vector. Include member functions to perform the following tasks:
1)  To create the vector.

2)  To modify the value of a given element.

3)  To multiply the vector by a scalar value.

4)  To display the vector in the form (10, 20, 30,…..)

#include<iostream.h>

#include<string.h>
#include<conio.h> template<class T> class vector
{

T *v; int size; public:

vector(int m); void create(T *a); void modify(int); void multiply(int); void display();
};

template<class T>

vector< T > :: vector(int m)
{

v=new T[size=m]; for(int i=0;i<size;i++) v[i]=0;

}

template<class T>

void vector<T>::create(T*a)
{
for(int i=0;i<size;i++)
{
cin>>a[i];
v[i]=a[i];
}
}

template<class T>

void vector<T>::modify(int k)
{
v[k]=v[k]+10;
}

template<class T>
void vector<T>::multiply(int k)

{

for(int i=0;i<size;i++) v[i]=v[i]*k;
}

template<class T>

void vector<T>::display()
{
cout.setf(ios::showpoint);
cout<<"\n(";
for(int i=0;i<size;i++) cout<<v[i]<<", "; cout<<")\n";
}

int main()

{

vector <float> v1(5); vector <int> v2(5); float *x;

int *y; int i; int s;

cout<<"Enter the float vector element :\n"; v1.create(x);
cout<<"Enter the Interger vector element :\n"; v2.create(y);
cout<<"enter the element u want to modify in float vector :"; cin>>i;
v1.modify(i);

v1.display();

cout<<"\nenter the element u want to modify in int vector :"; cin>>i;
v2.modify(i);

v2.display();

cout<<"\nenter the number to calculate the scalar product :"; cin>>s;
v1.multiply(s);

v2.multiply(s);

cout<<"\nthe Float vector after scalar product is as follows :"; v1.display();
cout<<"\nthe integer vector after scalar product is as follows :"; v2.display();
getch();

return(0);
}

***********************************OUTPUT*******************************

Enter the float vector element : 1.1 2.2 3.3 4.4 5.5




Enter the Integer vector element : 10 10 30 40 50





enter the element u want to modify in float vector :3

(1.100000, 2.200000, 3.300000, 14.400000, 5.500000, ) enter the element u want to modify in int vector :2

(10, 10, 40, 40, 50, )

enter the number to calculate the scalar product :2

the Float vector after scalar product is as follows : (2.200000, 4.400000, 6.600000, 28.799999, 11.000000, )

the integer vector after scalar product is as follows : (20, 20, 80, 80, 100, )

Define a function template for finding the minimum value contained in an array

Define a function template for finding the minimum value contained in an array. Write main( ) function to find the minimum value of integer array and minimum value of floating point numbers in an array.

#include<iostream.h>

#include<conio.h> template<class T>

T minimum(T a[],int size)

{

T min=a[0];

for(int i=0;i<size;i++)

{

if(a[i]<min)

min=a[i];

}

return min;

}

int main()

{

int a[10],size,i,min1; float b[10],min2; clrscr();

cout<<"enter the size value:\n"; cin>>size;
cout<<"enter the integer aray elements\n"; for(i=0;i<size;i++)

cin>>a[i];

cout<<"enter the floating array elements\n"; for(i=0;i<size;i++)

cin>>b[i];

min1=minimum(a,size);

min2=minimum(b,size);


cout<<"The minimum integer elements is:\n"; cout<<min1;
cout<<"\nThe minimum floating elements is :\n";

cout<<min2;

getch();

return 0;

}

************************************OUTPUT******************************

enter the size value: 5

enter the integer aray elements 20 30 10 38 28



 enter the floating array elements 20.4 19.7 14.8 1.7 2.6


The minimum integer elements is: 10
The minimum floating elements is : 1.7

26.8.15

Write a C++ program that displays the size

Write a C++ program that displays the size (in bytes) of a given file. The name of

the file is specified as command line argument.

#include<iostream.h>

#include<fstream.h>

#include<conio.h>

void main(int arge,char*argv[])

{

int bcount=0; char cl; ifstream testfile; clrscr();

cout<<"enter the file name\n"; cin>>argv[1]; testfile.open(argv[1],ios::in); while(testfile)
{

testfile.read((char*)&cl,sizeof(cl));

++bcount;

}

testfile.close();

cout<<"size of the given file:"<<argv[1]<<"is"<<bcount<<"bytes"<<endl; getch();
}


*********************************OUTPUT********************************


enter the file name

ph.cpp


size of the given file:is2308bytes

Write an interactive, menu-driven program

Write an interactive, menu-driven program that will access the file created in program No.17 and implement the following tasks:
i)   To determine the telephone numbers of the specified person.

ii)   To determine the name if a telephone number is given.

iii)   To update the telephone number whenever there is a change.

#include<iostream.h>

#include<fstream.h>

#include<iomanip.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h> class phone

{

public: long int no; char name[10]; void getdata()

{

cout<<endl<<"enter the name :"; cin>>name;
cout<<endl<<"enter the telephone number :"; cin>>no;

}

void putdata()

{

cout<<endl<<setw(10)<<name<<endl<<setw(10)<<no<<endl;

}

};


int main()

{


phone ph; fstream f; clrscr();
int n; f.open("pp1.txt",ios::trunc|ios::in|ios::out|ios::ate); cout<<"Enter the number of records :";

cin>>n;

for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<endl<<"conents of the file are as follows "; while(f.read((char *)&ph,sizeof(ph))) ph.putdata();

int choice; while(1)

{

cout<<"\n1.get name from telephone number"; cout<<"\n2.get telephone number from name"; cout<<"\n3.alter the telephone number"; cout<<"\n4.EXIT";
cout<<"\nenter ur choice "; cin>>choice;
f.seekg(0);

f.clear();

switch(choice)

{

case 1:cout<<"\nEnter the telephone number to get the name "; int no;

cin>>no;

f.seekg(0);

f.clear();

while(f.read((char *)&ph,sizeof(ph)))

{
if(ph.no==no)

{

ph.putdata();

}

}

break;

case 2:cout<<"\nEnter the name to get the telephone number"; char search_name[10];

cin>>search_name; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,search_name)==0) ph.putdata();

}

break;

case 3:cout<<"\nEnter the name to modify the telephone number "; char searchname[10];

cin>>searchname; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,searchname)==0)

{

ph.putdata();

cout<<endl<<"Enter the new details to modify :"; ph.getdata();

int loc=sizeof(ph); int p=f.tellp(); cout<<endl<<p; f.seekp(p-loc);

f.write((char *)&ph,sizeof(ph));

}

}

cout<<endl<<"After modification the content of the file is as follows ";

f.clear();

f.seekg(0);

while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

break;

case 4:exit(0);

}

}

}

******************************OUTPUT**********************************

Enter the number of records :3 enter the name :aaa

enter the telephone number :11111 enter the name :bbb

enter the telephone number :22222 enter the name :ccc

enter the telephone number :33333 conents of the file are as follows

aaa 11111 bbb 22222 ccc 33333
1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 1

Enter the telephone number to get the name 11111 found
aaa 11111
not foundnot foundnot found 1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT

enter ur choice 2

Enter the name to get the telephone numberccc ccc

33333

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 3

Enter the name to modify the telephone number ccc ccc

33333

Enter the new details to modify : enter the name :ccc
enter the telephone number :44444 42

After modification the content of the file is as follows aaa
11111 bbb 22222 ccc 44444

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 4

C++ program to read the file and output the list in the tabular format

A file contains a list of names and telephone numbers in the following form:

Name             Tel. No.

Write a C++ program to read the file and output the list in the tabular format. The name should be left-justified and numbers right-justified. Use a class object to store each set of data.

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

#include<fstream.h> class phone

{

public:

char name[20]; long int phone_no; void getdata()

{

cout<<"Enter the name and phone_no"<<"\n"; cin>>name>>phone_no;

}

void putdata()

{

cout<<endl<<setw(20)<<setiosflags(ios::left)<<name<<setw(10) <<setiosflags(ios::right)<<phone_no<<endl;

}

};

void main()

{

fstream f; phone ph; int n;


f.open("phonefile",ios::out|ios::in|ios::app);
clrscr();

cout<<"\nEnter the total no_of records:"; cin>>n;
for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<"\n\nThe content of the file is:\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME"

<<setw(10)<<setiosflags(ios::right)<<"TELEPHONE_NO"; while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

f.close();

getch();

}


**********************************OUTPUT********************************

Enter the total no_of records:3 Enter the name and phone_no RAGHU
2456570

Enter the name and phone_no RAJEEV
2457859

Enter the name and phone_no RANJU
2451230

The content of the file is:

NAME                TELEPHONE_NO

RAGHU                  2456570

RAJEEV                2457859

RANJU                   2451230