Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with a language of vour choice takes two sets as input and outpu

ID: 3727710 • Letter: W

Question

Write a program with a language of vour choice takes two sets as input and outputs the intersection, union, difference, and Cartesian product of the two sets. NOTE: 1/ Your program must have functions for each operation. 2/ Please submit the complete program to Beachboard under Lab 2 by 02/15/2018 at the beginning of class. 3/ You will demo your program during the lab. 4/ Here is a sample run: Set A: 11, 2, 3, 4} Set B: 12, 3, 4, 5} The intersection of A and B: (2, 3, 4) The union of A and B: {1, 2, 3, 4, 5} The difference of A and B: {1] The Cartesian product of A and B: ((1, 2), (1, 3), (1, 4), (1, 5), (2, 5/ Here is another sample run: Set A: 1 Set B: 12, 3, 4, 5} The intersection of A and B: { The union of A and B: {1, 2, 3, 4, 5} The difference of A and B: {1]^ The Cartesian product of A and B: ((1, 2), (1, 3), (1, 4), (1, 5)}

Explanation / Answer


#include<stdio.h>
#include<conio.h>
void Union(int set1[10],int set2[10],int m,int n);
void Intersection(int set1[10],int set2[10],int m,int n);
void CartesianProduct(int a[10],int b[10],int m,int n);
void main()
{
int a[10],b[10],m,n,i,j;
int ch;
clrscr();
printf(" Enter the number of elements in first set:");
scanf("%d",&m);
printf(" Enter the elements:");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf(" Element of First set:");
for(i=0;i<m;i++)
{
printf("%d ",a[i]);
}
printf(" Enter the number of elements in second set:");
scanf("%d",&n);
printf(" Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf(" Element of second set ");
for(i=0;i<n;i++)
{
printf("%d ",b[i]);
}
for(;;)
{
printf(" 1.Union 2.Intersection 3.CartesianProduct");
printf(" 4.exit");
printf(" Enter your choice:n");
scanf("%d",&ch);
switch(ch) {
case 1:
Union(a,b,m,n);
break;
case 2:
Intersection(a,b,m,n);
break;
case 3:
CartesianProduct(a,b,m,n);
break;
case 4:
exit(0);
}
getch();
}
}

void Union(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
printf("Element of resultant set");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
}

void Intersection(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==1)
{
c[k]=a[i];
k++;
}
}
if(k==0)
{
printf("Resultant set is null set!");
}else{
printf("Element of resultant set");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
}
}

//cartesian
void CartesianProduct(int a[10],int b[10],int m,int n)
{
printf("cartessian product=");
printf("{");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("(%d,%d)",a[i],b[j]);
printf(",");
}}
printf("}");
}

//This code does not covers difference part in it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote