12.1
Write a function template for finding the minimum value contained in an array.
Answer
#include<iostream> using namespace std; const int size=5; template <class T> class vector { T v[size]; public: vector(){} vector(T *b); void show(); minimum(vector<T> &m); }; template <class T> vector<T>::vector(T *b) { for(int i=0;i<size;i++) v[i]=b[i]; } template<class T> T vector<T>::minimum(vector<T> &m) { int j=0; for(int k=1;k<size;k++) { if(m.v[j]>m.v[k]) j=k; } return m.v[j]; } template<class T> void vector<T>::show() { cout<<"("<<v[0]; for(int i=1;i<size;i++) cout<<","<<v[i]; cout<<")"; cout<<"\n"; } int main() { int x[size]={5,7,3,1,8};//size is 5; float y[size]={1.2,1.5,2.3,1.0,0.501}; vector<int> v1; vector<float> v2; v1=x; v2=y; cout<<" minimum value = "<<v1.minimum(v1)<<" of array"; v1.show(); cout<<" minimum value = "<<v2.minimum(v2)<<" of array"; v2.show(); return 0; }
OUTPUT
minimum value = 1 of array(5,7,3,1,8)
minimum value = 0.501 of array(1.2,1.5,2.3,1,0.501)
12.2
Write a class template to represent a generic vector. Include member functions to perform the following tasks:
- (a) To create the vector
- (b) To modify the value of a given element
- (c) To multiply by a scalar value
- (d) To display the vector in the following form (10, 20, 30 …)
Answer
#include<iostream> #include<iomanip> using namespace std; template <class santo> class vector { float *p; int size; public: void creat_vector(santo a); void set_element(int i,santo value); void modify(void); void multiply(santo b); void display(void); }; template <class santo> void vector<santo>::creat_vector(santo a) { size=a; p=new float[size]; } template <class santo> void vector<santo>::set_element(int i,santo value) { p[i]=value; } template <class santo> void vector<santo> :: multiply(santo b) { for(int i=0;i<size;i++) p[i]=b*p[i]; } template <class santo> void vector<santo>:: display(void) { cout<<"p["<<size<<"] = ( "; for(int i=0;i<size;i++) { if(i==size-1) cout<<p[i]; else cout<<p[i]<<" , "; } cout<<")"<<endl; } template <class santo> void vector<santo>::modify(void) { int i; cout<<" to edit a given element enter position of the element : "; cin>>i; i--; cout<<" Now enter new value of "<<i+1<<"th element : "; santo v; cin>>v; p[i]=v; cout<<" Now new contents : "<<endl; display(); cout<<" to delete an element enter position of the element :"; cin>>i; i--; for(int j=i;j<size;j++) { p[j]=p[j+1]; } size--; cout<<" New contents : "<<endl; display(); } int main() { vector<float> hasib; int s; cout<<" enter size of vector : "; cin>>s; hasib.creat_vector(s); cout<<" enter "<<s<<" elements one by one :"<<endl; for(int i=0;i<s;i++) { float v; cin>>v; hasib.set_element(i,v); } cout<<" Now contents :"<<endl; hasib.display(); cout<<" to multiply this vector by a scalar quantity enter this scalar quantity : "; float m; cin>>m; hasib.multiply(m); cout<<" Now contents : "<<endl; hasib.display(); hasib.modify(); return 0; }
OUTPUT
enter size of vector : 4
enter 4 elements one by one :
4 3 2 5
Now contents :
p[4] = ( 4 , 3 , 2 , 5)
to multiply this vector by a scalar quantity enter this scalar quantity : 3
Now contents :
p[4] = ( 12 , 9 , 6 , 15)
to edit a given element enter position of the element : 2
Now enter new value of 2th element : 222
Now new contents :
p[4] = ( 12 , 222 , 6 , 15)
to delete an element enter position of the element :3
New contents :
p[3] = ( 12 , 222 , 15)
Next Previous