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.
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 !
Define a class ArraySet using an array that represents a set and implements the ADT Set....
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 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...
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...
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...
- 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 {...
Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...
/ 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...
Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...
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();
Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...