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

Please I need help with htis program if you can , and its in C++. would you tell

ID: 3873904 • Letter: P

Question

Please I need help with htis program if you can , and its in C++. would you tell me what to do in the main file an how to connect them all together.

thanks alot

First, combine the two into one set by using + (union) operator, and print out the union set. The + operator must remove all duplicates and store one copy of any item. You also need to implement a subtraction operator (difference) where (A-B means removing all elements of B from A) and print out the result. So the main function takes two sets of integers and print the result of + and - operation.

** @file ArrayBag.cpp */

#include "ArrayBag.h"


ArrayBag::ArrayBag() : itemCount(0), maxItems(DEFAULT_ArrayBag_SIZE)
{
} // end default constructor


int ArrayBag::getCurrentSize() const
{
   return itemCount;
} // end getCurrentSize


bool ArrayBag::isEmpty() const
{
   return itemCount == 0;
} // end isEmpty


bool ArrayBag::add(const ItemType& newEntry)
{
   bool hasRoomToAdd = (itemCount < maxItems);
   if (hasRoomToAdd)
   {
       items[itemCount] = newEntry;
       itemCount++;
   } // end if
  
   return hasRoomToAdd;
} // end add


bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
   bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
   if (canRemoveItem)
   {
       itemCount--;
       items[locatedIndex] = items[itemCount];
   } // end if
  
   return canRemoveItem;
} // end remove


void ArrayBag::clear()
{
   itemCount = 0;
} // end clear


int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int searchIndex = 0;
while (searchIndex < itemCount)
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
  
searchIndex++;
} // end while

return frequency;
} // end getFrequencyOf


bool ArrayBag::contains(const ItemType& anEntry) const
{
   return getIndexOf(anEntry) > -1;
} // end contains

std::vector ArrayBag::toVector() const
{
std::vector ArrayBagContents;
   for (int i = 0; i < itemCount; i++)
ArrayBagContents.push_back(items[i]);
return ArrayBagContents;
} // end toVector

// private

int ArrayBag::getIndexOf(const ItemType& target) const
{
   bool found = false;
int result = -1;
int searchIndex = 0;
// if the ArrayBag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while

return result;
} // end getIndexOf

// ArrayBag.h ////

// setinput.txt //
3 4 5 7 5 16 7 12 11 12 3 9 9 8 1 12
15 4 3 6 1 12 3 12 7 8 19 9 11 12 8 5 -4 -100

Validate that all inputs are integers. Anything else should be rejected with an error message.

/** ADT bag: Array-based implementation.
@file ArrayBag.h */


#ifndef BAG_
#define BAG_

#include

typedef int ItemType;
class ArrayBag
{
private:
   static const int DEFAULT_BAG_SIZE = 100;
   ItemType items[DEFAULT_BAG_SIZE]; // 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;
std::vector toVector() const;
}; // end Bag

Explanation / Answer

Given below is the implementation of the Set class using ArrayBag to store the elements. The output of the TestSet.cpp is shown in the end. Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.

Set.h


#ifndef Set_h
#define Set_h
#include "ArrayBag.h"
class Set
{
private:
ArrayBag set;
public:
Set();
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;
std::vector<ItemType> toVector() const;
Set operator +(const Set &other);
Set operator -(const Set &other);
};

#endif /* Set_h */

Set.cpp


#include "Set.h"
Set::Set()
{
  
}
int Set::getCurrentSize() const
{
return set.getCurrentSize();
}
bool Set::isEmpty() const
{
return set.isEmpty();
}
bool Set::add(const ItemType& newEntry)
{
if(set.contains(newEntry))
return false; //don't add if already present
else
{
set.add(newEntry);
return true;
}
}
bool Set::remove(const ItemType& anEntry)
{
return set.remove(anEntry);
}
void Set::clear()
{
set.clear();
}
bool Set::contains(const ItemType& anEntry) const
{
return set.contains(anEntry);
}

std::vector<ItemType> Set::toVector() const
{
return set.toVector();
}
Set Set::operator +(const Set &other)
{
Set result;
std::vector<ItemType> setA = toVector();
for(int i = 0; i < setA.size(); i++)
result.add(setA[i]);
  
std::vector<ItemType> setB = other.toVector();
for(int i = 0; i < setB.size(); i++)
result.add(setB[i]); //while adding elements of setB, the add() method makes sure not to add duplicates
  
return result;
  
}
Set Set::operator -(const Set &other)
{
Set result;
std::vector<ItemType> setA = toVector();
for(int i = 0; i < setA.size(); i++)
{
if(!other.contains(setA[i])) //add it only if other set does not contain the element
result.add(setA[i]);
}
return result;
}

TestSet.cpp

#include "Set.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

void extractNumbers(char *line, Set &setA); //extracst the individual numbers in the line and adds them to setA

void display(std::vector<ItemType> vec);

int main()
{
Set setA, setB;
char line[256];
ifstream infile("setinput.txt");
  
if(!infile.is_open())
{
cout << "could not open input file setinput.txt" << endl;
return 1;
}
  
//get the 1st line and addd to setA
infile.getline(line, 256);
extractNumbers(line, setA);
  
//get the 2nd line and addd to setA
infile.getline(line, 256);
extractNumbers(line, setB);
  
infile.close();
  
Set unioN = setA + setB;
Set diffA_B = setA - setB;
Set diffB_A = setB - setA;
  
cout << " setA: ";
display(setA.toVector());
  
cout << " setB: ";
display(setB.toVector());
  
cout << " A + B: ";
display(unioN.toVector());
  
cout << " A - B: ";
display(diffA_B.toVector());
  
cout << " B - A: ";
display(diffB_A.toVector());
  
return 0;
}


void extractNumbers(char *line, Set &setA)
{
char delimiters[] = " ";
char *token;
token = strtok(line, delimiters);
while(token != NULL)
{
setA.add(atoi(token));
token = strtok(NULL, delimiters);
}
}

void display(std::vector<ItemType> vec)
{
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
}

input file : setinput.txt

3 4 5 7 5 16 7 12 11 12 3 9 9 8 1 12
15 4 3 6 1 12 3 12 7 8 19 9 11 12 8 5 -4 -100

output

$ g++ ArrayBag.cpp Set.cpp TestSet.cpp

$ ./a.out

setA: 3 4 5 7 16 12 11 9 8 1

setB: 15 4 3 6 1 12 7 8 19 9 11 5 -4 -100

A + B: 3 4 5 7 16 12 11 9 8 1 15 6 19 -4 -100

A - B: 16

B - A: 15 6 19 -4 -100