Write a C program for UNION AND INTERSECTION of two arrays

 TASK:

Write a C-Program in which declare two int arrays, each of maximum size 100. Ask the user to enter the size of the 1st array (maximum size can be 100), then ask the user to enter the integer values (no duplicate value is allowed) of the 1st array. After this ask the user to enter the size of the 2nd array (maximum size can be 100), then ask the user to enter the integer values (no duplicate value is allowed) of the 2nd array. Assume the user enters the correct values (no need of validation). Your code should determine and display the UNION and INTERSECTION of these two arrays.

CODE:

#include<stdio.h>

int main(){

int m,n,temp;

printf("Enter the size of array one: ");

scanf("%d",&m);

printf("\nEnter the size of arry two: ");

scanf("%d",&n);

int arr1[m],arr2[n],i,j,c=0,un[m+n],d=0,inter[d];

//asking for entries of array one

printf("entries of Array1: \n");

for (i=0;i<m;i++)

{

printf("entry %d : ",i+1);

scanf("%d",&arr1[i]);

un[c]=arr1[i]; //using for merging

c++;

}

//asking for entries of array two

printf("Enter entries of Array2: \n");

for (i=0;i<n;i++)

{

printf("entry %d : ",i+1);

scanf("%d",&arr2[i]);

un[c]=arr2[i]; //merging continue

c++;

}

for(i=0;i<(m+n);i++)      

    {

        int temp;

        for(j=i+1; j<(m+n) ;j++)

        {

            if(un[i]>un[j])

            {

                temp=un[j];

                un[j]=un[i];

                un[i]=temp;

            }

            else if (un[i]==un[j])//storing the value for intersection

            {

            inter[d]=un[i];

            d++;

}

        }

    }

    //printing union

       printf("\nUnion:");

       printf("\n{");

   for(i = 0; i < (m+n); i++){

      for(j = 0; j < (m+n); j++){

         if(un[i] == un[j] && i != j)

         {

          un[i]=54321;

         break;

     }

   }

}

   for (i=0;i<n+m;i++)

   {

    if (un[i]!=54321&&i!=(m+n-1))

    printf("%d,",un[i]);

    else if (un[i]!=54321&&i==(m+n-1))

    printf("%d",un[i]);

   }


   printf("}");

   //For intersection

   printf("\nintersection: ");

   printf("\n{");

   for(i = 0; i < d; i++){

      for(j = 0; j < (d); j++){

         if(inter[i] == inter[j] && i != j)

         {

          inter[i]=54321;

         break;

     }

   }

}

   for (i=0;i<d;i++)

   {

    if (inter[i]!=54321&&i!=(d-1))

    printf("%d,",inter[i]);

    else if (inter[i]!=54321&&i==(d-1))

    printf("%d",inter[i]);

   }


   printf("}");

getchar();

return 0;

}

Expected Output:





NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

Post a Comment (0)
Previous Post Next Post