C program To Calculate The Intersection Of Two Sets | Union Of Two Sets | Differences Between Two Sets And Symmetric Difference Of Two Sets | C Programming


This Program helps to calculate intersection of two sets, union of two sets, differences of two sets and symmetric difference of two sets using one simple program in which different variables are declared so that it can be useful to find intersection of two sets, union of two sets, differences of two sets and symmetric difference of two sets

Codes

#include<stdio.h>
int a[10],b[10],c[10],d[10],i,j,k=0,n,m,flag=0;
void unio()
{
 for(i=0;i<n;i++)
 {
 c[k]=a[i];
 k++; }
for(i=0;i<m;i++)
 {
flag=0;
 for(j=0;j<n;j++)
 {
 if(b[i]==a[j])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 {
 c[k]=b[i];
 k++;
 }
}
printf("\n Union \n");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
}
void intersection()
{
 printf("\nIntersections\n");
 for(i=0;i<n;i++)
 {
 for(j=0;j<m;j++)
 {
 if(a[i]==b[j])
 printf("%d ",a[i]);
 }
 }
}
void difference()
{
 printf("\nA-B\n");
 for(i=0;i<n;i++)
 {
 flag=0;
 for(j=0;j<m;j++)
 {
 if(a[i]==b[j])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 printf("%d ",a[i]);
 }
 printf("\n\nB-A\n");
 for(i=0;i<m;i++)
 {
 flag=0;
 for(j=0;j<n;j++)
 {
 if(b[i]==a[j])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 printf("%d ",b[i]);
 }
}
void symmetric_diff()
{
 k=0;
 for(i=0;i<n;i++)
 {
 flag=0;
 for(j=0;j<m;j++)
 {
 if(a[i]==b[j])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 {
 d[k]=a[i];
 k++;
 }
 }
 for(i=0;i<m;i++)
 {
 flag=0;
 for(j=0;j<n;j++)
 {
 if(b[i]==a[j])
 {
 flag=1;
 break;
 }
 }
 if(flag==0)
 {
 d[k]=b[i];
 k++;
 }
 }
 
 printf("\n(A-B)U(B-A)\n");
 for(i=0;i<k;i++)
 {
 printf("%d ",d[i]);
 }
}
int main()
{
 printf("Enter the size of array A\n");
 scanf("%d",&n);
 printf("Enter the element of First array A\n");
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 }
 printf("Enter the size of array B\n");
 scanf("%d",&m);
 printf("Enter the elements of array B\n");
 for(j=0;j<m;j++)
 {
 scanf("%d",&b[j]);
 }
 unio();
 printf("\n");
 intersection();
 printf("\n");
 printf("difference of set\n");
 difference();
 printf("\n");
 symmetric_diff();
 printf("\n");
 return 0;
 }

Post a Comment

1 Comments

Subscribe Us and Thanks for visiting blog.