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

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

ID: 3911214 • Letter: L

Question

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

Modify the ArrayBag 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 3 of 3 ? Write a recursive array-based implementation of the method toVector for the class ArrayBag. ? Write a client function that merges two bags into a new third bag. Do not destroy the original two bags

  Modify the ArrayBag 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 3 of 3 ? Write a recursive array-based implementation of the method toVector for the class ArrayBag. ? Write a client function that merges two bags into a new third bag. Do not destroy the original two bags

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote