Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help me with some ideas

Posted on 1998-07-07
6
Medium Priority
?
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Accepted Solution

by:
Slarti earned 20 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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

618 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