Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that defines template of vector class that provides modify and multiplication facility

Posted By: Easy Tutor     Category: C++ Programming     Views: 6180

Write a program that defines template of class vector that provides facility to modify particular index value and multiply vector by 10.

Code for Program that defines template of vector class that provides modify and multiplication facility in C++ Programming

#include <iostream.h>
#include <conio.h>

template <class T>
class vector
{
    T *arr;
    int size;

    public:

    vector() {
       arr=NULL;
       size=0;
    }

    vector(int m);
    vector(T *a,int n);
    void modify(T value,int index);
    void multiply(int scalarvalue);
    void display();
};

template <class T>
vector<T> :: vector(int m)
{
   size=m;
   arr = new T[size];
   for(int i=0;i<size;i++)
       arr[i]=0;
}

template <class T>
vector<T> :: vector(T *a,int n)
{
   size=n;
   arr = new T[size];
   for(int i=0;i<size;i++)
       arr[i]=a[i];
}


template <class T>
void vector<T> :: modify(T value,int index)
{
    arr[index]=value;
}


template <class T>
void vector<T> :: multiply(int scalarvalue)
{
    for(int i=0;i<size;i++)
       arr[i] = arr[i] * scalarvalue;
}

template <class T>
void vector<T> :: display()
{
    cout<<"(";
    for(int i=0;i<size;i++)
    {
        cout<<arr[i];
        if(i!=size-1)
           cout<<", ";
    }
    cout<<")";
}


void main()
{
    clrscr();


    //Creating Integer Vector.int iarr[]={1,2,3,4,5};
    cout << "Integer Values \n";
    for(int i=0; i < 5; i++)
    {
        cout << "Enter integer value "  << i+1 << " : ";
        cin >> iarr[i];
    }
    vector <int> v1(iarr,5); //Integer array with 5 elements.
    cout<<"\nInteger Vector : ";
    v1.display();
    cout<<"\n\nModify index 3 with value 15\n";
    v1.modify(15,3); //modifying index 3 with value 15.
    cout<<"After Modification : ";
    v1.display();
    cout<<"\n\nMultiply with scalar value : 10\n";
    v1.multiply(10); //Multiply with scalar value 10.
    cout<<"After Multiplying : ";
    v1.display();
    cout<<"\n\n";


    //Creating double Vector.double darr[]={1.1,2.2,3.3,4.4,5.5};
    vector <double> v2(darr,5); //Double array with 5 elements.
    cout<<"\nDouble Vector : ";
    v2.display();
    cout<<"\n\nModify index 0 with value 9.9 \n";
    v2.modify(9.9,0); //modifying index 0 with value 9.9.
    cout<<"After Modification : ";
    v2.display();
    cout<<"\n\nMultiply with scalar value : 10\n";
    v2.multiply(10); //Multiply with scalar value 10.
    cout<<"After Multiplying : ";
    v2.display();
    cout<<"\n\n";

    getch();



}
  
Share: 



Easy Tutor
Easy Tutor author of Program that defines template of vector class that provides modify and multiplication facility is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!