0% found this document useful (0 votes)
30 views4 pages

Labwork 1

The document contains code for finding the union, intersection and difference between two arrays in C programming language. It includes code snippets to take input for two arrays, check for elements that are common between the arrays or elements that are only present in the first array but not the second.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Labwork 1

The document contains code for finding the union, intersection and difference between two arrays in C programming language. It includes code snippets to take input for two arrays, check for elements that are common between the arrays or elements that are only present in the first array but not the second.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Solution
For union and intersection

#include<stdio.h>
int main()
{
int a[100],b[100];
int n1,i,j,n2,c,s;
printf("Enter first array elements number\n\t");
scanf("%d",&n1);
printf("Enter %d elemets of first array\n",n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter second array elements number\n\t");
scanf("%d",&n2);
printf("Enter %d elemets of second array\n",n1);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);

printf("\n intersection between a and b: ");


for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
printf("%d",a[i]);
}
}

printf("\n union of a and b: ");


i=n1;
s=n1+n2;
for(i=n1,j=0;i<s;i++,j++)
{
a[i]=b[j];
}

for(i=0;i<s;i++)
printf("%d",a[i]);

}
For difference

#include<stdio.h>

#define max 100

int ifexists(int z[], int u, int v)

int i;

if (u==0) return 0;

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

if (z[i]==v) return (1);

return (0);

int main()

int p[max], q[max], r[max];


int m,n;

int i,j,k;

printf("Enter length of first array:");

scanf("%d",&m);

printf("Enter %d elements of first array\n",m);

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

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

printf("\nEnter length of second array:");

scanf("%d",&n);

printf("Enter %d elements of second array\n",n);

for(i=0;i<n;i++ ) scanf("%d",&q[i]);

k=0;

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

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

if (p[i]==q[j])

{ break;

if(j==n)

if(!ifexists(r,k,p[i]))

r[k]=p[i];

k++;

}
printf("\nThe difference of the two array is:\n");

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

printf("%d\n",r[i]);

You might also like