I need help display the number of matching values of two arrays, help would be very much appreciated.

The computers suppose to pick 5 random numbers and they get stored in an array. The user is also suppose to pick five numbers and to be put in an array. Then I'm suppose to find the number of matching value between the two arrays.

For example, say the computer picked (0,1,2,3,4,5,) and the user picked (0,1,7,8,9), there would be 2 matches. However, my problem is that the repeated values are suppose to be counted only once. When the user picks (0,1,1,2,3) and the computer picks (9,8,1,2,3) there are only 3 not 4 matches. The last time I compiled my code the user picked (1,2,3,4,5) and the computer picked (6,2,2,0,5), and there were three matches instead of two. Here's my code so far. Any hints or ideas will be appreciated thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 int count= 0;
	for (int i = 0;i < 5; i++)
	{
		for(int j=0; j<5;j++){
			if (computer[i] == user[j]){
				count++;
				computer[i] = -1; 
				break;
			}
			if (computer[i] == user[j]){
				count++;
				user[j] = -1; 
				break;
			}
			if (computer[i] == user[j]){
				matches++;
				break;
			}
		}
	}
Last edited on
Topic archived. No new replies allowed.