Question

I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

I am having trouble with my C++ program....

Directions:

In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted.
The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the linked list. It should provide the following public methods:
 IntegerSet(int size): this constructor method creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.
 int size(): this method returns the cardinality of the set.
 boolean isEmpty(): this method returns true if the set has no elements, false otherwise.
 boolean isMember(int item): this method returns true if item is an element of the set, false otherwise.
 boolean add(int item): if item is not in the set, this method adds it to the set and returns true; otherwise, the set is unchanged and the method returns false.
 boolean remove(int item): if item is in the set, this method removes it from the set and returns true; otherwise, the set is unchanged and the method returns false.
 boolean isSubset(IntegerSet set2): this method returns true if every element in set2 is also in the calling set, false otherwise.
 IntegerSet intersection(IntegerSet set2): this method returns a new set that is the intersection of the calling set and set2. An integer is an element of the intersection set if it is in both the calling set and set2.
 IntegerSet union(IntegerSet set2): this method returns a new set that is the union of the calling set and set2. An integer is an element of the union set if it is in either the calling set or set2, or both. Keep in mind that the union set formed should not contain any duplicates.
 IntegerSet difference(IntegerSet set2): this method returns a new set that is the difference between the calling set and set2 (in this order). An integer is an element of the difference set if it is in the calling set but not in set2.
 void display(): this method prints all elements of the set on the screen.
In addition to the IntegerSet class, you are also required to write a test driver that serves as the test harness for the IntegerSet class. This test harness should provide the user with options to explicitly test each method in the IntegerSet class.

Program so far:

Header.h

#include <iostream>

#include <cstdlib>

using namespace std;

class IntegerSet

{

private:

struct Node

{

int value;

Node * next;

};

int cardinality = 0;

Node *front;

public:

IntegerSet();

IntegerSet(int size); // creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.

int size(); // returns the cardinality of the set.

bool isEmpty(); // returns true if the set has no elements, false otherwise.

bool isMember(int item); // returns true if item is an element of the set, false otherwise.

bool add(int item); // if item is not in the set, this method adds it to the set and returns true; otherwise, the set is unchanged and the method returns false.

bool remove(int item); // if item is in the set, this method removes it from the set and returns true; otherwise, the set is unchanged and the method returns false.

bool isSubset(IntegerSet set2); // returns true if every element in set2 is also in the calling set, false otherwise.

IntegerSet intersection(IntegerSet set2); // returns a new set that is the intersection of the calling set and set2. An integer is an element of the intersection set if it is in both the calling set and set2.

IntegerSet Union(IntegerSet set2); // returns a new set that is the union of the calling set and set2.An integer is an element of the union set if it is in either the calling set or set2, or both.Keep in mind that the union set formed should not contain any duplicates.

IntegerSet difference(IntegerSet set2); // returns a new set that is the difference between the calling set and set2 (in this order). An integer is an element of the difference set if it is in the calling set but not in set2.

void display(); // prints all elements of the set on the screen.

};

IntegerSet::IntegerSet()

{

front = NULL;

cardinality = 0;

}

// Creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.

IntegerSet::IntegerSet(int size)

{

front = NULL;

cardinality = size;

};

// Returns the size of Cardinality

int IntegerSet::size()

{

front = NULL;

return cardinality;

}

// Checks if cardinality is empty

bool IntegerSet::isEmpty()

{

if (cardinality == 0)

return true;

else

return false;

}

// Returns true if item is an element of the set, false otherwise.

bool IntegerSet::isMember(int item)

{

Node *flag = front;

while (flag->next != NULL)

{

if (flag->value == item)

return true;

else

flag = flag->next;

}

return false;

}

// Checks if item is a member or not, if it is return false, else return true

bool IntegerSet::add(int item)

{

if (isMember(item) == true)

return false;

else

{

Node *Point = new Node;

Point->value = item;

Point->next = front->next;

front->next = Point;

cardinality++;

return true;

}

}

bool IntegerSet::remove(int item)

{

if (isMember(item))

{

Node *n1, *n2;

n1 = front;

n2 = front->next;

if (front->value = item)

{

front = front->next;

return true;

}

while (n2 != 0)

{

if (n2->value = item)

{

n1->next = n2->next;

return true;

}

else

{

n1 = n1->next;

n2 = n2->next;

}

}

}

else

return false;

}

bool IntegerSet::isSubset(IntegerSet set2)

{

Node *n;

n = set2.front;

while (n->next != NULL)

{

if (isMember(n->value))

n = n->next;

else

return false;

}

return true;

}

IntegerSet IntegerSet::intersection(IntegerSet set2)

{

IntegerSet set3;

int x = 0;

Node *n1, *n3;

n1 = set2.front;

n3 = set3.front;

Node *n2 = new Node;

while (n1->next != NULL)

{

if (isMember(n1->value))

{

if (x == 0)

{

n3->value = n1->value;

n1 = n1->next;

x = 1;

set3.cardinality++;

}

else

{

n2 = new Node;

n2->value = n1->value;

n3->next = n2;

n3 = n3->next;

n1 = n1->next;

set3.cardinality++;

}

}

else

n1 = n1->next;

}

return set3;

}

IntegerSet IntegerSet::Union(IntegerSet set2)

{

IntegerSet set3;

int x = 0;

Node *n1, *n3;

n1 = set2.front;

n3 = set3.front;

Node *n2 = new Node;

while (n1->next != NULL)

{

if (x == 0)

{

n3->value = n1->value;

n1 = n1->next;

x = 1;

set3.cardinality++;

}

else

{

set3.add(n1->value);

n1 = n1->next;

}

}

Node *n = front;

while (n->next != NULL)

{

if (n3 != front)

{

set3.add(n->value);

n = n->next;

}

else

{

n3->value = n->value;

n = n->next;

set3.cardinality++;

}

}

return set3;

}

IntegerSet IntegerSet::difference(IntegerSet set2)

{

IntegerSet set3;

int x = 0;

Node *n1, *n3;

n1 = front;

n3 = set3.front;

Node *n2 = new Node;

while (n1->next != NULL)

{

if (set2.isMember(n1->value) == false)

{

if (x == 0)

{

n3->value = n1->value;

n1 = n1->next;

x = 1;

set3.cardinality++;

}

else

{

set3.add(n1->value);

}

}

else

n1 = n1->next;

}

return set3;

}

void IntegerSet::display()

{

Node *top = front;

while (top != NULL)

{

cout << top->value << ", ";

top = top->next;

}

}

Source.cpp

//Project 5

#include <iostream>

#include <cstdlib>

#include "Header.h"

using namespace std;

int main()

{

int i;

int options = 1;

cout << "Input IntegerSet size" << endl;

cin >> i;

IntegerSet set1(i);

while (options > 0 || options <=8)

{

cout << "1 - Add Item" << endl;

cout << "2 - Remove Item" << endl;

cout << "3 - Check isMember" << endl;

cout << "4 - Subset" << endl;

cout << "5 - Intersection" << endl;

cout << "6 - Union" << endl;

cout << "7 - Difference" << endl;

cout << "8 - End" << endl;

cin >> options;

switch (options)

{

case 1:

{

cout << "Input new item: " << endl;

cin >> i;

set1.add(i);

set1.display();

break;

}

case 2:

{

cout << "Input item to remove: " << endl;

cin >> i;

set1.remove(i);

set1.display();

break;

}

case 3:

{

cout << "Input item to check: " << endl;

cin >> i;

if (set1.isMember(i) == true)

{

cout << i << " is a member." << endl;

}

else {

cout << i << " is not a member." << endl;

}

break;

}

case 4:

{

cout << "Input size of new IntegerSet: " << endl;

cin >> i;

IntegerSet set2(i);

IntegerSet subset = set1.isSubset(set2);

subset.display();

break;

}

case 5:

{

cout << "Input size of new IntegerSet: " << endl;

cin >> i;

IntegerSet set2(i);

IntegerSet inter = set1.intersection(set2);

inter.display();

break;

}

case 6:

{

cout << "Input size of new IntegerSet: " << endl;

cin >> i;

IntegerSet set2(i);

IntegerSet un = set1.Union(set2);

un.display();

break;

}

case 7:

{

cout << "Input size of new IntegerSet: " << endl;

cin >> i;

IntegerSet set2(i);

IntegerSet differ = set1.difference(set2);

differ.display();

}

case 8:

{

break;

}

}

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question.
Please don't forget to rate the answer if it was helpful. Thank you

Header.h
=======
#include <iostream>
#include <cstdlib>
using namespace std;
class IntegerSet
{
private:
struct Node
{
int value;
Node * next;
};
int cardinality ;
Node *front;
public:
IntegerSet();
IntegerSet(int size); // creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.
int size(); // returns the cardinality of the set.
bool isEmpty(); // returns true if the set has no elements, false otherwise.
bool isMember(int item); // returns true if item is an element of the set, false otherwise.
bool add(int item); // if item is not in the set, this method adds it to the set and returns true; otherwise, the set is unchanged and the method returns false.
bool remove(int item); // if item is in the set, this method removes it from the set and returns true; otherwise, the set is unchanged and the method returns false.
bool isSubset(IntegerSet set2); // returns true if every element in set2 is also in the calling set, false otherwise.
IntegerSet intersection(IntegerSet set2); // returns a new set that is the intersection of the calling set and set2. An integer is an element of the intersection set if it is in both the calling set and set2.
IntegerSet Union(IntegerSet set2); // returns a new set that is the union of the calling set and set2.An integer is an element of the union set if it is in either the calling set or set2, or both.Keep in mind that the union set formed should not contain any duplicates.
IntegerSet difference(IntegerSet set2); // returns a new set that is the difference between the calling set and set2 (in this order). An integer is an element of the difference set if it is in the calling set but not in set2.
void display(); // prints all elements of the set on the screen.
};
IntegerSet::IntegerSet()
{
front = NULL;
cardinality = 0;
}

// Creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.
IntegerSet::IntegerSet(int size)
{
front = NULL;
cardinality = 0;
int num;
cout << "Enter " << size << " elements for the set" << endl;
for(int i = 0; i < size; i++)
{
cin >> num;
add(num);
}
};

// Returns the size of Cardinality
int IntegerSet::size()
{
return cardinality;
}

// Checks if cardinality is empty
bool IntegerSet::isEmpty()
{
if (cardinality == 0)
return true;
else
return false;
}

// Returns true if item is an element of the set, false otherwise.
bool IntegerSet::isMember(int item)
{
Node *curr = front;
while (curr != NULL)
{
if (curr->value == item)
return true;
else
curr = curr->next;
}
return false;
}

// Checks if item is a member or not, if it is return false, else return true
bool IntegerSet::add(int item)
{
if (isMember(item) == true)
return false;
else
{
  
  
Node *prev = NULL;
Node *curr = front;
while(curr != NULL)
{
if(item < curr->value)
break;
prev = curr;
curr = curr->next;
}
  
Node *n = new Node;
n->value = item;
n->next = curr;
  
if(prev == NULL) //inserting as first node
front = n;
else
prev->next = n;
cardinality++;
return true;
}
}


bool IntegerSet::remove(int item)
{
Node *prev = NULL, *curr = front;
while(curr != NULL)
{
if(curr->value == item)
break;
prev = curr;
curr = curr->next;
}
  
if(curr == NULL) //did not find item
return false;
  
if(prev == NULL) //removing first node?
front = curr->next;
else
prev->next = curr->next;
  
delete curr;
cardinality--;
return true;
}

bool IntegerSet::isSubset(IntegerSet set2)
{
Node *n;
n = set2.front;
while (n != NULL)
{
if (isMember(n->value))
n = n->next;
else
return false;
}
return true;
}
IntegerSet IntegerSet::intersection(IntegerSet set2)
{
IntegerSet set3;
Node* n = set2.front;
while (n != NULL)
{
if (isMember(n->value))
set3.add(n->value);
n = n->next;
}
return set3;
  
}

IntegerSet IntegerSet::Union(IntegerSet set2)
{
IntegerSet set3;
Node* n = front;
while(n != NULL)
{
set3.add(n->value);
n = n->next;
}
  
n = set2.front;
while(n != NULL)
{
if(!set3.isMember(n->value))
set3.add(n->value);
n = n->next;
}

  
return set3;
}
IntegerSet IntegerSet::difference(IntegerSet set2)
{
IntegerSet set3;
Node* n = front;
while(n != NULL)
{
if(!set2.isMember(n->value))
set3.add(n->value);
n = n->next;
}
return set3;
}
void IntegerSet::display()
{
Node *top = front;
if(top != NULL)
{
cout << top->value;
top = top->next;
while (top != NULL)
{
cout << ", " << top->value;
top = top->next;
}
}
cout << endl;
  
}

Source.cpp

========
//Project 5
#include <iostream>
#include <cstdlib>
#include "Header.h"
using namespace std;

int main()
{
int i;
int options = 1;
cout << "Input IntegerSet size" << endl;
cin >> i;
IntegerSet set1(i);
while (options > 0 && options <8)
{
cout << "1 - Add Item" << endl;
cout << "2 - Remove Item" << endl;
cout << "3 - Check isMember" << endl;
cout << "4 - Subset" << endl;
cout << "5 - Intersection" << endl;
cout << "6 - Union" << endl;
cout << "7 - Difference" << endl;
cout << "8 - End" << endl;
cin >> options;
switch (options)
{
case 1:
{
cout << "Input new item: " << endl;
cin >> i;
set1.add(i);
set1.display();
break;
}
case 2:
{
cout << "Input item to remove: " << endl;
cin >> i;
set1.remove(i);
set1.display();
break;
}
case 3:
{
cout << "Input item to check: " << endl;
cin >> i;
if (set1.isMember(i) == true)
{
cout << i << " is a member." << endl;
}
else {
cout << i << " is not a member." << endl;
}
break;
}
case 4:
{
cout << "Input size of new IntegerSet: " << endl;
cin >> i;
IntegerSet set2(i);
if(set1.isSubset(set2))
cout << "set2 is subset of set1" << endl;
else
cout << "set2 is NOT subset of set1" << endl;

break;
}
case 5:
{
cout << "Input size of new IntegerSet: " << endl;
cin >> i;
IntegerSet set2(i);
IntegerSet inter = set1.intersection(set2);
cout << "The intersection set contains:" << endl;

inter.display();
break;
}
case 6:
{
cout << "Input size of new IntegerSet: " << endl;
cin >> i;
IntegerSet set2(i);
cout << "The union set contains:" << endl;

IntegerSet un = set1.Union(set2);
un.display();
break;
}
case 7:
{
cout << "Input size of new IntegerSet: " << endl;
cin >> i;
IntegerSet set2(i);
IntegerSet differ = set1.difference(set2);
cout << "The difference set contains:" << endl;
differ.display();
}
break;
case 8:
{
break;
}
}
}
return 0;
}

output
========
Input IntegerSet size
3
Enter 3 elements for the set
2 4 6
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
1
Input new item:
5
2, 4, 5, 6
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
2
Input item to remove:
4
2, 5, 6
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
3
Input item to check:
10
10 is not a member.
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
3
Input item to check:
5
5 is a member.
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
4
Input size of new IntegerSet:
5
Enter 5 elements for the set
2 1 4 3 5
set2 is NOT subset of set1
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
4
Input size of new IntegerSet:
2
Enter 2 elements for the set
2 6
set2 is subset of set1
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
5
Input size of new IntegerSet:
1
Enter 1 elements for the set
5
5
1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
6
Input size of new IntegerSet:
3
Enter 3 elements for the set
1 2 3
1, 2, 3, 5, 6

1 - Add Item
2 - Remove Item
3 - Check isMember
4 - Subset
5 - Intersection
6 - Union
7 - Difference
8 - End
8

Add a comment
Know the answer?
Add Answer to:
I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can i please get help on this? i don't know how to do it and I'm...

    can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #incl...

    I can't get my code to work on xcode and give me an output. #include <conio.h> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <string> #include <vector> using namespace std; // So "std::cout" may be abbreviated to "cout" //Declare global arrays int dummy1[10]; int dummy2[10]; int dummy3[10]; int universalSet[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Function to return statement "Empty thread" when the resultant set is empty string isEmpty(int arr[]) { string...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

  • c++ Some pieces of the following code are missing. Fill in the blanks of the following...

    c++ Some pieces of the following code are missing. Fill in the blanks of the following program. Each answer must fit on a single line. include <iostream> #include <cstdib #include <ctimes using namespace std; randoml_event return 2 (rand() 5 random2 intre + rand 90 return ret: random1_1000X rand) intret 100 return ret: fint 2) return nt; return n2 is oder Is_odot if (n%2 0) return return true; int sum3 (int n, int n, int n3 X int sum= n1 +...

  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT