我正在尝试检查包含5个值的两个数组的每个值,以查看是否有任何匹配。
例如,{3,5,2,6,8}的随机数组和用户生成的{3,2,2,5,9}数组。在这种情况下,将有两个匹配。
该程序的目标是检查随机数组并将其与用户生成的数组进行比较并返回匹配数。
问题:我能够生成一个随机数组,但我仍然试图检查任何匹配的数字并在主函数中输出该数字
到目前为止,这是我的代码:
#include <iostream>
#include <ctime> //for time() function
using namespace std;
void generateNumbers(int arrLotto[], int arrSize) {
srand(static_cast<unsigned int>(time(0)));
for (int i = 0; i < arrSize; i++) {
int rnum = (rand() % (10));
arrLotto[i] = rnum;
cout << arrLotto[i] << " ";
}
}
int findMatches(const int arrLotto[], const int arrUser[], int arrSize)
{
int matchCount = 0;
for (int i = 0; i < arrSize; i++) {
if (arrLotto[i] == arrUser[i]) {
matchCount++;
}
return matchCount;
}
}
int main() {
int rnum;
int arrLotto[5];
int arrUser[5];
int arrSize = 5;
int matchCount = findMatches(arrLotto, arrUser, arrSize);
//prompt user for lotto numbers
cout << "Enter your 5 lottery number picks (0-9)\n" << endl;
for (int i = 0; i < 5; i++) {
cout << "Number " << i+1 << ": ";
cin >> arrUser[i];
}
//display Lotto numbers
cout << "\nLottery Numbers" << endl;
generateNumbers(arrLotto, arrSize);
//display array user numbers
cout << "\nYour Numbers" << endl;
for (int i = 0; i < 5; i++) {
cout << arrUser[i] << " ";
}
cout << endl;
//display matches
cout << "\nYou matched " << matchCount << " numbers" << endl;
if(matchCount == 5)
cout << "You are a grand winner" << endl;
return EXIT_SUCCESS;
}