C++ Need to find the difference items in 2 arrays and store in a 3rd array. Also
ID: 3890158 • Letter: C
Question
C++ Need to find the difference items in 2 arrays and store in a 3rd array. Also need to account for the 2 arrays holding multiples of the same value (i.e. if array1 has value x 5 times and array2 has value x 2 times, the difference would be value x 3 times).
Here is what I have:
template<class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::bagDifference(const ArrayBag<ItemType> &otherBag) const//bag difference that display the different contents between the two bags
{
ArrayBag<ItemType> differenceBag;
for(int i = 0; i < getCurrentSize(); i++)
{
if(!otherBag.contains(items[i]))
{
differenceBag.add(items[i]);
}
}
return differenceBag;
}//end of bagDifference
The problem with this is if i use ararys such as:
array1 = 1 1 1 2 2
array2 = 1 2 2 2 1
array3 is saying it contains 0 items when it should contain 1 2.
Any help would be appreciated!
Explanation / Answer
The problem with your code is that every time you are checking whether the value is present or not and when it is present you are not adding it to the differenceBag.
So with a sample data as
array1 = 1 1 1 2 2
array2 = 1 2 2 2 1
the differenceBag will never be having any items since every time when 1 and 2 are looked up in the otherBag, they are always there.
You need to do a little bit of tweaking here.
My approach calls for adding all the elements of one array into the differenceBag and then removing all the items of the other array from the differenceBag
what is left behind is the difference.
template<class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::bagDifference(const ArrayBag<ItemType> &otherBag) const//bag difference that display the different contents between the two bags
{
//this approach requires having a copy constructor
//otherwise you can go for assignment operator overloading
ArrayBag<ItemType> differenceBag(otherBag);
//Now differenceBag will have all the elements of otherBag
// we will start removing element after element by checking them from this bag
//This also means that you should have a remove function defined which would remove an item only once
for(int i = 0; i < getCurrentSize(); i++)
{
if(differenceBag.contains(items[i]))
{
differenceBag.remove(items[i]);
}
}
return differenceBag;
}//end of bagDifference
// If you dont already have a remove function, this would be helpful
template<class ItemType>
void ArrayBag<ItemType>::remove(ItemType item) const
{
for(int i = 0; i < getCurrentSize(); i++)
{
if( items[i] == item)
{
//once the item si found, replace it with the next item, thereby moving each item one position to the left and reduce the size by 1
for(int j=i; j<getCurrentSize()-1; j++)
{
items[j] = ietms[j+1];
}
//decrement the size variable
size--;
// return the function back to its caller, because we only want to remove the item once
return;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.