how to compute a value of intersection of two arrays?

I was able to write a function that prints out the intersection of two arrays. However, I am trying to obtain the value of the members of the intersection. if our final intersection is {6, 12} I should return 2 as the value because I have 2 members. My return value is 1 and I don't know what I am doing wrong.
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
int main(){
    
    int data1[] = { 3, 6, 9, 12};
    int data2[] = { 2, 4, 6, 8, 10, 12 };
    int result[] = {};
    size_t length1 = sizeof(data1)/sizeof(int);
    size_t length2 = sizeof(data2)/sizeof(int);
    size_t resultMax= 0;
    int i =0;
    int j =0;
    while (i < length1 && j < length2) {
        if (data1[i] < data2[j]) {
            i++;
        }
        else if (data2[j] < data1[i]){
            j++;
        }
        else if (data1[i] == data2[j]){
            
            result[i] = data1[i];
            cout<<"valor : "<< result[i] <<endl; // output is 6 and 12
            i++;
            j++;
       resultMax = sizeof(result[i])/sizeof(int);
        }
    }


    cout << "Final Size: "<< resultMax; //output is 1
    return resultMax;
}
Last edited on
Any help?
Replace line 24 to:
 
resultMax += sizeof(result[i])/sizeof(int);


Hopefully this is the result you expect
Array result[] hasn't been given any non-zero size. Any assignment to it (e.g. line 20) is illegal - it could be going anywhere.

You could do many things, including
- making result a "satisfactorily-big" array and keeping a running tally of the number of intersections: changing line 24 to
resultMax++;
would do;
- making result a vector<int> and using result.push_back(...);
- not bothering with result[] at all; you don't use it. Just keep the running tally of the number of intersections.

You would get quite a lot of guidance just by attempting to run it in c++ shell. It would also assist people to run your program there if you put the headers in.
Last edited on
Yeah, i know it would be easier using vectors. But I want to do it without vectors. if i do result[] ={}; and doesnt assign it to zero, is that illegal?how do I need to declare my array then?
What do you mean by "You would get quite a lot of guidance just by attempting to run it in c++ shell"? What is c++ shell?


Thank you in advance.
is this what you ara taking about ? http://cpp.sh/85sy6
Yes, you have found c++ shell. For runnable code samples you can get at it from the gear-wheel icon at the top of the code sample. Go to the Options tab and turn all the warnings on before you try to run: it will tell you that you have a zero-size array.

When you declare
int result[]={};
how big do you think your array is? Its size is zero, and you cannot change that at run time.

If you want, you can declare the array result[] to be suitably large, say
int result[100];
and hope that the intersection of your other two arrays is not bigger.

The values put in result[] are wrong. If you declare result with sufficient size then you can remove line 20 and replace your original lines 22-24 by
1
2
3
4
result[resultMax]=data1[i];
resultMax++;
i++;
j++;

You cannot relate resultMax to the size of a static array as you tried to do first.

Any output that you get from this at present is more by luck than judgement. All you are doing with the line
resultMax += sizeof(result[i])/sizeof(int);
is
resultMax += 4/4
(assuming an int occupies 4 bytes) or just
resultMax += 1
Your result is a zero-size array; result[i] is, therefore, outside array bounds. Since all you are doing with this particular line is asking for the size of an integer here you may well get away with it for finding the number of elements in the intersection. However, if you do try printing out the CONTENTS of result[] after the end of the loop you will be out of luck. If result[] is a zero-size array, then line 20, result[i] = data1[i];, is VERY WRONG; it could be writing to memory just about anywhere. Even if you make result[] large enough to hold the whole intersection then this statement will still be wrong - data1[i] is not the ith element of the intersection.

If you do need to store your intersection (and there is nothing in your code at present that requires it) then you would be better using a resizable array - aka a vector<int>. I can assure you that with your code at present the array result[] does NOT store the intersection because (a) it has zero size and (b) even if you did set a larger size then you are putting the intersection elements into the wrong positions in it.
Last edited on
Check this one:

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
int main(){
    
    int data1[] = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30}; // as far as you want
    int data2[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30}; // as far as you want
    int intersection = 0;
    int intersection_arr[] = {}; // if you want to document it
    size_t length1 = sizeof(data1)/sizeof(int);
    size_t length2 = sizeof(data2)/sizeof(int);
    
    int i =0;
    int j =0;
    while (i < length1 || j < length2) {
        if (data1[i] < data2[j]) {
            i++;
        }
        else if (data2[j] < data1[i]){
            j++;
        }
        else if (data1[i] == data2[j]){
            
            cout<<"valor : "<< data1[i] <<endl;
            intersection_arr[intersection] = data[i];
    		intersection++;
    		i++;
            j++;
       }
    }

    cout << "Final Size: "<< intersection;
    return intersection;
}


The code above will work correctly with assuming data1 and data2 do not have identical member and both move increment or decrement
@MadMaul13,

intersection_arr[] is still a zero-size array. It is still wrong. You are writing beyond the ends of an array on line 22. If you are getting away with it here then it is only because no memory has been taken for other purposes immediately after.

Please put the headers in your code. Then people could immediately test it in c++ shell (via the little gear wheel icon at the top right of the code sample). You would then find also that data[i] in line 22 should be data1[i].

If you try adding 40, 50 to the data2[] array, you will find that the intersection is wrong. This is because the logic of the program is wrong (you have introduced a further error to @claudilla's code) and you are able to (and are likely to) write and read beyond the ends of arrays.
Last edited on
Topic archived. No new replies allowed.