Helllo,

I am supposed to do the following:


Mathematically, a set is said to be consists of well-defined elements. For
instance, the following are well-defined sets.

A = {1, 2, 3, 4, 5, 6, 7}
B = {2, 3, 4, 8, 9}

A set may be described using other types of expression; specially, if the
set has elements (members) that are numerous to list. There are a well-defined
operations that can be performed on sets.

1. Determining a given set B is a subset of A, or not.

B is-a-subset-of A ==> false

2. Union of sets: finds all elements of both A and B and generates a unique
set.

A union B = {1, 2, 3, 4, 5, 6, 7, 8, 9}

The teacher is no help at all. I still have no idea how to do this. Can anybody explain this to me?

Thank you!

You can use the STL set (#include <set>).
http://www.cplusplus.com/reference/stl/set/

For union, just add all of the elements to the set and it will take care of removing duplicates.

Ok, so I just....


1. You read the elements from keyboard
2. Store them into arrays
3. Have a loop that goes through each elements and store them into the third array

That would solve the union problem. Since you put them all in one and the dublicates get deleted.
But I still don't get how I do 1. ? I did read the link, though.

You must check whether all elements present in B is present in A or not. Union is just not copying two arrays in a single array, you must check for duplicates.

Can I just do the input like this?

int main()
{
int InputArray[5];
cout << "Enter the first set of numbers: ";
cin >> InputArray[0];
cout << "Enter the second set of numbers: ";
cin >> InputArray[1];

Now I would just do the loops etc. to do the union and the other stuff.

inputarray[0] means first element of input array. That's not a set at all. Union means recurrent elements must be ignored in the second and nonrecurrent elements are added to the set.

For union, the following code must do the trick... I tried it and its right..

int k=0,t=0,z=0,i;
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
		{
			if(nat[i]==odd[j])
			t++;
		}
		if(t==0)
		{
			res[k]=nat[i];
			k++;
		}
		t=0;
	}
	for(j=0;j<5;j++)
	{
	res[k]=odd[j];
	k++;
	}
	printf("\n\nUnion of sets A and B = \t\t");
	/*for(i=0;i<k;i++)
		printf("%d ",res[i]);*/
	while(z<5)
	{
		int temp;
		if(res[z]>res[z+1])
		{
			temp=res[z];
			res[z]=res[z+1];
			res[z+1]=temp;
			z=0;
		}
		z++;
	}
	//printf("\n");
	for(i=0;i<k;i++)
	{
		printf("%d " ,res[i]);
	}
This article has been dead for over six months. Start a new discussion instead.