Contact Learn C
Copy Program Share Program

Program 121:To print Union and Intersection of given Array

Program 118:
 
#include<stdio.h>
int removerepeated(int size,int a[]);
void sort(int size,int a[]);
main()
{
 int i,size1,size2,size,j=0,k,intersectionsize;
 printf("Enter size of an array1\n");
 scanf("%d",&size1);
 printf("Enter size of an array2\n");
 scanf("%d",&size2);
 int a[size1],b[size2],uni[size1+size2];
 if(size1<size2)
 {
  intersectionsize=size1;
 }
 else if(size1>size2)
 {
  intersectionsize=size2;
 }
 else
 {
  intersectionsize=size1;
 }
 
 int intersection[intersectionsize];
 
 
 printf("Enter numbers for array 1\n");
 for(i=0;i<size1;i++)
 {
  scanf("%d",&a[i]);
  
 }
  
 printf("Enter numbers for array 2\n");
 for(i=0;i<size2;i++)
 {
  scanf("%d",&b[i]);
 }
 //union start
 for(i=0;i<size1;i++)
 {
  uni[j]=a[i];
  j++;
 }
 for(i=0;i<size2;i++)
 {
  uni[j]=b[i];
  j++;
 }


 //Sorting
sort(size1+size2,uni);
//Remove repeated elements
size=removerepeated(size1+size2,uni);

 printf("Array afetr Union \n");
  for(i=0;i<size;i++)
 {
  printf("%d\n",uni[i]);
 }
 //Intersection starts
 k=0;
 for(i=0;i<size1;i++)
 {
  for(j=0;j<size2;j++)
  {
   if(a[i]==b[j])
   {
    intersection[k]=a[i];
    k++;
   }
   
  }
 }
 //Sorting
sort(k,intersection);
//Removing
 size=removerepeated(k,intersection);
 
 printf("Array after intersection\n");
 if(size>0)
 {
  for(i=0;i<size;i++)
 {
  printf("%d\n",intersection[i]);
 } 
 }
 else
 {
  printf("No intersection\n");
 }

 
}

int removerepeated(int size,int a[])
{
 int i,j,k;
 for(i=0;i<size;i++)
  {
 for(j=i+1;j<size;)
 {
    if(a[i]==a[j])
    {
     
     for(k=j;k<size;k++)
     {
      a[k]=a[k+1];
     }
         size--;
        }
    else
    {
      j++;
     } 
 }
 }
return(size);
}

void sort(int size,int a[])
{
 int i,j,temp;
   for(i=0;i<size;i++)
   {
 for(j=i+1;j<size;j++)
 {
  if(a[i]>a[j])
  {
   temp=a[i];
   a[i]=a[j];
   a[j]=temp;
  }
 }
   }
}
Explanation:

//Coming Soon...

 Output:

Print Union and Intersection of given Array














Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide