views:

55

answers:

3

I have two arrays that both hold 5 sets of random numbers.

First I display a list of all the numbers in the first array; then I need to add to that list, any number that's not in the first array, and display each of those. To do that, I use another array to put the unique values in to display. I already have a function that displays the current array.

Here's the code where I have the problem:

//SIZE is defined in the beginning as 5.

printArray(array1);
 int i, j;
 //For each number in array1, compare each number in array2 to it.
 for(j=0; j<SIZE; j++)
 {
  for(i=0; i<SIZE; i++)
     {
    if(array2[j] != array1[i])//?
    {
     arraySum[j] = array2[j];
     std::cout << arraySum[j] << std::endl;
     break;
    }

     }
 }
 //printArray(arraySum);
+1  A: 

It sounds like you're looking for std::set_difference. Example:

std::set_difference(array2, array2 + SIZE, array1, array1 + SIZE,
    std::ostream_iterator<int>(std::cout, "\n"));

Note that array1 and array2 have to be sorted for set_difference.... you can use std::sort on the input arrays if need be.

You'll need to #include <algorithm> for std::set_difference and you'll need to #include <iterator> for std::ostream_iterator.

EDIT: If you want output similar to wheaties' post, you can use std::set_union instead of std::set_difference:

std::set_union(array1, array1 + SIZE, array2, array2 + SIZE,
    std::ostream_iterator<int>(std::cout, "\n"));
Billy ONeal
A: 

There's an algorithm for that.

myVector.insert( otherVector.begin(), otherVector.end() );
sort( myVector.begin(), myVector.end() );
myVector.erase( unique( myVector.begin(), myVector.end() ) );
for_each( myVector.begin(), myVector.end(), ostream_iterator( cout, "\n" ) );

This adds the first array's contents into the second, sorts the contents, finds all the duplicate values and moves them to the end of the vector, and finally erases all those duplicates. Note: Stole the ostream_iterator idea right off of Bill's post, although I gave him a +1.

This will save you from having to cout the first and then the second. Just do it all at once.

wheaties
That would display all numbers including those in the original array, not the numbers which were not in the original array but in the second array.
Billy ONeal
Good point, although I'm not displaying them. Time for a quick rework, although I like your solution.
wheaties
@wheaties: Rather than forcing use of vectors and modifying them in the process, why not use `std::set_union`?
Billy ONeal
He wants to create an array that contains all the unique numbers. He also wants to output only the values in that unique array. While I'm "forcing" the use of 'vector's, I find there is generally few reasons not to favor 'vector's over arrays. In the end, he's going to wind up with a modification to some entity.
wheaties
@wheaties: `set_union` does what your code above does. It returns all the values from both ranges, but removes duplicate values. And it doesn't require modification of the input ranges.
Billy ONeal
So it does. Thanks for calling me out on it ...and continuing until I saw I was going about this less than efficiently. I was under the mistaken belief that it did what `set_symmetric_difference` did.
wheaties
+1  A: 

I believe this is what you're looking for...

#include <iostream>
int main(){
int i, j, count;
int SIZE = 5;
int array1[] = {1,3,4,5,7};
int array2[] = {0,9,14,5,18};
int arraySum[5];
//For each number in array1, compare each number in array2 to it.
for(j=0; j<SIZE; j++)
{
    count = 0;
    for(i=0; i<SIZE; i++)
    {
        if(array2[j] != array1[i])
        {
        count++;        
        }
    }
    if(count >= 5){
        arraySum[j] = array2[j];
        std::cout << arraySum[j] << std::endl;
    }
}
return 0;
}

Hope this helps.

Silvae