class template to represent a generic vector using c++

/*
Create a class template to represent a generic vector. Include following member functions:
1. To create the vector.
2. To modify the value of a given element
3. To multiply by a scalar value
4. To display the vector in the form (10,20,30,...)
*/

#include<iostream>
using namespace std;
template<class T>
class vector
{
    T v[20];
    int size;
   
    public:
        void create();
        void modify();
        void mult();
        void display();
};

template<class T>
void vector<T>::create()
{
    int i;
    T value;
    char ans;
    size=0;
    do{
        cout<<"\nEnter the index & value:";
        cin>>i>>value;
        v[i]=value;
        size++;
        cout<<"\nDo you want more elements?";
        cin>>ans;
    }while(ans=='y'||ans=='Y');
}

template<class T>
void vector<T>::modify()
{
    int key;
    T newval;
    cout<<"\nEnter index for modificaion:";
    cin>>key;
    cout<<"\nEnter new value:";
    cin>>newval;
    v[key]=newval;
}

template<class T>
void vector<T>::mult()
{
    int i;
    int scalarval;
    cout<<"\nEnter scalar value for multiplication";
    cin>>scalarval;
    for(i=0;i<size;i++)
    v[i]=v[i]*scalarval;
}

template<class T>
void vector<T>::display()
{
    int i;
   
    cout<<"\nSize of vector is:"<<size;
    cout<<"\nElements in vector are:";
    cout<<"(";
    for(i=0;i<size;i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<")";
}

int main()
{
    int ch;
    vector<int>obj;
    cout<<"\nProgram for template class";
    do
    {
        cout<<"\nMAIN MENU";
        cout<<"\n1.Create";
        cout<<"\n2.Display";
        cout<<"\n3.Mult";
        cout<<"\n4.Modify";
        cout<<"\n0.Exit";
        cout<<"\nEnter your choice:";
        cin>>ch;
       
        switch(ch)
        {
            case 1:
                obj.create();
                break;
               
            case 2:
                obj.display();
                break;
               
            case 3:
                obj.mult();
                break;
               
            case 4:
                obj.modify();
                break;
               
            case 0:
                cout<<"\nExit\n";
                break;
               
            default:
                cout<<"\nInvalid choice";
                break;
        }
    }while(ch!=0);
    return 0;
}

output-
Program for template class
MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:1

Enter the index & value:0 10

Do you want more elements?y

Enter the index & value:1 20

Do you want more elements?y

Enter the index & value:2 30

Do you want more elements?y

Enter the index & value:3 40

Do you want more elements?n

MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:2

Size of vector is:4
Elements in vector are:(10 20 30 40 )
MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:3

Enter scalar value for multiplication 2

MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:2

Size of vector is:4
Elements in vector are:(20 40 60 80 )
MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:4

Enter index for modificaion:3

Enter new value:99

MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:2

Size of vector is:4
Elements in vector are:(20 40 60 99 )
MAIN MENU
1.Create
2.Display
3.Mult
4.Modify
0.Exit
Enter your choice:0

Exit

Comments

  1. Overall Python is better than C++ in terms of its simplicity and easy syntax. But C++ is better in terms of performance, speed, vast application areas, etc.

    ReplyDelete

Post a Comment

Popular posts from this blog

Write a function template selection Sort. Write a program that inputs, sorts and outputs an integer array and a float array.

C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.

Write C++ program using STL for Sorting and searching