In: Computer Science
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have been stored inside the union array.
Arrays:
a[] = {5, 10, 15, 20, 25, 30, 35, 40};
b[] = {3, 10, 13, 20, 23, 28, 30, 33, 35};
What I should be getting:
union[] = {3,5,10,13,15,20,23,25,28,30,33,35,40};
My loop that I made:
void Union(int left[], int left_size, int right[], int right_size, int result[], int& result_size)
{
int* walker1;
int* walker2;
int* walker3;
int counter = 0;
walker1 = left; //first pointer points to left array
walker2 = right; //second pointer points to right array
walker3 = result; //third array points to the appended result array
while(counter < left_size || counter < right_size)
{
if(*walker1 == *walker2) //checks if the elements are the same number
{
*walker3 = *walker1; //pointer walker3 takes in element of walker1
walker1++; //if the numbers are the same, walker1
walker2++; //and walker2 increments to the next element to compare
walker3++;
}
else if(*walker1 < *walker2) //checks if element in first array is less than the second array
{
*walker3 = *walker1; //walker3 takes in element of walker1
walker1++; //walker1 will increment to the next element to compare to the second array
walker3++;
}
else if(*walker1 > *walker2)
{
*walker3 = *walker2;
walker2++;
walker3++;
}
counter++;
}
while(counter < right_size)
{
*walker3 = *walker2;
walker2++;
walker3++;
counter++;
}
result_size = counter;
cout << result_size;
}
void test_Union()
{
int* walker;
int a[] = {5, 10, 15, 20, 25, 30};
int b[] = {3, 10, 13, 20, 23, 28, 30, 33, 35};
int resultSize;
int unionArr[0];
walker = unionArr;
Union(a, LEFT_SIZE, b, RIGHT_SIZE, unionArr, resultSize);
for(int i = 0; i < resultSize; i++)
{
cout << *walker << " ";
walker++;
}
cout << endl << endl << endl;
}
Screenshot
Program
//Header files
#include <iostream>
using namespace std;
//Function to union left and right sorted array
void Union(int left[], int left_size, int right[], int right_size,
int *result, int& result_size){
int i = 0, j = 0;
while (i < left_size && j <
right_size)
{
//Check left array element less
than right array element
if (left[i] < right[j]) {
result[result_size++] = left[i++];
}
//Check right array element less
than left array element
else if (right[j] < left[i])
{
result[result_size++] = right[j++];
}
//Equal condition
else{
result[result_size++]=right[j++];
i++;
}
}
//Add remaining elements
while (i < left_size) {
result[result_size++] =
left[i++];
}
while (j < right_size) {
result[result_size++] =
right[j++];
}
}
//Test function
void test_Union(){
//For union array
int* unionArray;
//Size
int resultSize = 0;
//Test arrays
int a[] = { 5, 10, 15, 20, 25, 30,35,40 };
int b[] = { 3, 10, 13, 20, 23, 28, 30, 33, 35 };
//Allocate max size needed for result array
unionArray = new int[(sizeof(a) / sizeof(a[0]))+
(sizeof(b) / sizeof(b[0]))];
//Call function
Union(a,(sizeof(a) / sizeof(a[0])), b, (sizeof(b) /
sizeof(b[0])), unionArray, resultSize);
//Display union array
for (int i = 0; i < resultSize; i++){
cout << unionArray[i]
<< " ";
}
cout << endl << endl << endl;
}
//Main method
int main() {
test_Union();
}
--------------------------------------------------------------------
Output
3 5 10 13 15 20 23 25 28 30 33 35 40
------------------------------------------------------------------
Note:-
I changed your function to generate union array in easiest way,I assume it's okay for you.