Question

Define a class ArraySet using an array that represents a set and implements the ADT Set....

  1. Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation.

Hi,

I wrote the program but I don"t know why it showing the output - 000

000

0

0

My main.cpp file code is this -

#include<stdio.h>

#include<iostream>

#include<fstream>

#include "ArraySet.h"

#include "ArraySet.cpp"

using namespace std;

int main(){

ArraySet<int> setA, setB, setC;

// adds to setA

setA.add(10);

setA.add(20);

setA.add(30);


// prints set A

setA.display();

// adds to setB

setB.add(30);

setB.add(40);

setB.add(50);

// prints set B

setB.display();

// unions of setA and setB

setC=setA.unions(setB);

setC.display();

// intersections of setA and setB

setC = setA.intersection(setB);

setC.display();

// difference of setA and setB

setC = setA.difference(setB);

setC.display();

// removes 30 from setA

setA.remove(10);

setA.display();

// clear the setC

setC.clear();

setC.display();

}




And my ArraySet.h file code is this -

#ifndef BAG_H

#define BAG_H

template <class ItemType>

// ArraySet class

class ArraySet {

private:

ItemType* items;

int count;

int capacity;

public:

ArraySet(); // default constructure

void display( ) const;

int getCurrentSize() const;

bool isEmpty() const;

bool add(const ItemType& newEntry);

bool remove(const ItemType& anEntry);

bool remove ();

void clear();

bool contains(const ItemType& anEntry);

ItemType* toArray();

ArraySet<ItemType> unions (ArraySet<ItemType> setB);

ArraySet<ItemType> intersection (ArraySet<ItemType> setB);

ArraySet<ItemType> difference (ArraySet<ItemType> setB);

};

#endif


And my ArraySet.cpp file code is this -

#include "ArraySet.h"

#include<iostream>

using namespace std;

template <class ItemType>

ArraySet<ItemType>::ArraySet(){

int size;

items = new ItemType[size];

count = 0;

capacity = size;

}

// dispaly Function

template<class ItemType>

void ArraySet<ItemType>::display() const

{

int i;

for(i = 0; i<count; i++)

{

cout<< items[i] << " " ;

}

cout<< endl;

}

// Function to get the current number of entries in this set

template <class ItemType>

int ArraySet<ItemType>:: getCurrentSize() const

{

return count;

}

// Check wheather this set is empty

template <class ItemType>

bool ArraySet<ItemType>::isEmpty()const {

return count == 0;

}

// Add a new entry to this set avoiding duplicates

template <class ItemType>

bool ArraySet<ItemType>::add (const ItemType& newEntry) {

for (int i =0; i<count; i++)

{

if (items[i] == newEntry)

return false;

}

if (count == capacity){

capacity = 2* capacity+2;

}

ItemType*temp = new ItemType[capacity];

for (int i =0; i<count; i++ )

{

temp[i] = items[i];

}

temp[count] = newEntry;

count++;

return true;

}

// Test wheather this set comtains a given entry

template<class ItemType>

bool ArraySet<ItemType>:: contains(const ItemType& anEntry) {

for (int i = 0; i <count ; i ++){

if (items[i] == anEntry){

return true;

}

return false;

}

}

template< class ItemType>

bool ArraySet<ItemType>:: remove(const ItemType& anEntry){

int i;

for(int i=0; i<count ; i++)

{

if (items[i] == anEntry) break;

}

if (i == count)

return false;

for ( int j =1; i <count-1 ; j++)

{

items[i] = items[i +1];

}

count --;

return true;

}

// checks wheather this set is empty

template< class ItemType>

bool ArraySet<ItemType>:: remove(){

if(count==0)

return true ;

ItemType anEntry=items[count-1];

remove(anEntry);

return anEntry;

}

// removes all entries from the set

template< class ItemType>

void ArraySet<ItemType>:: clear(){

delete items;

count = 0;

}

// retrieves all the entries that are in the set

template <class ItemType>

ItemType* ArraySet<ItemType>:: toArray(){

ItemType* temp= new ItemType[count];

for (int i = 0; i < count; i++)

{

temp[i] = items[i];

}

return temp;

}

// performing set union operation on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>:: unions(ArraySet<ItemType>setB){

ArraySet<ItemType> setC;

for (int i=0; i<count; i++){

setC.add(items[i]);

}

for (int i=0; i< setB.count; i++){

setC.add(setB.items[i]);

}

return setC;

}

// performing set intersection operation on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>::intersection(ArraySet<ItemType>setB) {

ArraySet setC;

for (int i=0; i<count ; i++)

{

for (int j=0; j< setB.count; j++){

if(items[i] == setB.items[j]){

setC.add(items[i]);

}

}

}

return setC;

}

// perform set difference operation(A -B) on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>::difference(ArraySet<ItemType>setB){

ArraySet setC;

for(int i=0; i<count; i++)

{

int j;

for(j=0; j<setB.count; j++)

{

if(items[i] == setB.items[j])

break;

}

if (j ==setB.count)

setC.add(items[i]);

}

return setC;

}

PLEASE HELP ME to get the proper output! I have to submit this code by today.

  

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

hey i m solving your output problem . please give me a like for my effort . you did minor mistake in add() instead of items in item[] ,you add the item into temp[] , that's why the items[] show every time address of the number . now we get desire output ..

here is the code --

main.cpp

#include<stdio.h>

#include<iostream>

#include<fstream>

#include "ArraySet.h"

#include "ArraySet.cpp"

using namespace std;

int main(){

ArraySet<int> setA, setB, setC;

// adds to setA

setA.add(10);

setA.add(20);

setA.add(30);


// prints set A

setA.display();

//adds to setB

setB.add(30);

setB.add(40);

setB.add(50);

// prints set B

setB.display();

// unions of setA and setB

setC=setA.unions(setB);

setC.display();

// intersections of setA and setB

setC = setA.intersection(setB);

setC.display();

// difference of setA and setB

setC = setA.difference(setB);

setC.display();

// removes 30 from setA

setA.remove(10);

setA.display();

// clear the setC

setC.clear();

setC.display();

}

ArraySet.h

#ifndef BAG_H

#define BAG_H

template <class ItemType>

// ArraySet class

class ArraySet {

private:

ItemType* items;

int count;

int capacity;

public:

ArraySet(); // default constructure

void display( ) const;

int getCurrentSize() const;

bool isEmpty() const;

bool add(const ItemType& newEntry);

bool remove(const ItemType& anEntry);

bool remove ();

void clear();

bool contains(const ItemType& anEntry);

ItemType* toArray();

ArraySet<ItemType> unions (ArraySet<ItemType> setB);

ArraySet<ItemType> intersection (ArraySet<ItemType> setB);

ArraySet<ItemType> difference (ArraySet<ItemType> setB);

};

#endif

ArraySet.cpp

#include "ArraySet.h"

#include<iostream>

using namespace std;

template <class ItemType>

ArraySet<ItemType>::ArraySet(){

int size;
//ItemType*temp = new ItemType[capacity];

items = new ItemType[size];

count = 0;

capacity = size;

}

// dispaly Function

template<class ItemType>

void ArraySet<ItemType>::display() const

{

int i;

for(i = 0; i<count; i++)

{

cout<< items[i] << " " ;

}

cout<< endl;

}

// Function to get the current number of entries in this set

template <class ItemType>

int ArraySet<ItemType>:: getCurrentSize() const

{

return count;

}

// Check wheather this set is empty

template <class ItemType>

bool ArraySet<ItemType>::isEmpty()const {

return count == 0;

}

// Add a new entry to this set avoiding duplicates

template <class ItemType>

bool ArraySet<ItemType>::add (const ItemType& newEntry) {

for (int i =0; i<count; i++)

{

if (items[i] == newEntry)
return false;

}

if (count == capacity){

capacity = 2* capacity+2;

}

ItemType*temp = new ItemType[capacity];

for (int i =0; i<count; i++ )

{

temp[i] = items[i];

}

temp[count] = newEntry;
//you did minor mistake here , instead of adding item into items* ,you add into temp[]
//that's why item[] prints the address of i every time .
//now i made this change and now we can get the desire output .
items[count]=newEntry;

count++;

return true;

}

// Test wheather this set comtains a given entry

template<class ItemType>

bool ArraySet<ItemType>:: contains(const ItemType& anEntry) {

for (int i = 0; i <count ; i ++){

if (items[i] == anEntry){

return true;

}

return false;

}

}

template< class ItemType>

bool ArraySet<ItemType>:: remove(const ItemType& anEntry){

int i;

for(int i=0; i<count ; i++)

{

if (items[i] == anEntry) break;

}

if (i == count)

return false;

for ( int j =1; i <count-1 ; j++)

{

items[i] = items[i +1];

}

count --;

return true;

}

// checks wheather this set is empty

template< class ItemType>

bool ArraySet<ItemType>:: remove(){

if(count==0)

return true ;

ItemType anEntry=items[count-1];

remove(anEntry);

return anEntry;

}

// removes all entries from the set

template< class ItemType>

void ArraySet<ItemType>:: clear(){

delete items;

count = 0;

}

// retrieves all the entries that are in the set

template <class ItemType>

ItemType* ArraySet<ItemType>:: toArray(){

ItemType* temp= new ItemType[count];

for (int i = 0; i < count; i++)

{

temp[i] = items[i];

}

return temp;

}

// performing set union operation on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>:: unions(ArraySet<ItemType>setB){

ArraySet<ItemType> setC;

for (int i=0; i<count; i++){

setC.add(items[i]);

}

for (int i=0; i< setB.count; i++){

setC.add(setB.items[i]);

}

return setC;

}

// performing set intersection operation on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>::intersection(ArraySet<ItemType>setB) {

ArraySet setC;

for (int i=0; i<count ; i++)

{

for (int j=0; j< setB.count; j++){

if(items[i] == setB.items[j]){

setC.add(items[i]);

}

}

}

return setC;

}

// perform set difference operation(A -B) on this set(A) and a given set(B)

template <class ItemType>

ArraySet<ItemType> ArraySet<ItemType>::difference(ArraySet<ItemType>setB){

ArraySet setC;

for(int i=0; i<count; i++)

{

int j;

for(j=0; j<setB.count; j++)

{

if(items[i] == setB.items[j])

break;

}

if (j ==setB.count)

setC.add(items[i]);

}

return setC;

}

and the snapshot of the output --

hey it was helpful for you,,Please Give me a like to appreciate my effort . your like means a lot to me ... THank You !

Add a comment
Know the answer?
Add Answer to:
Define a class ArraySet using an array that represents a set and implements the ADT Set....
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
  • 5. Below i s the class deciaration for the Bag class from your text. Refer to...

    5. Below i s the class deciaration for the Bag class from your text. Refer to this hea he Ted implementation @file Bag.h #ifndet BAG #define BAG template <class ItemType> class Bag private: static const int DEFAULT BAG SIZE 6; Il current count of Bag items /I max capacity of the Bag ItemType items[DEFAULT BAG SIZE]; //array of Bag items int itemCount; int maxitems; /l Returns either the index of the element in the array items that ll contains the...

  • c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are...

    c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are given an interface: SetInterfac This is a public interface and completely specifies what the Set class operations must be. SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods. Set differs in the fact that it does not allow duplicates. This also means that add()must check that an element is not...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • 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...

  • Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag....

    Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag. Be sure to include a default constructor to initialize the private members of the class. Test that your code works properly. BagInterface includes the methods:  public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public boolean remove(T anEntry); public void clear(); public int getFrequencyOf(T anEntry); public boolean contains(T anEntry); public T[] toArray();

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / 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...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

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