Hi can please help with the union bagsUnion methode i am trying to group duplicate values together. i know i need to use the itemOccurences and contains method to duplicate the two bags in one , but i...


Hi can please help with the union



bagsUnion methode i am trying to group duplicate values together. i know i need to use the itemOccurences and contains method to duplicate the two bags in one , but i dont know how to do that in c++.



here is the program detail. I just need the union bag method to get fixed.



this is the deatail







  1. Hint:




  2. Consider using class method itemOccurrences in your solution for method bagsUnion. Union Definition


    The union of two Bag objects, bag1 and bag2, is another, new Bag object that contains of all items of the two source objects. The new Bag object may contain more than one copy of an item if, 1) both objects contain the item once, and 2) if either object contains multiple copies of an item. For example, if bag1 contains 5 occurrences of item 2 and bag2 contains 3 copies of 2, the new Bag object will contain 8 occurrences of 2.


    and here is my code


    #include



    #include



    #include





    #include
    "Bag.h"






    templatetypename
    Type>



    Bag::Bag() {


    arrayNumbers = 0;


    }




    templatetypename
    Type>



    Bag::Bag(const
    Type& item) {


    arrayNumbers = 0;


        items[arrayNumbers++] = item;


    }




    templatetypename
    Type>



    Bag::Bag(const
    Bag& bag) {


    arrayNumbers = bag.arrayNumbers;



    for(int
    value = 0;value < arraynumbers;="">


            items[value] = bag.items[value];



        }


    }




    templatetypename
    Type>



    Bag::~Bag() {


    arrayNumbers= 0;


    }




    templatetypename
    Type>



    const
    BagType>&
    Bag::operator=(const
    BagType>& bag) {




    if
    (this
    != &bag)


    {


        arrayNumbers  = bag.arrayNumbers;



    for
    (int
    i = 0; i < arraynumbers;="" ++i)="">


    items[i] = bag.items[i];


        }


    }



    return
    *this;



    }




    templatetypename
    Type>



    bool
    Bag::addItem(const
    Type& item) {



    if
    (isFull()) {



    return
    false;


        }


        items[arrayNumbers++] = item;



    return
    true;


    }




    templatetypename
    Type>



    bool
    Bag::removeItem(const
    Type& item) {



    int
    index = -1;



    int
    value = 0;



    while(value < arraynumbers)="">



    if
    (items[value] == item) {


            index = value;




    break;



            }


            ++value;


        }


    if
    (index  == -1)
    return
    false;



        --arrayNumbers;



    int
    number = index;



    while( number < arraynumbers)="">


            items[value] = items[number + 1];


            ++number;


        }





    return
    true;



    }




    templatetypename
    Type>



    int
    Bag::removeAllItems(const
    Type& item) {



    int
    cnt = 0;



    while
    (removeItem(item)) ++cnt;



    return
    cnt;


    }



    templatetypename
    Type>



    void
    Bag:: empty(){


    arrayNumbers = 0;


    }



    templatetypename
    Type>



    int
    Bag::nmbrItems() {



    return
    arrayNumbers;


    }




    templatetypename
    Type>



    int
    Bag::capacity() {



    return
    BAG_SIZE;


    }






    templatetypename
    Type>



    bool
    Bag::isEmpty() {



    return
    arrayNumbers == 0;


    }




    templatetypename
    Type>



    bool
    Bag::isFull() {



    return
    arrayNumbers ==
    BAG_SIZE;


    }




    templatetypename
    Type>



    bool
    Bag::contains(const
    Type& item) {



    int
    value = 0;



    while( value < arraynumbers)="">



    if
    (items[value] == item) {



    return
    true;


            }


            ++value;


        }



    return
    false;


    }




    templatetypename
    Type>



    int
    Bag::itemOccurrences(const
    Type& item) {




    int
    count = 0;



    int
    value = 0;



    while( value < arraynumbers)="">



    if
    (items[value] == item) {


                ++count;


            }


            ++value;


        }



    return
    count;


    }



    templatetypename
    Type>


    BagType>
    Bag::bagsUnion(const
    Bag& bag ) {


    BagType> unionBag;



    unionBag.arrayNumbers = bag.arrayNumbers;



    for
    (int
    i = 0; i < unionbag.arraynumbers;="" ++i)="">


    unionBag.items[i] = bag.items[i];


        unionBag.contains(bag.items[i]);


        }



    return
    unionBag;


    }





    templatetypename
    Type>


    string
    Bag::toString() {



    if
    (isEmpty())
    return
    "[]";


        string number = "[" + std::to_string(items[0]);



    int
    value = 1;



    while( value < arraynumbers)="">


      number += ", " + std::to_string(items[value]);



            ++value;


        }



       number += "]";




    return
    number;



    }








Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here