Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

class IntegerSet

Posted on 2004-04-13
2
Medium Priority
?
2,326 Views
Last Modified: 2012-08-13
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
 

Author Comment

by:edelossantos
ID: 10819719
Anyone have any ideas?
0
 
LVL 4

Accepted Solution

by:
bkfirebird earned 2000 total points
ID: 10819934
here's something to start .....
IntegerSet::IntegerSet(int s)
{
   size = s;
   set = new int[size];
   emptySet();
}

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

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

try writing some of the missing functions yourself ... they are not so difficult ... start working on emptySet
the idea of EE is to help you figure, not to give the final solution
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

886 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