Solved

Help me with some ideas

Posted on 1998-07-07
6
425 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 122
C++ standard library based binary archive format 6 98
How to convert MFC::CString to UTF8 wchar_t* 10 200
Arduino EDI - Programming language 3 90
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…
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. …
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.
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.

813 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