sorted array

hello dears

I have one problem in my program . It has 2 array like

arry1:{2,2,2,3,1,0,0,0}
arry2:{3,2,2,2,1,0,0,0}

Now I want to get this array from 2 top array

arry3:{3,0,1,2,4,5,6,7}

This array is number of place's element from array 2 in array 1

Would you please give me some suggestion ? how can I solve it ? how can I write code for this part ?

Thank you so much
Regards
Last edited on
Go through the second array and look for each element in the first array.
Use a bool array to mark which elements are already taken (or replace them with an invalid value in the first array, if there is such a value).

thanks athar

I will check it im my program and if I have any question , I will ask again
thanks again

Hello

I did the thing that you said

1
2
3
4
5
6
7
8
9
10
11
12
13
int k ,i,j ;
 for (k = 0 ; k< N ; k++)
{
     for (j = 0 ; j< N ; j++)
   {
         if (arry2[k] == arry1[j])
         {    
                 arry3[k] = j ;
	 arry1[k] = N ;
	 break ;
         }
    }
}


but it gives wrong result !!
would you please check it for me that it is wrong or not ?

thank alot
regards
Last edited on
You can't really set the list items in arry1 to a null value unless you know with complete certainty that that value is not ever going to be in arry2; boolean array would be a much better option.

This is how I did it, you can simplify my code a lot, but the idea is the same.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char** argv) {

    //Both arrays you provided
    int list_a[8] = { 2, 2, 2, 3, 1, 0, 0, 0 };
    int list_b[8] = { 3, 2, 2, 2, 1, 0, 0, 0 };
    
    //Position in array
    int pos = 0;
    
    //Vectors of arrays you provided
    vector<int> a( list_a, list_a + sizeof(list_a)/sizeof(int) );
    vector<int> b( list_b, list_b + sizeof(list_b)/sizeof(int) );

    //Boolean Array Athar suggested
    vector<bool> temp( false, list_b + sizeof(list_b)/sizeof(int) );
    
    //Empty third array
    vector<int> c;
    
    
    while( !b.empty() )
    {
        //Reset the position to the front of array A each iteration
        pos = 0;
        
        //Increases the position if the values aren't the same, OR if that position has been used before
        while( ( a[pos] != b.front() ) || ( temp[pos] == true ) )
            pos++;
        
        b.erase( b.begin() );
        
        c.push_back( pos );
        
        temp[pos] = true;
                
    }
    
    //Print out third array
    for( uint i = 0; i < c.size(); i ++)
        cout << c[i] << "   ";
    
    return 0;
}
Topic archived. No new replies allowed.