Tuesday, 22 March 2016

Assignment No:05

Assignment No:05

Q1.What is Files & Streams?
Q2. Explain file Handling Concepts.
Q3. What is STL?  Explain Components of STL.
Q4. Explain Algorithm & STL Vector Class

Q5.  Write note on Basic Searching Sorting Algorithm
Q6. Explain Min-Max Algorithm

**********************************************************************************************



Friday, 18 March 2016

Implement C++/Java/Python program to write a class template to represent a generic vector (PR:09)

CPP program: 
Theory:
Vector
  • Vectors are sequence containers representing arrays that can change in size.
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
  • The STL vector class is a template class of sequence containers that arrange elements of a given type in a linear arrangement and allow fast random access to any element. They should be the preferred container for a sequence when random-access performance is at a premium.
   ******************************************************************************************************  
#include<iostream>
using namespace std;
template<class T>
class vector
{
    T v[10];
    int size;
    public:
        void create();
        void modify();
        void multiplication();
        void display();
};
template<class T>
void vector<T>::create()
{
    int i;
    T value;
    char ans;
    size=0;
    do
    {
        cout<<"\nEnter Index and Value:\n";
        cin>>i>>value;
        v[i]=value;
        size++;
        cout<<"\nDo you want enter more elements?";
        cin>>ans;
    }while(ans=='y');
}
template<class T>
void vector<T>::modify()
{
    int key;
    T newvalue;
    cout<<"\nFor modification enter the index of vector:";
    cin>>key;
    cout<<"\nEnter modifyed value";
    cin>>newvalue;
    v[key]=newvalue;
}
template<class T>
void vector<T>::multiplication()
{
    int i,scalarval;
    cout<<"\nEnter scalar";
    cin>>scalarval;
    for(i=0;i<size;i++)
    v[i]=v[i]*scalarval;
}
template<class T>
void vector<T>::display()
{
    int i;
    cout<<"size of vector="<<size<<endl;
    cout<<"Elements in the vector are:\n";
    cout<<"(";
    for(i=0;i<size;i++)
    cout<<v[i]<<"";
    cout<<")";
}
int main()
{
    int ch;
    char ans;
    vector<int>obj;
    cout<<"\n\t------------------------------------------";
    cout<<"\n\tProgram for representing generic vector using class template";
    cout<<"\n\t------------------------------------------";
    do
    {
        cout<<"\nMain Menu";
        cout<<"\n1.Create\n2.Display\n3.Modify\n4.Multiplication";
        cout<<"\nEnter your choice";
        cin>>ch;
        switch(ch)
        {
            case 1:
                obj.create();
                break;
            case 2:
                obj.display();
                break;
            case 3:
                obj.modify();
                break;
            case 4:
                obj.multiplication();
                break;
        }
        cout<<"\nDo you want to continue?(y/n):";
        cin>>ans;
    }while(ans=='y');
    return 0;
}