Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory)

-->

#include
#define Max 50              //define max value
//define array to get values
int S1[Max];
int S2[Max];
int S3[Max];

void Get_set_value(int S[],int n)
{
	int i;
	fflush(stdout);
	for(i=1;i<=n;i++)
	{

		scanf("%d",&S[i]);
	}
	S[0] = n;
}/*function to set values from user*/

int check_same(int S[],int x)
{
	int n,i;
	n = S[0];
	for(i=1;i<=n;i++)
	{
		if(S1[i] == x)
			return 1;
	}
	return 0;
}/*Function to check Same set values*/

void union_set()
{
  int n,i,c;
  n = S1[0];
  for(i=1;i<=n;i++)
  {
	  S3[i] = S1[i];
  }

  c = S2[0];
  for(i=1;i<=c;i++)
  {
	  if(!check_same(S1,S2[i]))
	  {
		  S3[++n] = S2[i];
	  }
  }

  S3[0] = n;
}/* Function to find union od S1 & S2*/

void Intersection(int S1[],int S2[])
{
	int i,n,j=0;
	n = S1[0];
	for(i=1;i<=n;i++)
	{
		if(check_same(S2,S1[i]))
			S3[++j] = S1[i];
	}
	S3[0] = j;
}/* Function to find intersection of S1 & S2*/

void display_value(int S[])
{
	int i,n;
	n = S3[0];
	printf("{");
	for(i=1;i<=n;i++)
	{
		printf("%d",S[i]);
	}
	printf("}");
}/*Function to display union & Intersection values*/

int main()
{
	int n;
	//call function to get values and find union & intersection values and display it
	printf("\nProgram to perform math set operation\n");
	printf("How many elements for set 1: \n");
	fflush(stdout);
	scanf("%d",&n);
	printf("Enter the elements for set 1:\n");
	Get_set_value(S1,n);

	printf("How many elements for set 2: \n");
	fflush(stdout);
	scanf("%d",&n);
	printf("Enter the elements for set 2: \n");
	Get_set_value(S2,n);

	union_set(S1,S2);
	printf("\nUnion of S1 U S2 = S3 = ");
	display_value(S3);

	Intersection(S1,S2);
    printf("\nIntersection of S1 ^ S2 = S3 = ");
    display_value(S3);

    return 0;
}

Output:

Program to perform math set operation
How many elements for set 1:
4
Enter the elements for set 1:
2
3
4
5
How many elements for set 2:
3
Enter the elements for set 2:
2
3
4

Union of S1 U S2 = S3 = {2345}
Intersection of S1 ^ S2 = S3 = {234}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.