Create a Vector class to represent a series of integer values. Allocate memory for Vector object using new operator. Write a C++ menu driven program with following member functions: i. To accept a vector ii. To display a vector in the form (10,20,30,…) iii. To multiply by a scalar value iv. To modify the value of a given position from vector

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<iostream.h>
#include<conio.h>
class vector
{
   int *a,n,i,s,p,val; //*a= pointer a, n=no , s=scalar value , p=position , val=value
   public:
   void acc()
   {
    cout<<"\nEnter how many no in vector: ";
      cin>>n;
      a=new int [n]; //creating memory dynamically
      for(i=0;i<n;i++)
      {
        cout<<"\nEnter vector elements: ";
         cin>>a[i];
      }
   }
   void dis()
   {
    cout<<"(";
       for(i=0;i<n;i++)
      {
        cout<<a[i]<<",";

      }
      cout<<")";
   }
   void mul()
   {
      cout<<"\nEnter scalar value: ";
      cin>>s;
       for(i=0;i<n;i++)
      {
a[i]=a[i]*s;
      }
   }
   void pos()
   {
    cout<<"\nEnter postion: ";
      cin>>p;
      cout<<"\nEnter a value: ";
      cin>>val;
    for(i=0;i<n;i++)
      {
      if(i==p-1)
         {
          a[i]=val;
         }
      }
   }
};
void main()
{
int ch;
vector v; // Instance of class vector
   do
   {
    cout<<"\n1.Accept a vector\n2.Display a vector\n3.Multiply by vector\n4.Replace a vector element\n5.Exit";
      cout<<"\nEnter your choice: ";
      cin>>ch;
      switch(ch)
      {
      case 1:v.acc();break;
         case 2:v.dis();break;
         case 3:v.mul();break;
         case 4:v.pos();break;
      }
   }while(ch!=5);
}

0 Comments