Create the abstract data type IntSet used to implement a mathematical set of non-negative integers (includes zero). Each object of class IntSet can keep track of whether integers are in the set or not. Implement as follows.
A set is to be represented internally as a bool array containing true and false. For example, if you call the array set, then set[num] is true if the integer num is in the set, false if num is not in the set. Use "new" to dynamically allocate memory for the array. One object’s array stores one set
Develop a full class with the following operations:
1. operator+: returns the union of two sets, the set of elements that are contained in either or both sets. (Note - do not call any identifiers “union” as that is a keyword in C++.)
2. operator*: returns the intersection of two sets, the set of all elements that are common to both sets.
3. operator-: returns the difference of two sets, say A-B, which is the set of all elements that are in set A but not in set B. (E.g., A = { 2 3 5 30}, B = { 3 5 10 20}, then A-B is { 2 30}.)
4. operator=: the assignment operator to assign one set to another.
5. operators (+=, *=, -=): the union, intersection, difference assignment.
6. operators (==, !=): bool equality and inequality. A == B if they contain the same elements.
7. insert: insert an element into a set, has bool return value,, e.g., bool success = A.insert(5); (Note: ignore invalid integers, return false, e.g., bool success = A.insert(-10); If a value is already in the set and is inserted, return true).
8. remove: remove an element from a set, bool return value, e.g., bool success = A.remove(10); (Note: ignore invalid integers, return false, e.g,. bool success = A.remove(-10); ).
9. isEmpty: determine if a set is empty or not, bool return value, e.g., if (A.isEmpty()) ...
10. isInSet: determine if an integer is in the set or not, e.g., if (A.isInSet(5)) ... (Note: returns false for invalid integers).
11. operator>: to input an entire set. (Note: This is a simple loop to take integers; terminate at some sentinel value, say -1. Ignore invalid integers. Zero is valid as it is non-negative.)
13. All proper constructor and destructor functions; constructor(s) initialize a set to (1) The "empty set" (a set which has no elements) or, (2) A set that can take up to five values to be inserted into the set
e.g., IntSet A, B(8), C(12,5), D(5,-10,12), E(100,2,-5,45), F(1,2,3,4,5); IntSet X(D); // X is an exact copy of D.
Set A is the empty set. (For implementation, you could use nullptr, but using an array of one element keeps the implementation simpler and cleaner.) While A is currently the empty set, after the operator>> or operator= or insert() is used, the set could contain many values. Set B initially holds an 8. Set C has a current largest value of 12, also contains the value 5. The constructor for D will ignore the -10, but is initialized to contain 5 and 12. E is { 2 45 100}. F is { 1 2 3 4 5}. X is a copy of D, is { 5 12}. You must initially size the arrays by the parameters, e.g., A's array is size 1, B's array is size 9, C's array is size 13, etc.
Later, after operations, the size may change. You may need to make arrays larger for operations (e.g., A = B;); but since new and delete are costly operations, typically, for efficiency reasons, don’t allocate new arrays unless needed and never shrink them if less space is needed.
Often, when more memory is needed for an array, applications will double the array size to minimize the number of new and delete operations. Do not do that in your program. This assignment is practice in memory allocation and deallocation. Having a larger than needed array can mask errors.
As always expected when programming, comment clearly and thoroughly. Clearly state any assumptions and implementation details in the beginning comment block of the class definition. Comments in the class definition file should describe the ADT and all functionality. Functions in the .cpp file must be separated using comment separators as shown in examples and expand all tabs to spaces. You must handle errors involving a set, e.g., trying to access a non-existent value, but you do not need to worry about data type. In other words, you will not get non-integers, e.g., a char instead of an int as input. This is best handled with exception handling (not covered).
// Example program
#include <iostream>
#include <string>
using namespace std;
class IntSet
{
int *set; //Set to store integers
int size = 0; //size of the set
public:
IntSet(){
set = NULL; //initializes set to NULL when no values passed
}
// Construtor when one value is passed
IntSet(int a){
insert(a);
}
// Construtor when two values are passed
IntSet(int a,int b){
insert(a);
insert(b);
}
// Construtor when three value are passed
IntSet(int a,int b,int c){
insert(a);
insert(b);
insert(c);
}
// Construtor when four value are passed
IntSet(int a,int b,int c,int d){
insert(a);
insert(b);
insert(c);
insert(d);
}
// Construtor when five value are passed
IntSet(int a,int b,int c,int d,int e){
insert(a);
insert(b);
insert(c);
insert(d);
insert(e);
}
// Constructor for passing an array
IntSet(int *pSet){
set = pSet;
}
//INSERTING ELEMENT
bool insert(int elm){
// Check if element is negative
if(elm<0){
return false;
}
//check for size
if(size==0)
{
set = new int[1];
size = 1;
}
else{
//increase memory size of array and element by creating a temporary
array
int* newSet = new int[size+1];
for(int i = 0; i < size; i++)
newSet[i] = set[i];
size += 1;
delete [] set;
set = newSet;
}
set[size-1]=elm;
return true;
}
//REMOVING ELEMENT
bool remove(int elm){
//check for negative remove element
if(elm<0)
return false;
IntSet temp;//temporary object
bool found = false;//variable to track if removed something or
not
//to remove element
for(int i = 0; i < size; i++){
if(set[i] != elm)
{
temp.insert(set[i]);
}
else{
found = true;
}
}
//pointing the set to new set of values
delete this->set;
this->set = temp.set;
this->size = temp.getSize();
return found;
}
//union
IntSet operator+(IntSet &b){
IntSet temp;//temporaray object
//copying values of first object to temp
for(int i = 0;i<this->getSize();i++){
temp.insert(this->getElementAt(i));
}
//copying values of second object to temp only if not present in
temp
for(int i = 0;i<b.getSize();i++){
if(!temp.isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
return temp;
}
//intersection
IntSet operator*(IntSet &b){
IntSet temp;//temporary object
//coppies the values which are present in both the sets
for(int i = 0;i<b.getSize();i++){
if(this->isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
return temp;
}
//difference
IntSet operator-(IntSet &b){
IntSet temp;//temporary object
//copies value from first object to temp if it is not present in
both the objects
for(int i = 0;i<this->getSize();i++){
if(! b.isInSet(this->getElementAt(i)))
temp.insert(this->getElementAt(i));
}
//copies value from second object to temp if it is not present in
both the objects
for(int i = 0;i<b.getSize();i++){
if(! this->isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
return temp;
}
//copy values of one set to another by making union of both
set
IntSet operator+=(IntSet &b){
IntSet temp;//temporary object
//copies values from first object to temp
for(int i = 0;i<this->getSize();i++){
temp.insert(this->getElementAt(i));
}
//copies values from second object to temp if not already
present
for(int i = 0;i<b.getSize();i++){
if(!temp.isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
//deletes the current set and replaces with the new formed set and
also changes the size
delete this->set;
this->set = temp.set;
this->size = temp.getSize();
return temp;
}
//copy values of one set to another by making intersection of both
set
IntSet operator*=(IntSet &b){
IntSet temp;//temporary object
//copy values to temp if present in both thee sets
for(int i = 0;i<b.getSize();i++){
if(this->isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
//deletes the current set and replaces with the new formed set and
also changes the size
delete this->set;
this->set = temp.set;
this->size = temp.getSize();
return temp;
}
//copy values of one set to another by making difference of both
set
IntSet operator-=(IntSet &b){
IntSet temp;//temporary object
//copies value from first object to temp if it is not present in
both the objects
for(int i = 0;i<this->getSize();i++){
if(! b.isInSet(this->getElementAt(i)))
temp.insert(this->getElementAt(i));
}
//copies value from second object to temp if it is not present in
both the objects
for(int i = 0;i<b.getSize();i++){
if(! this->isInSet(b.getElementAt(i)))
temp.insert(b.getElementAt(i));
}
//deletes the current set and replaces with the new formed set and
also changes the size
delete this->set;
this->set = temp.set;
this->size = temp.getSize();
return temp;
}
//Copies one object to another
IntSet operator=(IntSet b){
IntSet temp;
for(int i = 0;i<b.getSize();i++){
temp.insert(b.getElementAt(i));
}
return temp;
}
//compares if two sets are same or not
bool operator==(IntSet b){
if(this->getSize() != b.getSize())
return false;
for(int i = 0;i<size;i++){
if(this->getElementAt(i) != b.getElementAt(i))
return false;
}
return true;
}
//compares if two sets are different or not
bool operator!=(IntSet b){
if(this->getSize() != b.getSize())
return true;
for(int i = 0;i<size;i++){
if(this->getElementAt(i) != b.getElementAt(i))
return true;
}
return false;
}
IntSet operator>>(IntSet b){
IntSet temp;
for(int i = 0;i<b.getSize();i++){
temp.insert(b.getElementAt(i));
}
//deletes the current set and replaces with the new formed set and
also changes the size
delete this->set;
this->set = temp.set;
this->size = temp.getSize();
return temp;
}
//destructor
~IntSet(){
//delete [] set;
}
//returns the current size of the set
int getSize()
{
return size;
}
//returns the element at a particular position of the set
int getElementAt(int pos)
{
if(pos>=0 && pos<size)
return set[pos];
else
return -1;
}
//checks if an element is present in a set
bool isInSet(int elm){
for(int i = 0;i<size;i++)
{
if(elm == set[i])
return true;
}
return false;
}
//checks if set is empty or not
bool isEmpty(){
if(size == 0)
return true;
return false;
}
//prints all elements of the set
void print(){
for(int i = 0;i<size;i++)
{
cout<<set[i]<<" ";
}
}
};
int main()
{
IntSet A, B(8), C(12,5), D(5,-10,12), E(100,2,-5,45),
F(1,2,3,4,5);
//Playground for playing with different operators and
functions
}
Create the abstract data type IntSet used to implement a mathematical set of non-negative integers (includes...
Write the C module intSet to implement an unordered set of integers according to the specification given below. File intSet.h (downloadable off the class web pages): #ifndef INTSET_H #define INTSET_H typedef struct intsetType *intSet; intSet createSet(); // returns an intSet created at runtime using malloc void destroySet(intSet); // frees up the memory associated with its argument void clear(intSet); // clears set to empty (freeing any memory if necessary) int card(const intSet); // returns the cardinality of the set bool equals(const...
Write the C module intSet to implement an unordered set of integers according to the specification given below. File intSet.h: #ifndef INTSET_H #define INTSET_H typedef struct intsetType *intSet; intSet createSet(); // returns an intSet created at runtime using malloc void destroySet(intSet); // frees up the memory associated with its argument void clear(intSet); // clears set to empty (freeing any memory if necessary) int card(const intSet); // returns the cardinality of the set bool equals(const intSet,const intSet); // returns true if...
(C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...
This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...
Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...
C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the following attributes: Data members (private): string: username int array: ratings Number of elements should be size int: numRatings Number of books in the database Int: size The capacity of the ratings array (50). Constant Member functions (public): Default constructor Sets username to an empty string, numRatings to 0, size to 50, and all the elements of ratings array to the value 0 Parameterized constructor...
/* FILE NAME: Class{aSet}.cpp FUNCTION: A template class for a set in C++. It implements all the set operations, except set compliment: For any two sets, S1 and S2 and an element, e A. Operations which result in a new set: (1) S1 + S2 is the union of S1 and S2 (2) S1 - S2 is the set difference of S1 and S2, S1 - S2 (3) S1 * S2 is the set intersection of S1 and S2, S1 * S2 (4) S1 + e (or e +...
/ Animal.hpp
#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <string>
class Animal
{
public:
Animal();
Animal(std::string, bool domestic=false, bool
predator=false);
std::string getName() const;
bool isDomestic() const;
bool isPredator() const;
void setName(std::string);
void setDomestic();
void setPredator();
protected: // protected so that derived class can directly
access them
std::string name_;
bool domestic_;
bool predator_;
};
#endif /* ANIMAL_H_ */
//end of Animal.h
//
/////////////////////////////////////////////////////////////////Animal.cpp
#include "Animal.h"
Animal::Animal(): name_(""),domestic_(false),
predator_(false){
}
Animal::Animal(std::string name, bool domestic, bool
predator):
name_(name),domestic_(domestic), predator_(predator)
{
}
std::string Animal::getName() const{
return...
C++ Please ensure code is fully working, will rate Question to be answered: Repeat Exercise 1 for the class linkedStackType question one: Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...
program will enter data into two single dimension arrays (do not store duplicate values in arrays) program will find the union and intersection of the two arrays using one function program will find the symmetric difference of two arrays program will display the union, intersection, and symmetric difference */ short* input_data(short size); // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...