Solved

Help me with some ideas

Posted on 1998-07-07
6
423 Views
Last Modified: 2008-02-01
I wrote this program about an integer set and the source code is below.  My friend challenged me to use templates e.g. ints floats and strings.  I am pasting my program below.

Could you please give any suggestions what to do 'cos i am a little confused...
And for those who think that i am trying to have my homework done.. i repeat, this is for my own good.  I only need suggestions.. please do help.

Thank you.

/***************************************************************************
***** This program will create a class called IntegerSet and each object *****
***** of class can hold integers in the range 0 through 100.             *****      
*****************************************************************************/



 #include <iostream.h>
  class IntegerSet
{
 
   private:
    int a[101];
 
   public:
    IntegerSet();
    IntegerSet(int b, int c, int d, int e, int f);
 
    void unionOfIntegerSets(IntegerSet a1, IntegerSet a2);
    void intersectionOfIntegerSets(IntegerSet a1, IntegerSet a2);
    void insertElement(int k);
    void deleteElement(int k);
    void setPrint();
    bool isEqualTo(IntegerSet a1);
    int SetElement(int i);
};


      IntegerSet::IntegerSet()
{
      int i;
      for (i = 0; i < 101; i++)
 {
    a[i]=0;
 }
}

    IntegerSet::IntegerSet(int b, int c, int d, int e, int f)
{
    int i=0;
    while(i < 101)
 {
    a[i]=0;
    i=i+1;
 }

    a[b]=1;
    a[c]=1;
    a[d]=1;
    a[e]=1;
    a[f]=1;
}

    int IntegerSet::SetElement(int i)
{
    return a[i];
}

    void IntegerSet::unionOfIntegerSets(IntegerSet a1, IntegerSet a2)
{
    int i=0;
    while(i < 101)
 {
    if (a1.SetElement(i)==1)
  {
    a[i]=1;
      }
    else
  {
    if (a2.SetElement(i)==1)
   {
    a[i]=1;
   }
  }
    i=i+1;
 }
}


    void IntegerSet::insertElement(int k)
{
    if(a[k]==0)
 {
    a[k]=1;
 }       
}


    void IntegerSet::deleteElement(int m)
{
    if(a[m]==1)
 {
    a[m]=0;
 }
}
                                                      
    void IntegerSet::intersectionOfIntegerSets(IntegerSet a1, IntegerSet a2)
{
    int i=0;
    while(i < 101)
 {
    if(a1.SetElement(i)==1 && a2.SetElement(i)==1)
  {
    a[i]=1;
  }
    i=i+1;
 }
}


    void IntegerSet::setPrint()
{
    int i=0;
    bool empty=true;

    while(i < 101)
 {
    if(a[i]==1)
  {
    cout << i << " ";
    empty=false;
  }
    i=i+1;
 }

    if(empty)
 {
    cout << "---";
 }
    cout << "\n";
}


    bool IntegerSet::isEqualTo(IntegerSet a1)
{
    int i=0;
    while(i < 101)
 {
    if(a[i] != a1.SetElement(i))
  {
    return false;
  }
    i=i+1;
 }
    return true;
}

    void main()
{
    IntegerSet a(80, 50, 100, 12, 15), b(67, 50, 15, 80, 43), c, un, intrsctn;

    cout << "a: ";
    a.setPrint();
    cout << "b: ";
    b.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "These sets are equal\n";
 }
    else
 {
    cout << "These sets are unequal\n";
 }

    cout << "Their union:\n";
    un.unionOfIntegerSets(a, b);
    un.setPrint();

    cout << "Their intersection:\n";
    intrsctn.intersectionOfIntegerSets(a, b);
    intrsctn.setPrint();

    cout << "c: ";
    c.setPrint();

    a.deleteElement(12);
    a.deleteElement(100);
    cout << "Delete 12 and 100 from a1. Now\na:";
    a.setPrint();

    a.insertElement(67);
    a.insertElement(43);
    cout << "Insert 43 and 67 into a1. Now\na:";
    a.setPrint();

    if(a.isEqualTo(b))
 {
    cout << "a equals b\n";
 }
}
0
Comment
Question by:ash2902
6 Comments
 
LVL 2

Accepted Solution

by:
Slarti earned 10 total points
ID: 1167369
1. Change the top of the class declaration to:
template <class T> class Set
{  // continue normally

2. Inside the class declaration, change all occurences of int to T.
e.g. T a[101];

3. Change all member function definitions so that they start with 'template <class T>', for example:
template <class T> Set<T>::Set()
{ // continue function normally, replacing all int's with T's ...

4. To create an object use the following:
Set<int> set1; // create a Set of int's.
Set<float> set2; // create a Set of float's.

If you want more details, add more points...

0
 

Author Comment

by:ash2902
ID: 1167370
Adjusted points to 15
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1167371
If you want to parameterize the set using templates as suggested by slarti, you should probably choose a different implementation instead of a static array of 100.  What happens if you make a set of float or char?  The array implementation doesn't quite work right.

If you want to create a custom set class (not using the STL) that can hold an unrestricted amount of any type of object and also provide functionality such as union, intersect, difference, equality, subset, size, etc... let me know and I'll be glad to help out.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:alexo
ID: 1167372
The STL comes with a set container.  Use it instead of reinventing the wheel.
0
 
LVL 2

Expert Comment

by:VEngineer
ID: 1167373
I agree with Alexo, but if you are learning C++, it never hurts to make one of your own for practice sake.
0
 

Author Comment

by:ash2902
ID: 1167374
i am still having problems...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

912 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

20 Experts available now in Live!

Get 1:1 Help Now