UMBC CMSC 202, Computer Science II, Fall 1999,

Project 4 Questions and Answers


Date: Mon, 6 Dec 1999 14:06:46

> I have interesting problem. Most all of my things work, I get no seg
> faults, etc.  This is the problem- When the sorting algorithm is called it
> changes the actual values back in main. This means when a map is made with
> a already used "key" array, it is sorted, but if the "value" array is not,
> or it has been used before, it will not match up correctly.  Should I make
> a copy of the Set array, and a copy of the value array(not pointers, but
> actual distinct copy) and pass that to sort, and use that to put stuff in
> the map objects? I think this would work, but will this be okay with
> you?(Uses extra memory, and time, but I can't think of another way.)
> 

Both Set and Map constructors should be making copies of the arrays passed
in.  This becomes the data associated with the object and is distinct from
whatever has been passed in.  You should not be making any changes to the
arrays back in main().


Date: Mon, 6 Dec 1999 14:04:39

> Should we delete the print statements from our destructors, or should we
> leave them in?
> 

Either way is fine.


Date: Mon, 6 Dec 1999 09:13:34

> I'm a bit confused with the alternate constructor for the map class. 
> Should there be a pre-condition that numElems is greater than or equal to 
> 1, or should we just write the code so that an empty Map is made for 
> numElems == 0?
> 

Yes, numElems should be >= 1.  The default constructor should be used to
create an empty Map.

> Also, are the key-value pairs supposed to be matched according to the 
> order they are in when they are passed in and then sorted?  Suppose the
> following are passed to the alternate constructor:
> 
> n = {4, 8, 8, 2, 10}
> v = {"Alan", "Betty", "Cal", Dotty"}
> numElems = 4
> 
> Is the following what should happen?
> 4 would be matched with Alan.  8 would be matched with Betty.  Since there
> is already an 8, the second 8 in n would be ignored, and 2 would be
> matched with Cal.  Finally, 10 would be matched with Dotty.  Then the
> pairs would be sorted in ascending order based on the keys.
> 

The second 8 would be matched with Cal.  Since it is a duplicate key, both
the key and its corresponding value are ignored.  2 is matched with Dotty.
10 is not used.

> Finally, what if numElems is less than the number of elements in v?  Would
> only the first numElems values (i. e. the first x values, where x is the 
> value of numElems) be assigned to keys? If there are less unique keys than
> values, should we cause the program to terminate, or would the number of
> key-value pairs be the number of unique keys?
> 

There is no way for you to verify that numElems is "correct".  We are
going to assume that it is the correct number of elements in both the
integer and string arrays.  (After eliminating duplicates, however, the
actual number of key/value pairs in the map may be less than numElems).

If the value specified as numElems is not correct, there are two
possibilities:  (1) It is smaller than the actual number of elements in
the array(s).  This is no problem; the "extra" elements are not used.
(2) It is larger than the actual arrays.  This is a problem, and will
result in out-of-bounds subscripts, which will likely crash the program.
But there is no way to detect this.  The good news is that this will not
occur when your project is graded.  The value given for numElems will be
correct.


Date: Thu, 2 Dec 1999 10:25:55

> 	In order to have the Maps sort properly, I moved the sort 
> command from the constructor in Set to the constructor in Map.  Well, 
> now the sets do not get sorted.  
> 	What I would like to do is put the sort function call into the
> Display function--but I can't becuase it takes a const.  May I change 
> Display so it is not const?
> 

No, and ideally, we don't want to do the sorting in Display(), since it
only needs to be done once.  I believe the best solution is to leave the
Set constructor alone (and let it sort), but when you write the Map
constructor, don't call the base class (Set) constructor so that Map can
do its own sort.  The sort done by Map must be different than Set, because
it must sort the integers and the strings "in parallel".


Date: Wed, 1 Dec 1999 16:27:02

> You say that we should check for duplicates in sets
> and maps. The set part is simple, as I did that is previous project, but
> how can you check that no "value" is the same for two "keys"? I assumed
> that we could assume that if we dropped all the duplicate from the keys,
> then we would not have to worry about the "values" that were pointed to by
> the duplicate keys. Do we need to make sure, for instance, that 111 and
> 222 don't both have a value "bob", even if they are different pointers to
> different blocks of memory? If we need to only keep one of the two, how
> should we decide which key/value pair to get rid of?  Please let me know,
> 

You can have duplicate values, just no duplicate keys.  So for example, it
is OK to have 111 and 222 both with a value of "bob".  But it would not be
OK to have 111 with value "bob" and 111 with value "alice".  In the case
of duplicate keys, I would say keep the first one you encounter, but
eliminate any subsequent key/value pairs that have the same key. 


Date: Wed, 1 Dec 1999 14:18:43

> In Set.C, I was removing duplicate values from the array (which messed up
> order in the array). Due to the nature of Map, can I assume that each key
> is unique, and make a change to Set.C (do not check for repeating values) 
> 
> 

No, you still need to check for duplicates (for both Set's and Map's).


Date: Tue, 23 Nov 1999 16:23:53

> Can I make a change to Set.H file, i.e. can I change the private section
> of the class to protected?
> 

Yes.


Last Modified: 6 Dec 1999 14:11:38 EST by Alan Baumgarten, abaumg1@cs.umbc.edu

Back up to Fall 1999 CMSC 202 Section Homepage