Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LISTED CODE Listing 3-2 A program that tests the core methods of the class Array

ID: 3911068 • Letter: L

Question

LISTED CODE

Listing 3-2 A program that tests the core methods of the class ArrayBag

  /** Header file for an array-based implementation of the ADT bag.  @file ArrayBag.h */  # ifndef _ARRAY_BAG  # define _ARRAY_BAG  # include "BagInterface.h"  template < class ItemType >  class ArrayBag:  public BagInterface < ItemType >  {      private:      static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag      ItemType items [DEFAULT_CAPACITY]; // Array of bag items      int itemCount; // Current count of bag items      int maxItems; // Max capacity of the bag      // Returns either the index of the element in the array items that      // contains the given target or -1, if the array does not contain      // the target.      int getIndexOf (const ItemType & target) const;      public:      ArrayBag ();      int getCurrentSize () const;      bool isEmpty () const;      bool add (const ItemType & newEntry);      bool remove (const ItemType & anEntry);      void clear ();      bool contains (const ItemType & anEntry) const;      int getFrequencyOf (const ItemType & anEntry) const;      vector < ItemType > toVector () const;  }  ;  // end ArrayBag  # include "ArrayBag.cpp"  # endif
3. Modify the Array Bag class given in Chapter 3 to meet the following specs: Write a member function replace that replaces a given item in a given bag with another given item. The function should return a boolean value to indicate whether the replacement was successful. Page 2 of 3 Write a recursive array-based implementation of the method to Vector for the class ArrayBag Write a client function that merges two bags into a new third bag. Do not destroy the original two bags Good Luck!

Explanation / Answer

// replace element a to b

void replace(ItemType a , ItemType b)

{

    int i;

   

    // traverse through arraybag

    for( i = 0 ; i < this->itemCount ; i++ )

        // if the current element is the element to be replaced

        if( this->items[i] == a )

            this->items[i] = b;

}

// recursive toVector() function

vector<ItemType> toVector()

{

    return toVectorutil( 0 );

}

// index is the index of the current element to be visited

vector<ItemType> toVectorUtil(int index)

{

    // if all the elements have been iterated

    if(index > this->itemCount)

    {

        vector<Itemtype> ob;

       

        // return an empty vector

        return ob;

    }

       

    // get a vector of elements after the current element

    vector<Itemtype> x = toVectorUtil( index + 1 );

   

    // insert new element at the first position of x

    x.insert(x.begin() , this->items[index]);

   

    return x;

}

// merge current bag and ob and return the resultant arraybag

ArrayBag<ItemType> merge(ArrayBag<ItemType> ob)

{

    // store the merge of current bag and ob

    ArrayBag<ItemType> ans;

   

    int i;

   

    // get ob as a vector

    vector<ItemType> v1 = ob.toVector();

   

    // add elements of current bag to ans

    // traverse through current bag

    for( i = 0 ; i < this->itemCount ; i++ )

        // add current element to ans

        ans.add( this->items[i] );

       

    // add elements of bag ob to ans

    // traverse through v1

    for( i = 0 ; i < v1.size() ; i++ )

        // add current element to ans

        ans.add( v1[i] );

   

    return ans;

}