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, )

No comments:

Post a Comment

Comment Here