Solved

class IntegerSet

Posted on 2004-04-13
2
2,196 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 500 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

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

708 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