Showing posts with label Union. Show all posts
Showing posts with label Union. Show all posts

C Program To Find Union & Intersection Of Two Array

How to write a C Program To Find Union & Intersection Of Two Array in C Programming Language ?


Solution For C Program :

/*C Program To Find Union & Intersection Of Two Array.*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char arr1[10],arr2[10],arr3[20],arr4[20],arr5[10];
/*If u want to use already initialized array then u can use code given below as comment & change according to this in code given ahead*/
//char arr1[10]={'b','a','l','j','i'},arr2[10]={'g','o','s','w','a','m','i'},arr3[20],arr4[20],arr5[10];
int i=0,m,n,j,temp,k=0,t;
clrscr();
printf("\tNOTE: Union and Intersection are defined for sets and in \n\t     set repeation of element is not done.\n\n");
printf("enter no of elements in first array:");
scanf("%d",&m);
fflush(stdin);
printf("\nenter elements of first array:\n");
for(i=0;i<m;i++)
    {
    scanf("%c",&arr1[i]);
    fflush(stdin);
    }arr1[i]='\0';
printf("enter no of elements in second array:");
scanf("%d",&n);
fflush(stdin);
printf("\nenter the elements of second array:\n");
for(i=0;i<n;i++)
    {
    scanf("%c",&arr2[i]);
    fflush(stdin);
    }arr2[i]='\0';

printf("\nEntered array is:\n\tarr1={");

for(i=0;i<m;i++)
    {
    printf("%c,",arr1[i]);
    arr3[k]=arr1[i];k++;    //merging in arr3
    }
    printf("}\n\tarr2={");
for(i=0;i<n;i++)
    {
    printf("%c,",arr2[i]);
    arr3[k]=arr2[i];k++;    //merging in arr3
    }arr3[k]='\0';
    printf("}\n");
    printf("\nMerged array: ");
puts(arr3);
/***************sorting starts here*/

printf("\nsorted array is:\n");
for(i=1;arr3[i]!='\0';i++)
    {
    for(j=1;arr3[j]!='\0';j++)
        {
        if(arr3[j-1]<arr3[j])
            {
            temp=arr3[j-1];
            arr3[j-1]=arr3[j];
            arr3[j]=temp;
            }
        }
    }
/***************sorting ends here*******/
printf("\n\tarr3={");
for(i=0;arr3[i]!='\0';i++)
    {
    printf("%c,",arr3[i]);
    }
    printf("}\n");

/*********Union starts here********/
k=1;
arr4[0]=arr3[0];
    for(j=0;arr3[j]!='\0';j++)
        {
        if(arr3[j]!=arr3[j+1])
            {
            arr4[k]=arr3[j+1];
            k++;
            }
        }
printf("\nUnion of two array: \n \narr4={");
for(i=0;arr4[i]!='\0';i++)
    {
    printf("%c,",arr4[i]);
    }
/*********Intersection starts here******/
printf("}\n\nIntersection of two array: \n\narr5={");
t=0;
for(j=0;arr3[j]!='\0';j++)
    {
    if(arr3[j]==arr3[j+1])
        {
        arr5[t]=arr3[j];
        t++;
        }
    }arr5[t]='\0';

for(i=0;arr5[i]!='\0';i++)
    {
    printf("%c,",arr5[i]);
    }printf("}");

getch();
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to REAL Union of Intervals

How to write a C Program to REAL Union of Intervals in C Programming Language ?

This C Program to REAL Union of Intervals.

Solution:

  1. #include<stdio.h>
  2. float a[65536],b[65536];
  3. void quicksort(float min,float max)
  4. {
  5.     int pivot,i,j;
  6.     float k;
  7.     if(min<max)
  8.     {
  9.         pivot=max;
  10.         i=min;
  11.         j=max;
  12.         while(i<j)
  13.         {
  14.             while(a[i]<=a[pivot] && i<max)
  15.                 i++;
  16.             while(a[j]>a[pivot])
  17.                 j--;
  18.             if(i<j)
  19.             {
  20.                 k=a[i];
  21.                 a[i]=a[j];
  22.                 a[j]=k;
  23.                 k=b[i];
  24.                 b[i]=b[j];
  25.                 b[j]=k;
  26.             }
  27.         }
  28.         k=a[pivot];
  29.         a[pivot]=a[j];
  30.         a[j]=k;
  31.         quicksort(min,j-1);
  32.         quicksort(j+1,max);
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     int n,i,j;
  39.     float end,sum=0,t;
  40.     scanf("%d",&n);
  41.     for(i=0;i<n;i++)
  42.     {
  43.         scanf("%f",&a[i]);
  44.         scanf("%f",&b[i]);
  45.     }
  46.     quicksort(0,n-1);
  47.     end=b[0];
  48.     sum=end-a[0];
  49.     for(i=1;i<n;i++)
  50.     {
  51.         if(a[i]<=end)
  52.         {
  53.             if(b[i]>=end)
  54.             {
  55.                 t=b[i]-end;
  56.                 sum=sum+t;
  57.                 end=b[i];
  58.             }
  59.         }
  60.         else
  61.         {
  62.             t=b[i]-end;
  63.             sum=sum+t;
  64.             end=b[i];
  65.         }
  66.     }
  67.     printf("%f",sum);
  68.     return 0;
  69. }

Find the union and intersection of two sets of integer store it in two array C Program

How to write a program to find the union and intersection of two sets of integer store it in two array in C Programming Language ?



Solution:
/*write a program to find the union and intersection of two sets of integer store it in two array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,b[10],k=0,inter[10],u[10];
int j,m;
clrscr();
printf("\nenter how many number :");
scanf("%d",&n);
printf("\nenter the number;");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter the number;");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
u[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i]==a[j])
break;
}
if(j==n)
{
u[k]=b[i];
k++;
}
}
m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
inter[m]=a[i];
m++;
}
}
}
printf("\nthe union is :");
for(i=0;i<k;i++)
{
printf("%d,",u[i]);
}
printf("\nthe intersection is :");

for(i=0;i<m;i++)
{
printf("%d,",inter[i]);
}

getch();
}
/*
enter how many number :4

enter the number;5
8
7
1

enter the number;9
6
5
4

the union is :5,8,7,1,9,6,4,
the intersection is :5,
 */