TheAlgorithms-C-Plus-Plus/operations_on_datastructures/Union_of_2_arrays.cpp

35 lines
620 B
C++
Raw Normal View History

2017-12-24 01:30:49 +08:00
#include <iostream>
int main()
{
2019-08-21 10:10:08 +08:00
int m, n, i = 0, j = 0;
cout << "Enter size of both arrays:";
cin >> m >> n;
int a[m];
int b[n];
cout << "Enter elements of array 1:";
for (i = 0; i < m; i++)
cin >> a[i];
cout << "Enter elements of array 2:";
for (i = 0; i < n; i++)
2017-12-24 01:30:49 +08:00
cin >> b[i];
2019-08-21 10:10:08 +08:00
i = 0;
j = 0;
while ((i < m) && (j < n))
{
if (a[i] < b[j])
cout << a[i++] << " ";
else if (a[i] > b[j])
cout << b[j++] << " ";
2017-12-24 01:30:49 +08:00
else
{
cout << a[i++];
j++;
}
2019-08-21 10:10:08 +08:00
}
while (i < m)
cout << a[i++] << " ";
while (j < n)
cout << b[j++] << " ";
return 0;
2017-12-24 01:30:49 +08:00
}