My Array Class

Embed Size (px)

Citation preview

  • 8/14/2019 My Array Class

    1/11

    My Array Class============================================#include

    #include #include

    template

    class MyArray{

    public:

    /*======= Constructors =======*/

    MyArray() { }

    MyArray(int s){maxSize=s;

    used=0;a=new int [maxSize];

    for (int i=0;i

  • 8/14/2019 My Array Class

    2/11

    int getMaxSize(){

    return maxSize;}

    int getUsed(){

    return used;}

    /*======= isFull =======*/

    bool isFull()

    {

    if(used==maxSize)return true;

    elsereturn false;

    }

    /*======= isEmpty =======*/

    bool isEmpty(){

    if(used==0)return true;

    elsereturn false;

    }

    /*======= Add Element =======*/

    bool addElement(type value)

    {

    if(!isFull()){a[used]=value;

    used++;return true;

    }else

    return false;

  • 8/14/2019 My Array Class

    3/11

    }

    /*======= Get Element =======*/

    bool getElement(int index, type &value)

    {if(!isEmpty()){

    if(index>=0 && index

  • 8/14/2019 My Array Class

    4/11

    if(a[i]==value)

    {index=i;

    return true;}

    }}return false;

    }

    /*======= Last Index of Element =======*/

    bool lastIndex(type value, int &index)

    {if( isContain(value) )

    {

    for(int i=used; i>=0; i--){

    if(a[i]==value){

    index=i;return true;

    }}

    }return false;

    }

    /*======= Total Occurence of a Element =======*/

    type totalOccurence(type value)

    {int sum=0;

    for(int i=0; i

  • 8/14/2019 My Array Class

    5/11

    bool SetElement(int index, type value)

    {a[index]=value;

    return true;

    }

    /*======= Insert Element =======*/

    bool insertElementAt(int index,type &value){used++;

    for(int i=used;i>index;i--)a[i]=a[i-1];

    a[index]=value;return true;

    }

    /*======= Remove Element =======*/

    bool removeElementAt(int index){

    for(int i=index;i

  • 8/14/2019 My Array Class

    6/11

    if(a[c]>a[c+1])

    {hold=a[c+1];

    a[c+1]=a[c];a[c]=hold;

    }}

    }

    /*======= Sorting in Descending Order =======*/

    void dSort(){

    int hold;for(int p=0; p

  • 8/14/2019 My Array Class

    7/11

    MyArray ar(sizeOfArray);

    /*======= Menue =======*/int choice;

    do{

    cout

  • 8/14/2019 My Array Class

    8/11

    cout

  • 8/14/2019 My Array Class

    9/11

    ar.lastIndex(element,index);

    cout

  • 8/14/2019 My Array Class

    10/11

    case 9:{

    int index;cout>index;for(int i=0; i

  • 8/14/2019 My Array Class

    11/11

    {

    ar.dSort();

    int value;cout