CS1101C Lab 4 (Even Week)

Set Intersection

The deadline for this lab question is Friday 10 November 2006, 23:59:59 hours.

The name of your C program file must be called setintersect.c, files with any other name will not be marked.

Background

A set is a collection of objects. A set cannot contain two or more identical elements. Each element in the set is unique. Similarly, the order in which the elements of a set are listed is irrelevant. For this lab, we are interested only in sets of non-negative integers.

Look at the following set:

A = {0, 5, 17}

A is a set containing three elements: 0, 5, and 17.

Consider another set:

B = {5, 17, 0}

We say that B = A because the order of the elements in the set does not matter.

Consider a third set:

C = {5, 0, 17, 0, 5, 5, 0}

We ignore all duplicate elements in a set. Since the order of elements in the set does not matter, A = B = C.

For the purposes of this lab, when we display a set, we want to display the set with duplicate elements removed, and sorted in ascending order. That means the correct way to display the above equivalent sets is shown in set A.

Objective

The objective of this program is to read in two sets of non-negative integers (call them A and B) from a text file, and calculate the intersection of A and B (call this new set C). The intersection of two sets is the set of all elements which are members of both A and B.

It is possible to have an empty set, i.e. a set with no elements. This is denoted by {}.

The number of elements in a set is the number of unique members in a set.

Using "N" for intersect:

{1, 2, 3} N {4, 5, 6} = {}

{1, 2, 3} N {3, 4, 5} = {3}

{1, 2, 3} N {} = {}

{5, 3, 53, 64, 83, 2, 3, 9, 64, 3, 51} N {4, 1, 3, 83, 10, 6} = {3, 83}

Data File

Each data file contains data for the two sets A and B. Your program must read in the sets A and B, calculate the intersection of the two sets, and display C (C is the intersection of A and B).

The name of the data file is always sets2.txt. The following is a sample data file:

5 3 53 64 83 2 3 9 64 3 51 -1
4 1 3 83 10 6 -1

Each set contains zero or more non-negative integers. The end of a set is indicated by a negative number. You may assume that the maximum possible size of A and B is 100 elements each. What is the maximum possible size of set C? Remember to cater for empty sets.

The following is the sample output for the above sample input file. Follow it precisely else marks will be deducted.

Number of elements in A: 8
A = {2, 3, 5, 9, 51, 53, 64, 83}

Number of elements in B: 6
B = {1, 3, 4, 6, 10, 83}

C = A intersect B.

Number of elements in C: 2
C = {3, 83}

The above is only a sample data file. Your program must work for different data files.

Take Note


This document, index.html, has been accessed 17 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 7 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 10 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.