Sunday, August 2, 2009

C++ programming question.?

I am having problems figuring out the steps needed to answer the following question. I would appreciate it if someone could provide me with a brief outline for the code i need in order to write the following function.








Write a predicate function





bool same_set(vector%26lt;int%26gt; a, vector%26lt;int%26gt; b)





that checks whether two vectors have the same elements in some order, ignoring multiplicities. For example, the two vectors





1 4 9 16 9 7 4 9 11


and





11 11 7 9 16 4 1





would be considered identical. You will probably need one or more helper functions.

C++ programming question.?
You could sort both vectors, eliminate duplicates, and compare element by element. However, this would be "destructive" to the vectors unless you sort copies (or use element references instead).





One alternative method might be to duplicate one of the vectors and repeatively eliminate the common elements. If any elements are left, the vectors aren't identical.





A third method to consider (no extra vector memory/copy needed here): loop through a "source" vector comparing each element (skip any source duplicates) to the entire "target" vector; for each match, subtract 1 from the determined length of the "target" vector; once the "source" vector loop has completed, the target length should be zero if they're identical vectors. This 3rd method doesn't really need any "helper functions"; it's simply one outer loop (source vector) with a couple inner loops (check for source duplicates %26amp; compare to target vector). Let me know if you need code to demonstrate this but you don't appear to be requesting C++ source.

flash cards

No comments:

Post a Comment