Solved

class IntegerSet

Posted on 2004-04-13
2
294 Views
Last Modified: 2007-12-19
Need help with the missing function definitions.  Please advise.  Del

#ifndef INT_H
#define INT_H

class IntegerSet {

public:

   IntegerSet(int);
   IntegerSet(const IntegerSet &);

   IntegerSet unionOfIntegerSets(const IntegerSet &);
   IntegerSet intersectionOfIntegerSets(const IntegerSet &);

   void emptySet();
   void inputSet();
   void insertElement(int);
   void deleteElement(int);
   void setPrint() const;
   bool isEqualTo(const IntegerSet &) const;

private:

   int *set;
   int size;

   bool validEntry(int x) const
   {
      return x >= 0 && x < size;

   }

};

#endif

// int.cpp

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
   /*Need help in writing call to emptySet */ HELP!

}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   /*Need help in writing statement to allocate sufficient memory*/ HELP!

   emptySet();

   for(int i = 0; i < size; i++)
      /*Need help in writing statement to copy elements of init*/ HELP!

}

void IntegerSet::emptySet()
{
   /*Need help in writing function definition for emptySet*/ HELP!

}

void IntegerSet::inputSet()
{
   int number;

   do {
      cout << "Enter an element (-1 to end): ";
      cin >> number;

      if(validEntry(number))
        set[number] = 1;

        else if(number != -1)
        cout << "Invalid Element\n";

   } while(number != -1);
      cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
      cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
      empty = false;
      ++x;

      }

   if(empty)
      cout << setw(4) << "---";
      cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
      temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   /*Need help in writing definition for intersectionOfIntegerSets*/ HELP!

}

void IntegerSet::insertElement(int k)
{
   if(validEntry(k))
      set[k] = 1;
   else
      cout << "Invalid insert attempted!\n";

}

void IntegerSet::deleteElement()
{
   /*NEED DEFINITION WRITTEN FOR deleteElement()*/ HELP!

}

void IntegerSet::isEqualTo()
{
   /*NEED DEFINITION WRITTEN FOR isEqualTo()*/ HELP!

}

// int_driver.cpp

#include <iostream>

using namespace std;

int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet c(101);
   IntegerSet c(101);
   IntegerSet d(101);

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   /*NEED WRITTEN CALL TO unionOfIntegerSets FOR OBJECT a PASSING IT b;
     ASSIGN THE RESULT TO d*/ HELP!!

   /*NEED WRITTEN CALL TO intersectionOfIntegerSets FOR OBJECT a PASSING
     IT b; ASSIGN THE RESULT TO d*/ HELP!!

   cout << "\nUnion Of A and B is:\n";
   c.setPrint();
   cout << "Intersection of A and B is:\n";
   d.setPrint();

   if(a.isEqualTo(b))
     cout << "Set A is equal to set B\n";
   else
     cout << "Set A is not equal to set B\n";

   cout << "\nInserting 77 into set A...\n";
   a.insertElement(77);
   cout << "Set A is now:\n";
   a.setPrint();

   cout << "\nDeleting 77 from set A...\n";
   a.deleteElement(77);
   cout << "Set A is now:\n";
   a.setPrint();

   cout << endl;

   return 0;

}







0
Comment
Question by:edelossantos
2 Comments
 
LVL 10

Accepted Solution

by:
Mercantilum earned 500 total points
Comment Utility
Code is below :)

#include <iostream>
#include <iomanip>

using namespace std;

#include "int.h"

IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
}

IntegerSet::IntegerSet(const IntegerSet &init)
{
   size = init.size;
   set = new int[size]; //++

   for(int i = 0; i < size; i++)
      set[i] = init.set[i]; //++

}

void IntegerSet::emptySet()
{
    delete [] set;  //++
}

void IntegerSet::inputSet()
{
   int number;

   do {
     cout << "Enter an element (-1 to end): ";
     cin >> number;

     if(validEntry(number))
       set[number] = 1;

       else if(number != -1)
       cout << "Invalid Element\n";

   } while(number != -1);
     cout << "Entry complete\n";

}

void IntegerSet::setPrint() const
{
   int x = 1;
   bool empty = true;

   cout << '{';

   for(int u = 0; u < size; ++u)
      if(set[u]) {
     cout << setw(4) << u << (x % 10 == 0 ? "\n" : "");
     empty = false;
     ++x;

      }

   if(empty)
     cout << setw(4) << "---";
     cout << setw(4) << "}" << '\n';

}

IntegerSet IntegerSet::unionOfIntegerSets(const IntegerSet &r)
{
   IntegerSet temp(size > r.size ? size : r.size);
   temp.emptySet();

   int iterations = (size < r.size ? size : r.size);

   for(int i = 0; i < iterations; i++)
      if(set[i] == 1 || r.set[i] == 1)
     temp.set[i] = 1;

   return temp;

}

IntegerSet IntegerSet::intersectionOfIntegerSets(const IntegerSet &s)
{
   //++
   // will change 'set' to be intersection of current set and 's'
   // check for each item of set, if it is in s.
   // a duplicate set item will remain duplicated in set, [if is in s of course]
   int *newset = new int[size];
   int i,j,remain;
   for (remain=i=0 ; i<size ; i++)
   {
       for (j=0 ; j<s.size && set[i]!=s.set[j] ; j++) /* */;
       if (j < s.size) // it's in
           newset[remain++] = set[i];
   }
   // change set to newset
   delete [] set;
   set = newset;
   size = remain;
   // could be a bit cleaner by deleting set and reallocating the exact right nb of data after intersection
}

void IntegerSet::insertElement(int k)
{
   if(validEntry(k))
     set[k] = 1;
   else
     cout << "Invalid insert attempted!\n";

}

void IntegerSet::deleteElement(int k)
{
   if(validEntry(k))
     set[k] = 0;
   else
     cout << "Invalid delete attempted!\n";
}

bool IntegerSet::isEqualTo(const IntegerSet &s) const
{
   //++
   int i,equal = (size == s.size);

   if (equal)
   {
      for (i=0 ; i<size && set[i]==s.set[i] ; i++) /* */;
      if (i < size) equal = 0;
   }

   return (equal);
}

// int_1_driver.cpp



int main()
{
   IntegerSet a(101);
   IntegerSet b(101);
   IntegerSet *c;  //++
   IntegerSet *d;  //++

   cout << "Enter set A:\n";
   a.inputSet();
   cout << "\nEnter set B:\n";
   b.inputSet();

   //++ union of (a,b) in c
    c = new IntegerSet(a);
    c->unionOfIntegerSets(b);

   //++ intersection of (a,b) in d
    d = new IntegerSet(a);
    d->intersectionOfIntegerSets(b);

   cout << "\nUnion Of A and B is:\n";
   c->setPrint();
   cout << "Intersection of A and B is:\n";
   d->setPrint();

   if(a.isEqualTo(b))
     cout << "Set A is equal to set B\n";
   else
     cout << "Set A is not equal to set B\n";

   cout << "\nInserting 77 into set A...\n";
   a.insertElement(77);
   cout << "Set A is now:\n";
   a.setPrint();

   cout << "\nDeleting 77 from set A...\n";
   a.deleteElement(77);
   cout << "Set A is now:\n";
   a.setPrint();

   cout << endl;

   return 0;

}


--------------------------
int.h :
#ifndef INT_H
#define INT_H

class IntegerSet {

public:

   IntegerSet(int);
   IntegerSet(const IntegerSet &);

   IntegerSet unionOfIntegerSets(const IntegerSet &);
   IntegerSet intersectionOfIntegerSets(const IntegerSet &);

   void emptySet();
   void inputSet();
   void insertElement(int);
   void deleteElement(int);
   void setPrint() const;
   bool isEqualTo(const IntegerSet &) const;

private:

   int *set;
   int size;

   bool validEntry(int x) const
   {
     return x >= 0 && x < size;

   }

};

#endif
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now