CS 165 - Programming Assignment #3
Abstract Data Types
The happiest way to deal with a man is never to tell
him anything he does not need to know.
Robert A. Heinlein
Time Enough for Love
-
The Assignment:
-
Build a Simple ADT for Sets of Integers (IntSet)
-
.
-
Purpose:
-
Ensure that you can implement and use an Abstract Data Type
-
Before Starting:
-
Re-read your instructor's notes
on data abstraction.
-
Know how to compile and run
C++ projects on your system.
-
Due Date:
-
Monday, July 24, 2000
Discussion of the Assignment
Design and implement a simple ADT called IntSet for sets
of integers. Each object of class IntSet can hold
zero or more integers in the range 0 through 100. A set should be represented
internally as an array of ones and zeros. Array element a[i]
is 1 if integer i is in the set. Array element a[j]
is 0 if integer j is not in the set. The default constructor
should initialize a set to an empty set, i.e., a set whose array representation
contains all zeros.
The ADT should support the following set of operations:
-
default constructor IntSet() -- creates an empty set
-
constructor with one integer parameter -- creates a set containing just
the given integer
Example: IntSet(0) creates the set {0}
-
function isEmpty() -- returns true if the set is empty,
else false; does not change the set
-
function size() -- returns the number of elements in the
set, an integer between 0 and 100; does not change the set
-
function setPrint() -- prints set to cout
with entries surrounded by curly braces and separated by commas; returns
nothing; does not change the set
Example: if the set A has elements 1, 4, and
7, A.setPrint() prints {1, 4, 7}
-
function setUnion() -- creates a third set which is the
set-theoretic union of two existing sets.
-
function setIntersection() -- creates a third set which
is the set-theoretic intersection of two existing sets.
-
function relativeComplement() -- creates a third set which
is the set of elements that are in set A and not in set B.
-
function symmetricDifference() -- creates a third set whose
elements belong to set A or to set B but not both.
-
function isEqualTo() -- returns true if the two sets are
equal, false otherwise; does not change either set.
The class must be organized as two files, a header file, intset.h,
containing the class definition and an implementation file, intset.cpp,
containing the code for the functions of the class. Order of values
in a set is unimportant but the set should not contain duplicates.
Implementation hints
The following is a suggestion of how your intset.h could
possibly look like.
#ifndef INTSET_H
#define INTSET_H
class IntSet {
public:
IntSet() { emptySet(); } // default constructor
IntSet( int ); // alternate (overloaded) constructor
IntSet setUnion( const IntSet& );
IntSet setIntersection( const IntSet& );
bool isEmpty( void );
int size( void );
IntSet relativeComplement( const IntSet& );
IntSet symmetricDifference( const IntSet& );
void setPrint( void ) const;
bool isEqualTo( const IntSet& ) const;
// Auxiliary functions <- NOT necessary (see "Notes, comments, suggestions" below)
void emptySet( void );
void inputSet( void );
void insertElement( int );
void deleteElement( int );
private:
int set[ 101 ]; // range of 0 - 100
// Private member functions, if necessary
};
#endif
Testing your solution
Run the test program, settest1.cpp, to test
your class. Make sure that all results are correct
before submitting your solution. Feel free to write your own test programs
to ensure your solution is OK.
Submitting your solution
For this specific assignment, you must enclose the following deliverables
in a 9x12 manila envelope:
-
Turn in a printed copy of your source code files, intset.h and
intset.cc.
-
Turn in a printed record of the execution of settest.cpp
-
Submit the files intset.h and intset.cpp
on a 3.5" floppy disk.
If you have improved the class implementation
(bonus points), you should also submit the test driver program (driver.cpp,
containing a main() function)
that shows these improvements at work and a results.txt
file containing the corresponding results.
If you haven't improved the design nor written
another driver program, there is no need to submit my driver and results.
Don't forget to write your name in all the files
you submit!!
Notes, comments,
suggestions
-
You don't need to implement any of the "auxiliary functions". I
created those for my own use when I developed my solution to the problem.
The proposed driver program as well the drivers to be used by the grader
will
not expect those functions to be there.
-
If you're curious to know what they do, emptySet()
wipes out the contents of a set without destroying the object, inputSet()
allows the user to input the set elements using the keyboard (rather than
hardcoding the set's contents, as in the driver program), insertElement()
and deleteElement()
are self-explanatory.
-
You might find useful to add a private data member that keeps track of
the size of the set.
Bonus items
There are several possible improvements to the proposed solution and I
strongly reccomend you try some of them after you're sure
the original solution works according to the specifications.
Here are some suggestions:
-
Add another constructor that allows sets to be initialized with any number
of elements (up to a reasonable limit).
-
Find a better data structure to store the elements of each set. Remember
the limitations of arrays and how much they impact the original solution.
-
Overload meaningful operators so that set operations can be carried out
using a simpler, more elegant syntax.
Each significant improvement on the original design might be rewarded with
extra points, up to 15% of the assignment's grade. Needless to say, you
will not earn any bonus points if your solution does not meet all
the grading criteria for correctness, style,
and design.
Other improvements (besides the three suggested) might also earn bonus
points, subject to the same limitations stated previously (not more than
15%; no bonus points for solutions that are not 100% OK).
If you improve your solution in any way that you think might deserve
bonus points, please make sure to report these improvements in your readme.txt
file.
Back to CS 165 Home Page
Last updated: 17 July 2000 by Tim McGuire