-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc06.c
More file actions
80 lines (78 loc) · 1.45 KB
/
c06.c
File metadata and controls
80 lines (78 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//Find Union and Intersection of two arrays
#include<stdio.h>
int unioon(int a[],int b[],int n,int m){
int c[n+m];
int k=0;
for(int i=0;i<n;i++)
{
c[k++]=a[i];
}
for (int j = 0; j < m; j++)
{
int found = 0;
for (int i = 0; i < n; i++)
{
if (b[j] == a[i])
{
found = 1;
break;
}
}
if (!found)
{
c[k++] = b[j];
}
}
printf("union:\n");
for(int i=0;i<k;i++)
{
printf("%d\t",c[i]);
}
printf("\n");
}
int intersection(int a[],int b[],int n,int m)
{
int d[n+m];
int k=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
{
if(a[i]==b[j]){
//check duplicates
int found =0;
for(int x=0;x<k;x++)
{
if(d[x]==a[i]){
found=1;
break;}
}
if(!found){
d[k++]=a[i];
}
}
}
}
printf("\n intersection:\n");
for(int i=0;i<k;i++){
printf("%d\t",d[i]);
}
}
int main(){
int n,m;
printf("enter n: ");
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter m: ");
scanf("%d",&m);
int b[m];
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
unioon(a,b,n,m);
intersection(a,b,n,m);
}