In mathematics, a set is a collection of distinct elements and elements in a set are not in order. Here are some examples of sets:
(a) A set of integers, e.g. integerSet = {3, 1, 4, 2}
(b) A set of fruits, e.g. fruitSet = {apple, orange, papaya}
(c) A set of characters, e.g. charSet = {A, m, &}
We always enclose elements of sets inside a pair of { }.
Here are some other properties on sets:
(1) A set can be empty, i.e. no element. We call it empty set. In mathematics, we have a special symbol to denote empty set. Convenient to our design later, we will use { } to denote an empty set.
(2) When checking an element is inside a set. We call it belong to.
(3) If any element in a set is also an element in another set, we call it a subset. For example, {1, 2, 3} is a subset of {2, 3, 4, 6, 1}. Therefore empty set is a subset of every set.
(4) The cardinal number of a set is the number of elements in a set.
(5) The union of two sets A and B are all the elements belong to A and B, minus the duplications. For example, if A = {1, 2, 3} and B = {2, 3, 4, 5}, the union is {1, 2, 3, 4, 5}.
(6) The intersection of two sets A and B are the common elements of A and B. Using the example quoted in (5), the intersection is {2, 3}.
(7) The equality of two sets A and B are all the elements of A are in B and all the elements of B are in A. Or alternatively, A is a subset of B and vice versa.
(8) The difference of two sets A and B, denoted as A B, is the set of elements that is in A but not in B. For example, if A = {1, 2, 3} and B = {2, 3, 4, 5}. The differences are A B = {1} and B A = {4, 5}.
(9) The complement of a set A is all elements that belong to the universal set minus those elements in A.
We have all the required properties for our task.
In this assignment, our universal set (implicitly) is the 12 zodiac signs: They are
aquarius, pisces, aries, taurus,
gemini, cancer, leo, virgo, libra,
scorpio, sagittarius, capricorn
Each zodiac sign is an enumeration constant. Therefore all the sets formed in this assignment are sets of zodiac signs.
However, you should assume that all sets constructed are arrays of void* and all operations done on sets should bypass by these arrays and you type cast an element of the set in order to access to one of the above zodiac signs.
All storages used for the arrays should be dynamically created; and delete them when they are no longer needed.
When accessing an element of the array, you should access it via a pointer, i.e. by dereferencing this pointer. Using the notation, for example set [k] or *(set + k) accessing to the k th element of the set is not allowed.
Your program can start with the following declarations:
const int MAX = 12; // 12 signs
const int MAXSTR = 15; // length for each zodiac sign
typedef void* VoidPtr;
enum ZodiacSign = {aquarius, pisces, aries, taurus, gemini, cancer,
leo, virgo, libra, scorpio, sagittarius, capricorn};
Data validation is not necessary in this design, when I want to add in a sign to your system, you can assume that this is a valid zodiac sign; i.e. we have restricted ourselves in a set or a subset of the above 12 zodiac signs.
The best way to test your design is to develop an educational system to teach some basic set theory. All the sets used in your design should be randomly generated by the system i.e. the sizes (or cardinal number) and the elements.
The following function is extremely useful to construct and to return a set; and you MUST have this function in your design:
void constructSet (VoidPtr *, int size);
For example, when this function is called with
constructSet (s, 4);
The function returns through the array, a set of 4 random generated signs, for example
{virgo, gemini, acquarius, leo}
If you pass in the size = 0, nothing is constructed, by default, it is an empty set.
Note that the elements in a set are distinct and the elements are stored in random order.
We propose the following interactions for your system: see image.
You can see this assignment is another menu driven system which is similar to Assignment 1. This menu system is repeated and you always return back to the main menu after the end of an option.
Let us try to explore each of the options: see image.
When you enter option 0, the system randomly generates a set of some zodiac signs. Note that the set can be empty. You will see the empty set notation later. Option 0 is another menu system, i.e. a sub-menu. This sub-menu is repeated and when you exit from this sub-menu, you will return back to the main menu.
They are three choices in this submenu, to add, to check (or in set terminology, belong to) and to get the number of elements in set (or in set terminology, the cardinality). Let us explore all the choices for this sub-menu: see image.
As can be seen in the above interactions of choice 1; if a sign is not inside the set, it is added; if it is inside the set, the set remains unchanged.
The set generated above (including the updates) will be used to test choices 2 and 3. The following shows some of possible interactions and displays for choices 2 and 3. They are relatively simple: see image.
Note that for all sets constructed or generated in this assignment, their storages should be dynamically created. When an operation (or an option) is ended, you should help the compiler to do some garbage collection. For example, when you enter choice 9 in the following sub-menu: see image.
Let us now explore other options in the main menu: see image.
In option 1, the system randomly generates two sets and displays the union of these two sets; and doing the garbage collection when exiting from this option.
The same is done for option 2, but evaluate the intersection of the two sets: see image.
In the above screen shot, you see the notation of an empty set. Let us see another screen shot for some non-empty intersections: see image.
Now, option 3, the complement of a set is done with the universal set. Our universal set is the set of zodiac signs. The following shows some of the interactions and displays: see image.
Option 4 is the subset operations. The following shows some of the possible subsets interactions and displays: see image.
Option 5 is the set equality. The following shows some of the possible interactions and display: see image.
The final option 6 is the difference of two sets. The following shows some of the possible interactions and displays: see image.