-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy patharray union and intersection.cpp
More file actions
152 lines (151 loc) · 2.58 KB
/
array union and intersection.cpp
File metadata and controls
152 lines (151 loc) · 2.58 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
using namespace std;
class Set
{
int a[30];
int n;
public:
void getarr()
{
cout<<"\nEnter number of elements:";
cin>>n;
cout<<"Enter elements:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void putarr()
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
}
Set operator+(Set q);
Set operator*(Set q);
friend void arsort(int arr[], int n);
friend Set unionarr(int a[],int m,int b[],int n);
friend Set interarr(int a[],int m,int b[],int n);
};
void arsort(int arr[], int n)
{
int i, a, j;
for (i = 1; i < n; i++)
{
a = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > a)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = a;
}
}
Set interarr(int a[],int m,int b[],int n)
{
Set temp;
int i,j,k;
arsort(a,m);
arsort(b,n);
i=0;j=0;k=0;
while(k<m+n&&i<m&&j<n)
{
if(b[j]<a[i])
{
j++;
}
else if(a[i]<b[j])
{
i++;
}
else
{
temp.a[k]=a[i];
i++;
j++;
k++;
}
}
temp.n=k;
return temp;
}
Set unionarr(int a[],int m,int b[],int n)
{
Set temp;
int i,j,k,c;
arsort(a,m);
arsort(b,n);
i=0;j=0;k=0;
while(k<m+n&&i<m&&j<n)
{
if(b[j]<a[i])
{
temp.a[k]=b[j];
j++;
k++;
}
else if(a[i]<b[j])
{
temp.a[k]=a[i];
i++;
k++;
}
else
{
temp.a[k]=a[i];
i++;
j++;
k++;
}
}
if(i<m&&k<m+n)
{
for(c=i;c<m;c++)
{
temp.a[k]=a[c];
k++;
}
}
else if(j<n&&k<m+n)
{
for(c=j;c<n;c++)
{
temp.a[k]=b[c];
k++;
}
}
temp.n=k;
return temp;
}
Set Set::operator+(Set q)
{
Set temp;
temp=unionarr(a,n,q.a,q.n);
return temp;
}
Set Set::operator*(Set q)
{
Set temp;
temp=interarr(a,n,q.a,q.n);
return temp;
}
int main()
{
Set s1,s2,s3,s4;
s1.getarr();
s2.getarr();
cout<<"\narrays";
cout<<"\n";
s1.putarr();
s2.putarr();
s3=s1+s2;
s4=s1*s2;
cout<<" \n UNION 's3 = s1 + s2'\n";
s3.putarr();
cout<<" INTERSECTION 's4 = s1 * s2'\n";
s4.putarr();
return 0;
}