Hi, I need to write a code that compares two arrays (draw and entry) and see if there are any matching numbers in them. I have tried a few different things and I don't understand why this doesn't work. Here is my code

Hi, I need to write a code that compares two arrays (draw and entry) and see if there are any matching numbers in them. I have tried a few different things and I don't understand why this doesn't work. Here is my code

Expand|Select|Wrap|Line Numbers

int matches (int draw[], int entry[])

{

int noofmatches = 0;

for(int i=0;i<MAXSIZE;i++)

{

for(int j=0;j<MAXSIZE;j++)

{

if(draw[i]==entry[j])

noofmatches++;

}

}

return noofmatches;

}

Any idea's about what is wrong with it?
Thanks

Nothing, it work fine. It counts the number of elements in draw that are also in entry. What were you expecting?

BTW, this is not exactly safe to do. You have to be absolutely sure that you are passing an arrays that has only MAXSIZE or more elements in it.

If you are happy with just MAXSIZE elements in them, try using the following signature:

It could be your test input.
If either the first or second batch of numbers contains duplicate elements, your noOfMatches is going to be higher than you expect.
If that doesn't help, try to be define "isn't working" more specifically.

EDIT: Oops, Adrian spotted the more serious problem. :) But you still have an issue with non-unique elements.

I'm not quite sure what you mean by element but I am putting 6 numbers into each array. So six into entry[] and six into draw[].

The 6 numbers make up the 6 elements of each array.
The problem with your input-reading code is here:

Expand|Select|Wrap|Line Numbers

for (int i=0; i<MAXSIZE; i++)

{

cin >> draw[MAXSIZE];

}

What you want to do is store one number in each element (from 0 to MAXSIZE - 1) of your array.
What you're actually doing is storing each input into draw[MAXSIZE], so each successive input overwrites the previous one, and they are all written into memory outside of your array.
Replace cin >> draw[MAXSIZE] with cin >> draw[i].