CS 14 - Lab 0
CS 14
Homepage
Lab 0 - Get to know the students in your lab and brush up on your C++
The lab assignment for this lab is completely optional however I
highly suggest that you complete the lab. The assignment
is designed to give you a bit of practice with coding and classes to
get you back into the swing of programming. This is very important for
those of you that haven't programmed in a while and are a bit rusty.
I suggest you pair up with a partner and work on the lab together.
When you finish the lab, pair up with another group and test
eachothers code. Have fun!
Review for proficiency exam
I suggest you look over the study guide for the proficiency exam. If you
are not familiar with some of the things on this list, I suggest you
look through a general C++ programming book or search on the web for
more information.
Integer set lab practice
In this lab you will practice:
- Using classes to create a data type, IntegerSet, capable of storing
a set of integers
- Using dynamic memory allocation with the new and delete operators
Create a class called IntegerSet. A set is represented initially as an
array of ones and zeros. Array element a[i] is 1 if the integer i is in the
set. Array element a[j] is 0 if the integer j is not in the set. The default
constructor initializes a set to the so-called empty-set (i.e. a set whose
array representation contains all zeros).
An object of type IntegerSet is instantiated by passing to the constructor
an integer representing the range of the set. For example, a set of the
integers 0 to 99 is instantiated as follows:
IntegerSet mySet ( 100 );
An array containing the appropriate number of elements is allocated dynamically
and initialized by the constructor.
Provide member functions for the common set operations. For example, provide
a unionOfIntegerSets member function that creates a third set which is the set-
theoretic union of two existing sets (i.e. an element of the third array's set
is assigned a 1 if that element is 1 in either or both of the existing sets).
Provide an intersectionOfIntegerSets member function that creates a third set
which is the set-theoretic intersection of the two existing sets (i.e. an
element of the third set's array is set to 0 if that element is 0 in either
or both of the existing sets and an element of the third set's array is set to
1 if that element is 1 in each of the existing sets).
Provide an insertElement member function that inserts a new integer k into
a set (by setting a[k] to 1). Provide a deleteElement function that deletes
integer m (by setting a[m] to 0).
Provide a setPrint member function that prints a set as a list of numbers
seperated by spaces. Print only those elements which are present in the
set (i.e. their position in the array has a value of 1). Print --- for an
empty set.
Provide an isEqualTo member function that determines whether two sets are
equal.
Provide a copy constructor that takes as a parameter a reference to an
IntegerSet object.
Provide all of these options on a menu for the user. Continue
to let the user choose actions until they choose to exit.
Make sure to use good programming style throughut.
Make sure to provide good error checking throughout. When you pair
up with another group to test your code, they will be trying to
find those error conditions you did not check for.
Problem-Solving Tips
- Member functions unionOfIntegerSets and intersectionOfIntegerSets must
return an IntegerSet object. The object that invokes these functions and
the argument set passed to the member functions should not be modified
by the operation.
- Qualify the parameters of unionOfIntegersSet and intersectionOfIntegersSet
as const
- When deleteElement and insertElement are invoked with invalid arguments,
print an error message to the screen
Questions/Activities
- Why is it advantageous for set to be allocated dynamically?
- Add a destructor to IntegerSet that deallocates the dynamically allocated
memory
- What is the purpose of having the set member variable private?
© 2003 UC Riverside Department of Computer Science &
Engineering. All rights reserved.