Labwork 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy