CS533 Program 1 (Due October 11 10:00am)

The purpose of this assignment is to give you some practice with C++. You will implement and test a class defining sets of objects. Your class should support the following operations: Your set class should be a template class so that you can create a set of int or a set of char or a set of float, etc.

You should support printing of sets. A set should print in the format {0 1 2 3}

If you like, you can also try supporting input of sets in this same format.

When your implementation is complete, it should be possible for me to write a program such as the following:

#include "set.h"
#include <iostream.h>

main()
{
  set<int> a;
  set<int> b;   // sets should be initially empty

  a.insert(3);
  a.insert(5);
  a.insert(7);
  a.insert(5);  // should contain only the elements {3,5,7}

  b.insert(4);
  b.insert(5);
  b.insert(6);

  cout << "a = " << a << endl;
  cout << "b = " << b << endl;
  cout << "a intersect b = " << a.intersect(b) << endl;
  cout << "a union b = " << a.setunion(b) << endl;
  cout << "a difference b = " << a.difference(b) << endl;
  cout << "size of a is " << a.size() << endl;
  cout << "size of b is " << b.size() << endl;
}

In working the assignments for this course, I suggest that you create a directory structure something like the following:

533
   lib  (put Tim Budd's library routines here)
   p1   (put your files for program 1 here)
   p2   (put your files for program 2 here)
   p3   (etc.)

You should download all of the Budd library. You can download the following gzipped tar file. You should save this in the lib directory listed above using a name such as lib.tar.gz. To install these files, you then issue the commands:

     gunzip lib.tar.gz
     tar xvf lib.tar
The gunzip will decompress the file lib.tar.gz and convert it to a file named lib.tar. The tar command will extract the individual files from the tar file. You may then delete the lib.tar file if you wish. Then you should issue the command
     make all
which should compile the .C files from the library. This will complete installation of the library.

To work this assignment, you will want to use the list class. Your file set.h should contain the line

#include "../lib/list.h"
to include the list class. This will also include all of the code for iterators.

You could make your set class a subclass of list. There is no particular advantage or disadvantage of doing this for this assignment. So you may just want to represent the elements in the set using a list. Your algorithms for intersection, union, and difference should run in time proportional to the product of the sizes of the two sets. (Faster algorithms are possible, such as methods based on hash tables or methods that keep the sets in sorted order. You do not need to implement such complex algorithms.) Many of the member functions of the list class will be useful in this assignment, so make sure you understand them and use them if possible rather than duplicating their functions with your own routines. Also, please be sure to use iterators rather than indexing to access the elements of a list. Iterators are much more efficient.

You should turn in a source listing of the following:

Your code should be well-documented and your variable names well-chosen.