Activity - Intersection of counters
-
Review set_iterate2. It shows how to construct the union of two sets.
-
Discuss how you would construct the intersection of two counters lists - for a counters node with a key that does not exist in both sets, we set its count to 0 in the intersection result.
- Copy the skeleton code intersect to your cs50/ directory after logging to the plank server:
$ cp -r /thayerfs/courses/22spring/cosc050/workspace/intersect/ .
Here counters_intersect.c creates two dummy counters lists. We would like to complete the
counters_intersect()
function to perform counters intersection. - Once done, you can compile and run the code as below:
$ mygcc -o intersect counters_intersect.c libcs50/libcs50-given.a $ ./intersect {3=2, 4=0, 5=1, 7=3, }
Hint: Make sure the counters_iterate() function. Below is a reminder on how counters_iterate() was implemented:
/**************** counters_iterate() ****************/
void
counters_iterate(counters_t *ctrs, void *arg,
void (*itemfunc)(void *arg, const int key, int count))
{
if (ctrs != NULL && itemfunc != NULL) {
// scan the counters
for (countersnode_t *node = ctrs->head; node != NULL; node = node->next) {
(*itemfunc)(arg, node->key, node->count);
}
}
}
You’ll need this for Lab 6!