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

1,(30 points) Similar to Exercise at end of See 2.2 . 56 and 580%-137) Suppose U

ID: 3753966 • Letter: 1

Question

1,(30 points) Similar to Exercise at end of See 2.2 . 56 and 580%-137) Suppose U be the universal set. Then determine the bit strings for the following: a. An empty set b. Universal set. e. Symmetric difference of two sets which are subset of universal set,U(The symmetrie difference of A and B, is the set containing those element in either A or B, but not in both A and B) d. Union of n sets that all are subsets of the universal set, U e.Intersection of n sets that all are subsets of the universal set, U Define any variables (such as name of the sets, size of the universal set or the bitstring length, etc), state any assumptions and provide brief justification or explanation of the derivation of each of the above bit strings. (Provide general case answer and not example specific answer for all including justification)

Explanation / Answer

// File Name: UnionIntersectionDifference.cpp
#include <iostream>
#define MAX 50 // Maximum size of array
using namespace std;

// Defines a template class TemplateArray
template <class ItemType>
class TemplateArray
{
private:
// Template type array of size MAX to store data
ItemType itemArray[MAX];
// To store the length of the array
int length;
public:
// Prototype of member function
TemplateArray();
void addItem(ItemType);
ItemType getItem(int);
int getLength();
void showItem();
TemplateArray<ItemType> bagUnion(const TemplateArray<ItemType> &otherBag) const;
TemplateArray<ItemType> bagIntersection(const TemplateArray<ItemType> &otherBag) const;
TemplateArray<ItemType> bagDifference(const TemplateArray<ItemType> &otherBag) const;
};// End of class

// Default constructor definition
template <class ItemType>
TemplateArray<ItemType>::TemplateArray()
{
length = 0;
}// End of default constructor


// Template type of function to return the length of the string
template <class ItemType>
int TemplateArray<ItemType>::getLength()
{
return length;
}// End of function

// Template type function to add an item to the array
template <class ItemType>
void TemplateArray<ItemType>::addItem(ItemType item)
{
itemArray[length++] = item;
}// End of function

// Template type function to return an item at index position given as parameter from the array
template <class ItemType>
ItemType TemplateArray<ItemType>::getItem(int index)
{
return itemArray[index];
}// End of function

// Template type function to show the array
template <class ItemType>
void TemplateArray<ItemType>::showItem()
{
// Loops till length of the array
for(int x = 0; x < length; x++)
// Displays each index position of the array
cout<<itemArray[x]<<", ";
cout<<endl;
}// End of function

// Template type function to perform union operation
// Parameter: const TemplateArray<ItemType> &otherArray is a TemplateArray class object of type template (otherArray)
// Returns: TemplateArray<ItemType> is a TemplateArray class object of type template (temp)
template <class ItemType>
TemplateArray<ItemType> TemplateArray<ItemType>::bagUnion(const TemplateArray<ItemType> &otherArray) const
{
// Creates a temporary object of template type of class TemplateArray using default constructor
TemplateArray<ItemType> temp;
// Declares a counter for temp object
int counter;

// Loops till the length of the implicit available object array length
for(counter = 0; counter < length; counter++)
// Assigns x index position of array belongs to implicit available object
// to counter index position of temp object's array
temp.itemArray[counter] = itemArray[counter];

// Loops till the length of the parameter object otherArray length
for(int x = 0; x < otherArray.length; x++, counter++)
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
temp.itemArray[counter] = otherArray.itemArray[x];

// Adds the length of implicit object array length with parameter object array length
// stores it in temporary object length
temp.length = length + otherArray.length;
// Returns the object
return temp;
}// End of function

// Template type function to perform intersection operation
// Parameter: const TemplateArray<ItemType> &otherArray is a TemplateArray class object of type template (otherArray)
// Returns: TemplateArray<ItemType> is a TemplateArray class object of type template (temp)
template <class ItemType>
TemplateArray<ItemType> TemplateArray<ItemType>::bagIntersection(const TemplateArray<ItemType> &otherArray) const
{
// Creates a temporary object of template type of class TemplateArray using default constructor
TemplateArray<ItemType> temp;
// Declares a counter for temp object
int counter = 0;

// Loops till the length of the implicit available object array length
for(int x = 0; x < length; x++)
{
// Loops till the length of the parameter object array length
for(int y = 0; y < otherArray.length; y++)
{
// Checks if x index position of the array belongs to implicit available object
// is equals to y index position of the array belongs to parameter object
if(itemArray[x] == otherArray.itemArray[y])
{
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
// Increase the counter by one
temp.itemArray[counter++] = itemArray[x];
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Stores the counter value as the length of the temporary object
temp.length = counter;
// Returns the object
return temp;
}// End of function

// Template type function to perform difference operation
// Parameter: const TemplateArray<ItemType> &otherArray is a TemplateArray class object of type template (otherArray)
// Returns: TemplateArray<ItemType> is a TemplateArray class object of type template (temp)
template <class ItemType>
TemplateArray<ItemType> TemplateArray<ItemType>::bagDifference(const TemplateArray<ItemType> &otherArray) const
{
// Creates a temporary object of template type of class TemplateArray using default constructor
TemplateArray<ItemType> temp;
// Declares a counter for temp object
int counter = 0;
// To store found status
int found;

// Loops till the length of the implicit available object array length
for(int x = 0; x < length; x++)
{
// Sets the found status to -1 for not found for each element of implicit object array
found = -1;
// Loops till the length of the parameter object array length
for(int y = 0; y < otherArray.length; y++)
{
// Checks if x index position of the array belongs to implicit available object
// is equals to y index position of the array belongs to parameter object
if(itemArray[x] == otherArray.itemArray[y])
{
// Sets the found status to loop variable value y
found = y;
// Come out of the loop
break;
}// End of if condition
}// End of inner for loop

// Checks if found status is -1 then element not found
if(found == -1)
// Assigns x index position of array belongs to parameter object
// to counter index position of temp object's array
// Increase the counter by one
temp.itemArray[counter++] = itemArray[x];
}// End of outer for loop
// Stores the counter value as the length of the temporary object
temp.length = counter;
// Returns the object
return temp;
}// End of function

// main function definition
int main()
{
// Creates a string arrays with values
string arrayStr1[] = {"One", "Two", "Three", "Four", "Five"};
string arrayStr2[] = {"Twenty", "Thirty", "Fourth", "Fifty", "One"};

// Creates a integer arrays with values
int arrInt1[] = {1, 2, 3, 4, 5};
int arrInt2[] = {20, 30, 40, 50, 1};

// Declares two objects of class TemplateArray of type integer using default constructor
TemplateArray<int> firstInt;
TemplateArray<int> secondInt;

// Declares two objects of class TemplateArray of type string using default constructor
TemplateArray<string> firstStr;
TemplateArray<string> secondStr;

// Loops 5 times
for(int x = 0; x < 5; x++)
{
// Calls the method to add an element to the array belongs to object
// With the x index position of the local array
firstInt.addItem(arrInt1[x]);
secondInt.addItem(arrInt2[x]);
firstStr.addItem(arrayStr1[x]);
secondStr.addItem(arrayStr2[x]);
}// End of for loop

// Union operation for integer
// Calls the function to display the length of first bag for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
// Calls the function to display first information for integer
firstInt.showItem();
// Calls the function to displays the length of second bag for integer
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
// Calls the function to display second information for integer
secondInt.showItem();

// Calls the function to perform union operation for integer
TemplateArray<int> bagUnionInt = firstInt.bagUnion(secondInt);
// Calls the function to displays the length of union bag for integer
cout<<" The bag contains the union of these bags: The bag contains "<<bagUnionInt.getLength()<<" items: ";
// Calls the function to display union bag information for integer
bagUnionInt.showItem();

// Union operation for string
// Calls the function to displays the length of first bag for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
// Calls the function to display first information for string
firstStr.showItem();

// Calls the function to displays the length of second bag for string
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
// Calls the function to display second information for string
secondStr.showItem();

// Calls the function to perform union operation for string
TemplateArray<string> bagUnionStr = firstStr.bagUnion(secondStr);
// Calls the function to displays the length of union bag for string
cout<<" The bag contains the union of these bags: The bag contains "<<bagUnionStr.getLength()<<" items: ";
// Calls the function to display union bag information for string
bagUnionStr.showItem();

// Intersection operation for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
firstInt.showItem();
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
secondInt.showItem();

// Calls the function to perform intersection operation for integer
TemplateArray<int> bagIntersectionInt = firstInt.bagIntersection(secondInt);
cout<<" The bag contains the union of these bags: The bag contains "<<bagIntersectionInt.getLength()<<" items: ";
bagIntersectionInt.showItem();

// Intersection operation for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
firstStr.showItem();
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
secondStr.showItem();

// Calls the function to perform intersection operation for string
TemplateArray<string> bagIntersectionStr = firstStr.bagIntersection(secondStr);
cout<<" The bag contains the intersection of these bags: The bag contains "<<bagIntersectionStr.getLength()<<" items: ";
bagIntersectionStr.showItem();

// Difference operation for integer
cout<<" First bag: The bag contains "<<firstInt.getLength()<<" items: ";
firstInt.showItem();
cout<<" Second bag: The bag contains "<<secondInt.getLength()<<" items: ";
secondInt.showItem();

// Calls the function to perform difference operation for integer
TemplateArray<int> bagDifferenceInt = firstInt.bagDifference(secondInt);
cout<<" The bag contains the union of these bags: The bag contains "<<bagDifferenceInt.getLength()<<" items: ";
bagDifferenceInt.showItem();

// Difference operation for string
cout<<" First bag: The bag contains "<<firstStr.getLength()<<" items: ";
firstStr.showItem();
cout<<" Second bag: The bag contains "<<secondStr.getLength()<<" items: ";
secondStr.showItem();

// Calls the function to perform difference operation for string
TemplateArray<string> bagDifferenceStr = firstStr.bagDifference(secondStr);
cout<<" The bag contains the intersection of these bags: The bag contains "<<bagDifferenceStr.getLength()<<" items: ";
bagDifferenceStr.showItem();
}// End of main function

Sample Output:

First bag: The bag contains 5 items:
1, 2, 3, 4, 5,

Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,

The bag contains the union of these bags: The bag contains 10 items:
1, 2, 3, 4, 5, 20, 30, 40, 50, 1,

First bag: The bag contains 5 items:
One, Two, Three, Four, Five,

Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,

The bag contains the union of these bags: The bag contains 10 items:
One, Two, Three, Four, Five, Twenty, Thirty, Fourth, Fifty, One,

First bag: The bag contains 5 items:
1, 2, 3, 4, 5,

Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,

The bag contains the union of these bags: The bag contains 1 items:
1,

First bag: The bag contains 5 items:
One, Two, Three, Four, Five,

Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,

The bag contains the intersection of these bags: The bag contains 1 items:
One,

First bag: The bag contains 5 items:
1, 2, 3, 4, 5,

Second bag: The bag contains 5 items:
20, 30, 40, 50, 1,

The bag contains the union of these bags: The bag contains 4 items:
2, 3, 4, 5,

First bag: The bag contains 5 items:
One, Two, Three, Four, Five,

Second bag: The bag contains 5 items:
Twenty, Thirty, Fourth, Fifty, One,

The bag contains the intersection of these bags: The bag contains 4 items:
Two, Three, Four, Five,