Take the 2-minute tour ×

I'm working on a homework assignment for Comp Sci I, and I've tried literally everything I could find on the internet and in my textbook in an attempt to count the number of matching elements in two C++ arrays. I simply can't find a way to make my code count the number of matching elements in two arrays of the same size (same value, same position). For instance, if array1 has 5, 4, 3, 2, 1, and array2 has 1, 2, 3, 4, 5, there is one matching element, the 3. How can I get my code to perform this seemingly simple task?

Couldn't find an answer anywhere. My textbook only shows how to see if the arrays match exactly without counting the number of matches, and I haven't been able to find anyone else with this exact question for C++.

Here's my function so far:

int matchCounter(int lottery[5], int user[5], int matches, int SIZE)
    int count = 0;

    for (count < SIZE; count++;)
        if (lottery[count] == user[count])

    return matches;
} // end matchCounter

SIZE is a constant that equals 5, by the way. It would seem that this should work, but every time I run the program, it displays 0 matches, even in there are some matches. If anyone could help me out, I would be forever in your debt. Seriously, there's got to be some incredibly simple thing I'm just not seeing. Thanks for you help in advance.

share|improve this question
std::set_intersection –  Mooing Duck Nov 21 '12 at 0:57
Also, the code in the question doesn't compile –  Mooing Duck Nov 21 '12 at 0:57
I'm pretty sure you haven't tried "literally everything." –  bames53 Nov 21 '12 at 1:42
you should remove the 'class' and 'homework' tags from this question. The 'class' tag refers to the programming construct, not to a class at school/university. The 'homework' tag is obsolete and in the process of being removed see:stackoverflow.com/questions/tagged/homework for more details. –  Logan Besecker Nov 21 '12 at 2:43

4 Answers 4

Your for statement is garbled. Try this:

    for (int count = 0; count < SIZE; count++)

For reason why your program always fails: count++ is put in the position of the condition in the for loop, so every time this for loop is executed, count is evaluated and considered to be the condition, and then count is incremented by 1. However, every time count starts from 0 (as in int count = 0;), so the for loop actually never gets executed, and matches remain zero.


From your source code, you're not using your return value, which is also suggested by @jimhark. Change the function signature to:

int matchCounter(int lottery[5], int user[5], int SIZE)

And call with:

matches = matchCounter(lottery, user, 5);

Otherwise, your return value of the function matchCounter is ignored, while int matches only plays the role of a local variable in the function.

Read this about why the matches doesn't work: http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html (in a similar setting)

share|improve this answer
Still doesn't work, even in the format you listed. Perhaps there is a problem somewhere else in my code. Here is a link to my source code: link –  user1840483 Nov 21 '12 at 0:41
@user1840483 Change Line 50 to matches = matchCounter(lottery, user, matches, SIZE); –  Xiao Jia Nov 21 '12 at 0:43
IT WORKED. Thank you so much. –  user1840483 Nov 21 '12 at 0:46
@user1840483 you are welcome –  Xiao Jia Nov 21 '12 at 0:50
@user1840483 If you think this is ok, please accept it as the answer by clicking the check mark on the left. Thx. :-) –  Xiao Jia Nov 21 '12 at 2:36

Since this is a homework, I will try to point the errors without giving the solution.

The form of a for loop is:

for (INIT; COND; STEP) {

which is equivalent to:

while (COND) {

The way you did, your INIT is count < SIZE, which doesn't do anything. Your "COND" is count++, which returns 0 on the first iteration. As you may know, 0 is false, in the context of a boolean. Therefore, your for loop body is not being executed at all.

Also, you should not pass matches as an argument. It should be a local variable initialized with 0.

share|improve this answer
Upvote because this is an excellent write up. –  Nik Bougalis Nov 21 '12 at 0:41

Your may not be using your return value. Change sig to:

    matches = 0;

And make sure your're calling with:

matches = matchCounter(lottery, user, 5);

If you need a running sum, use:

matches += matchCounter(lottery, user, 5);

In any case matchCounter doesn't care what running total is, so why pass it in?

Also you may need to sort the lists first.

share|improve this answer
The down vote is a little harsh. He says he's always printing 0 and doesn't show how the function is called. This was worth checking. –  jimhark Nov 21 '12 at 0:43
+1 for it's part of the case :-) –  Xiao Jia Nov 21 '12 at 0:44
@XiaoJia, thanks for that. I think whoever down voted has probably been programming so long they don't remember what it's like learning C. Almost everyone makes mistakes like this at the beginning. Good luck. –  jimhark Nov 21 '12 at 0:52
@whoeveryouare thanks for un-downvoting. –  jimhark Nov 21 '12 at 1:48
completely agree :-) –  Xiao Jia Nov 21 '12 at 2:34

Maybe this can help:

int matches=0;

for(int i=0;i<ARRAY.SIZE;i++) {
   for(int j=0;j<ARRAY.SIZE;j++) {
       if(arr1[i]==arr2[j]) {
share|improve this answer

Your Answer


