Matching array values

What is the best way to go through the 2 arrays to see what numbers match what. I understand that I can use a series of if/else if statements but that is getting kind of long. If there another more efficient way to go through the arrays so that if yourNumbers[3] = computerNumbers[3] wins but if yourNumbers[0] = computerNumbers[4] that is not a winner?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int megaMillions()
{
	cout << "\t\t\tMEGA - MILLIONS\n";
	cout << "\t\t\t  INSTRUCTIONS\n\n";
	cout << "The computer will randomly generate 5 sets of numbers between 1-75. Then it\n"
		<< "will generate a powerball number between 1-15. The prizes are based on the set\n"
		<< "of numbers you match. The prizes are the following for the Mega-Millions.\n\n"
		<< "1 set - $1\n2 sets - $500\n3 sets - $100,000\n4 sets - $1,000,000\n5 sets - $5,000,000\n5 sets plus powerball - Jackpot\n";

	int yourNumbers[6] = { 0 };
	int computerNumbers[6] = { 0 };
	for (int i = 0; i < 5; ++i)
	{
		computerNumbers[i] = (rand() % 75) + 1;
	}

	computerNumbers[5] = (rand() % 15) + 1;

	cout << "\nEnter five sets of numbers 1-75, then 1 set between 1-15.\n";
	for (int i = 0; i < 5; ++i)
	{
		cout << "Number chose: ";
		cin >> yourNumbers[i];
		if (yourNumbers[i] > 75 || yourNumbers[i] < 1)
		{
			while (yourNumbers[i] > 75 || yourNumbers[i] < 1)
			{
				cout << "Invalid number entered: ";
				cin >> yourNumbers[i];
			}
		}
	}
	cout << "Now enter a number 1-15 for the powerball number: ";
	cin >> yourNumbers[5];
	if (yourNumbers[5] > 15 || yourNumbers[5] < 1)
	{
		while (yourNumbers[5] > 15 || yourNumbers[5] < 1)
		{
			cout << "Invalid number entered: ";
			cin >> yourNumbers[5];
		}
	}
	
	cout << "\nYour numbers are: " << yourNumbers[0] << " " << yourNumbers[1] << " " << yourNumbers[2]
		<< " " << yourNumbers[3] << " " << yourNumbers[4] << "  " << yourNumbers[5] << endl;
	cout << "\nThe computers numbers are: " << computerNumbers[0] << " " << computerNumbers[1] << " " << computerNumbers[2]
		<< " " << computerNumbers[3] << " " << computerNumbers[4] << "  " << computerNumbers[5] << endl;

	return 0;
}
Last edited on
Explain what do you want. Do you want to count all matches, or only those matches where both value and position match?
every match, so if my numbers are: 45 75 21 34 67 12
and the computers numbers are: 45 65 34 34 75 3

only the 1st number and the 4th number are matches because the 3rd number in my numbers does not match the 3rd number in the computers numbers. So basically order does matter.

This websites shows in picture form what I'm trying to do
http://www.wilottery.com/lottogames/megamillionsinfo.aspx

here's a list of just a few. There are quite a bit of different scenarios

1
2
3
4
5
6
7
8
9
10
11
1) yourNumbers[0] = computerNumbers[0]
2) yourNumbers[1] = computerNumbers[1]
3) yourNumbers[2] = computerNumbers[2]
4) yourNumbers[3] = computerNumbers[3]
5) yourNumbers[4] = computerNumbers[4]
6) yourNumbers[5] = computerNumbers[5]
7) yourNumbers[0] = computerNumbers[0], yourNumbers[1] = computerNumbers[1]
8) yourNumbers[0] = computerNumbers[0], yourNumbers[2] = computerNumbers[2]
9) yourNumbers[0] = computerNumbers[0], yourNumbers[3] = computerNumbers[3]
10) yourNumbers[0] = computerNumbers[0], yourNumbers[4] = computerNumbers[4]
11) yourNumbers[0] = computerNumbers[0], yourNumbers[5] = computerNumbers[5]


Last edited on
See the standard library algorithm std::equal()

http://www.cplusplus.com/reference/algorithm/equal/

Use:

 
bool res = std::equal(std::begin(yourNumbers), std::end(yourNumbers), std::begin(computerNumbers));
Last edited on
Wouldnt that just output true if all the numbers are equal and false for anything else? So if they still matches 4/6 numbers it would output false?
Use loop.
Something like
1
2
3
4
5
int count = 0;
for(int i = 0; i < arraysize; ++i) {
    if(yourNumbers[i] == computerNumbers[i])
        ++count;
}
Ahhh overthought something so simple. Thank you
Sorry, read through the question quite quickly. My mistake.
Last edited on
Topic archived. No new replies allowed.