Write a C program for UNION AND INTERSECTION of two arrays(modified)

 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. Check the duplication values. If any duplicate value is entered, display the message “Value already entered. Duplication not allowed, Enter another value”. Have the values equal to the size of the array entered by the user. 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. Check the duplication values. If any duplicate value is entered, display the message “Value already entered. Duplication not allowed, Enter another value”. Have the values equal to the size of the array entered by the user. Your code should determine and display the UNION and INTERSECTIONS of these two arrays

CODE:

#include<stdio.h>

int main(){

int m,n,temp,o;

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];

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

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

{

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

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

}

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

{

o=0;

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

{

if (arr1[i]==arr1[j])

{

printf("%d number is repeated Enter again: ",arr1[i]);

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

o=1;

}

}

un[c]=arr1[i];

c++;

if (o==1)

{

i=0;

}

}


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

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

{

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

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

}

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

{

o=0;

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

{

if (arr2[i]==arr2[j])

{

printf("%d number is repeated Enter again: ",arr2[i]);

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

o=1;

}

}un[c]=arr2[i];

c++;

if (o==1)

i=0;

}

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])

            {

            inter[d]=un[i];

            d++;

}

        }

    }

       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<m+n;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("}");

   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