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);
}




The output of the above program is:
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, )


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: