I need to create a union array of two sets, given as C-arrays. This is what I have so far. I think my if condition is correct, but the output only print out one set of that array. Can you point out what I did wrong?
#include "union_intersection.h"
#include <iostream>
using namespace std;
template <typename T>
void Union(T* left, int left_size,
T* right, int right_size,
T* result, int& result_size)
{
int i=0, j=0;
while (i< left_size && j <right_size){
if (left < right)
{
result = left;
cout<< *result<< "\t";
i++;
left++;
result_size++;
}
else if (right<left)
{
result =right;
cout<< *result << "\t";
j++;
right++;
result_size++;
}
else
{
result =left;
cout<< *result <<"\t";
i++; j++;
left++;
right++;
result_size++;
}
}
}
while
requires that both array have some elements left.Try this:
Also it looks weird that the value of the argument
result
is ignored and used as simple local variable, but I don't think this is invalid because the question only says about printing out and there are no specifications about what should be written or read to/from what is pointed at byresult
.