Solved

class IntegerSet

Posted on 2004-04-13
2
301 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
ID: 10821044
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 83
C++ to C# code conversion issue 4 112
max float value 3 51
Error creating a new C++ project in ,net 20 41
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

856 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