find matches between 2 arrays

trying to find matches between my 2 arrays and display how many matches.
int matches(int userarr[], int usersize, int arr[], int size)
{
int matches=0;
matches=0;

for (int i=0; i < 20; i++)
{
for(int j=0; j < 10; j++)
{
if(arr[i] == userarr[j])
{


}
}
}
matches++;
cout<<"You got "<<matches<<" matches!" << endl;

return 0;
}
full code if needed
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;
//functions used
void Init_Keno(int arr[], int size);
void User(int userarr[], int usersize);

void twenty_Num(int arr[], int size);
void disp_Board(int arr[], int size);
int matches(int userarr[], int usersize, int arr[], int size);

//main funtion
int main()
{
srand(time(0)); //seed the random number generator

//Print messages to welcome player to the game.
cout << "Welcome! This is a game called Keno. The Keno Board contains numbers from 1 to 80." <<endl;
cout << "You will enter 10 numbers from 1 to 80. The game will then generate 20 random numbers from 1 to 80."<<endl;
cout << "The way that you win is if the number you picked is also picked by the game. Prizes may vary depending on how many hits you get." << endl;
cout << endl;

char Yes = 'y';
char No = 'n';

// start the while loop
cout << "Do you want to play a game? Enter y for yes and n for no." << endl; //ask user to play
cin >> Yes, No;
while (Yes == 'y')
{
int KenoArray[81];
Init_Keno(KenoArray, 81); //call funtions

int user[10];
User(user, 10);

cout << endl;

twenty_Num(KenoArray, 81);//call funtions

disp_Board(KenoArray, 81);//call functions
matches(user, 10, KenoArray, 81);

cout << "Do you want to play again? Enter y for yes and n for no." << endl; //ask user to play again
cin >> Yes, No;
}




system("pause"); //freeze screen to view output
return 0; //Indicates program completed successfully
}
//funtions
void Init_Keno(int arr[], int size) //postcondition- all elements of the board are set to zero
{
//set all array elements to zero
for (int i=0; i < size ; i++)
{
arr[i]=0;
}

return;
}

void User(int userarr[], int usersize)
{
//array to store users inputs
int user[10];

int j;

for (int j=0; j < 10; j++)
{
cout << "Enter a number from 1-80" << endl; //ask user to enter 10 numbers
cin >> user[j];
}
return;
}
void twenty_Num(int arr[], int size)//Board initialized, 20 random numbers picked and stored in array
{

int j;
int count=0;
int ranNum;

count = 0;
while( count < 20) //get 20 random numbers
{
ranNum = rand() % 80 + 1;
if (arr[ranNum] == 0) //makes sure there are no duplicate numbers
{

arr[ranNum] = ranNum;
count++;
}
}

return;
}

void disp_Board(int arr[], int size) //shows the 20 numbers chosen
{



//prints out board with all numbers stored in it
int i;
for (int i=1; i <=10 ; i++)
cout << setw(5) << left << arr[i];
cout<< endl;
for (int i=11; i<=20; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=21; i<=30; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=31; i<=40; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=41; i<=50; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=51; i<=60; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=61; i<=70; i++)
cout << setw(5) << left << arr[i];
cout << endl;
for (int i=71; i<=80; i++)
cout << setw(5) << left << arr[i];
cout << endl;

return;
}
int matches(int userarr[], int usersize, int arr[], int size)
{
int matches=0;
matches=0;

for (int i=0; i < 20; i++)
{
for(int j=0; j < 10; j++)
{
if(arr[i] == userarr[j])
{


}
}
}
matches++;
cout<<"You got "<<matches<<" matches!" << endl;

return 0;
}
1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <iterator>

void display_matches( int a[], std::size_t sz_a, int b[], std::size_t sz_b )
{
    std::sort( a, a+sz_a ) ;
    std::sort( b, b+sz_b ) ;
    std::set_intersection( a, a+sz_a, b, b+sz_b,
                           std::ostream_iterator<int>( std::cout << "common elements:\n", "\n" ) ) ;
}

http://coliru.stacked-crooked.com/a/e0c907773453fd70
im sorry man but i don't understand your code.. i am on VS2012
anyone??
why don't you try studying the solution you've already been given. it will (a) solve your problem and (b) you'll learn something new out of it
googling the terms like std::sort, etc is quite simple
the above is the way to go but if you are trying to understand how to do it yourself for personal enlightenment, you have 2 main choices.

1) like bubble sort, you pick 1 guy in the first array and iterate over the WHOLE second array looking for match. If found, store and count it. This is the brute force way.

2) sort first, so you can avoid looking at the whole second array, there are numerous optimizations that make this much more efficient. This is what is being done above.

3) if you know for sure that each array only has ONE copy of any given value, you can jam them together, sort that, and iterate looking for adjacent matches. But it has that extra requirement that isnt for general data.



To see what's wrong with your code, here it is, indented to show the block structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int
matches(int userarr[], int usersize, int arr[], int size)
{
    int matches = 0;
    matches = 0;

    for (int i = 0; i < 20; i++) {
	for (int j = 0; j < 10; j++) {
	    if (arr[i] == userarr[j]) {

	    }
	}
    }
    matches++;
    cout << "You got " << matches << " matches!" << endl;

    return 0;
}

Where do you check if there is a match? Compare that to where you increment the "matches" variable.

What should happen if there are duplicates in the two arrays? If userarr is { 1, 2, 2, 3} and arr is {2, 2, 2, 4}, is the correct answer:
- 1 (because 2 is in both), or
- 2 (because both instances of 2 in userarr have matches), or
- 3 (because both instances of 2 in arr have matches) or,
- 6 (because each instance of 2 in userarr matches 3 instances in arr), or
- something else?
Topic archived. No new replies allowed.