Solved

Help me with some ideas

Posted on 1998-07-07
6
421 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

746 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

9 Experts available now in Live!

Get 1:1 Help Now