filtering array?

I want to enter a value each time to the originator[] and then delete every time the items in array[]which exist in the originator[]
example: originator[1]={0},array[counter1]={0,1,2} then filtered value should equal{1,2}
example1/originator[2]={0,2},array[counter1]={0,1,2,3} then filtered value should equal{1,3} and so on..
any one can help plz??
int filtered[];
int i2=0;
int originator[6];
originator[i2]=value;//value it varies from time to time {0,1,2,4..}
cout<<"originator has"<<originator[i2]<<endl;
// cout<<"originator has"<<originator[i2-1]<<endl;
i2++;

for(int ii=0;ii<counter1;ii++){//counter1 gives how many item in the array[] defined global ..
for(int i1=0;i1<i2;i1++){
cout<<"orig has"<<originator[i1]<<endl;//
if(array[ii]!=originator[i1])
{
filtered=array[ii];//I think the error is here
...}//if
}//2nd for
}// 1st for
Last edited on
If your arrays are already sorted, you can simply use std::set_difference.
If not, you can sort them using std::sort and then use std::set_difference.

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
#include <algorithm>
#include <iostream>

int main()
{
    const unsigned arr1_size = 5;
    const unsigned arr2_size = 3;
          unsigned arr3_size = 0;

    int array_1[10] = { 1, 0, 3, 4, 2 };
    int array_2[10] = { 2, 0, 3 };
    int array_3[10] = { 0 };

    std::sort(array_1, array_1 + arr1_size);
    std::sort(array_2, array_2 + arr2_size);

    arr3_size = std::set_difference(
        array_1, array_1 + arr1_size,
        array_2, array_2 + arr2_size,
        array_3) - array_3;

    for (unsigned i = 0; i < arr3_size; ++i)
        std::cout << array_3[i] << ' ';

    std::cout << std::endl;
}

Useful links:

http://cplusplus.com/reference/algorithm/set_difference/
http://cplusplus.com/reference/algorithm/sort/
Last edited on
It is also simple enough to do yourself (though much less efficient unless you first sort your arrays as m4ster r0shi did:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int filter( int* array, int array_size, int* originator, int originator_size, int* filtered )
  {
  // IMPORTANT: the 'filtered' array MUST BE at LEAST as big as 'array'
  // Returns the number of elements placed in the 'filtered' array
  int filtered_count = 0;

  int* originator_end = originator + originator+size;

  for (int aidx = 0; aidx < array_size; aidx++)
    {
    if (find( originator, originator_end, array[ aidx ] ) == originator_end)
      {
      filtered[ filtered_count ] = array[ aidx ];
      filtered_count += 1;
      }
    }

  return filtered_count;
  }

To use it, you must provide enough room for each item:

1
2
3
4
5
6
7
8
9
10
int array[ 10 ] = { 0, 1, 2, 3, 4 };
int array_size = 5;

int originator[ 10 ] = { 2, 3 };
int originator_size = 2;

int filtered[ 10 ];
int filtered_size;

filtered_size = filter( array, array_size, originator, originator_size, filtered );

The find() function is a standard algorithm (#include <algorithm>).
http://www.cplusplus.com/reference/algorithm/find/

Also, your variable names stink. (Sorry!)

The array you are filtering (what you call 'array') should have a name that describes its contents, like 'test_scores' or 'miles' or the like.

The array you are filtering with (what you call 'originator') is technically itself the filter.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.