Set theory

Hi All,
This is how far I am with my code, but I am not sure how to write the followings-

1.(!C UNION C) INTERSECT A
2. A UNION B UNION C UNION D

Can you please help!?
------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
int printUnion(int B[], int c[], int m, int n)
{
  int i = 0, j = 0;
  while(i < m && j < n)
  {
    if(B[i] < c[j])
      cout<< B[i++];
    else if(c[j] < B[i])
      cout<<c[j++];
    else
    {
    cout<<c[j++];
      i++;
    }
  }
 
  /* Print remaining elements of the larger array */
  while(i < m)
   cout<< B[i++] <<endl;
  while(j < n)
   cout<< c[j++]<<endl;
return 0;
}

int printIntersection(int A[], int d[], int m1, int n1){
	 int i = 0, j = 0;
  while(i < m1 && j < n1)
  {
    if(A[i] < d[j])
      i++;
    else if(d[j] < A[i])
      j++;
    else /* if arr1[i] == arr2[j] */
  {
	  cout<< d[j++] <<endl;
      i++;
    }
  }
  return 0;
}
 
/* Driver program to test above function */
int main()
{
	int A[]={1,2,3,4,5,6,7,8,9,10};
	int B[]={2,4,6,8,10};
	int c[]={1,3,5,7,9};
	int d[]={1,2,3,5,7};

  int m = sizeof(B)/sizeof(B[0]);
  int n = sizeof(c)/sizeof(c[0]);
printUnion(B, c, m, n);

int m1= sizeof(A)/sizeof(A[0]);
int n1= sizeof(d)/sizeof(d[0]);
printIntersection(A,d,m1,n1);
  //getchar();
  return 0;
}
Last edited on
I don't know what the requirements are for your homework, but if really be surprised if your code didn't have to actually create an actual list of numbers when combining another two.

Though, the way to handle the first part of (1) would take a bit more thinking than I would expect in an assignment here. Did your professor give you any guidelines on how to represent a set?

For (1), what elements are in the union of a set with everything not in that set? Then what happens when you intersect that with another set?

(Remember, union is anything in either set. Intersection is only stuff found in both sets.)

Hope this helps.
2) Is it not that union is all the numbers found in your sets? So all you have to do is check whether the value is intersected in both sets so as to not have multiple elements in that set.
1) I dont quite understand this here. You see, supposing (!C union C) intersects with A, that means that ALL ELEMENTS of A are the intersection of the said sets. Because note that !C is any element that are not in C, that means (!C union C) is the UNIVERSAL set itself! :)
It's actually other part of the question where I have to find (B union C) , (A intersect D). But I can't seem to get those 2, I mentioned with this code.
can you suggest some easy way to do it?


-thanks
I can suggest new ways to store your sets, but I think you'd be better off asking your professor for help.
Topic archived. No new replies allowed.