Modify the program so that it will prompt the user to enter one number at a time
ID: 658228 • Letter: M
Question
Modify the program so that it will prompt the user to enter one number at a time. Something like this:
Enter five numbers 0 through 9 for lottery numbers
Please enter number#1: 5 [enter]
Please enter Number#2: 3 [enter]
........
Please enter Number#5: 4 [enter]
*****************************************************************
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 5;
const int MAX_RANGE = 10;
void generateNumbers(int[], int);
void getUser(int[], int);
int findMatches(int[], int[], int);
void displayValues(int[], int[], int);
int main()
{
int lottery[ARRAY_SIZE]; // Lottery numbers
int user[ARRAY_SIZE]; // User's numbers
int numMatches; // Number of matches
generateNumbers(lottery, ARRAY_SIZE);
cout << "Enter five numbers 0 through 9 for lottery numbers" << endl;
getUser(user, ARRAY_SIZE);
numMatches = findMatches(lottery, user, ARRAY_SIZE);
displayValues(lottery, user, ARRAY_SIZE);
cout << "You matched " << numMatches << " numbers. ";
if (numMatches == 5)
cout << "Congratulations! You're the grand prize winner!! ";
system("pause");
return 0;
}
void generateNumbers(int lottery[], int size)
{
srand(time(NULL));
cout << "normally do not see this, added to help debug" << endl;
for (int i = 0; i < size; i++)
{
lottery[i] = 0 + rand() % MAX_RANGE;
cout << " lottery[" << i << "] = " << lottery[i] << " ";
}
cout << endl << endl;
}
void getUser(int user[], int size)
{
cin >> user[0] >> user[1] >> user[2] >> user[3] >> user[4];
}
int findMatches(int lottery[], int user[], int size)
{
int matches = 0;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (user[i] == lottery[j])
{
++matches;
break;
}
}
}
return matches;
}
void displayValues(int lottery[], int user[], int size)
{
cout << "Users numbers: ";
for (int i = 0; i < size; ++i)
cout << user[i] << " ";
cout << endl << "Lottery numbers: ";
for (int i = 0; i < size; ++i)
cout << lottery[i] << " ";
cout << endl;
}
Explanation / Answer
BY MODIFYING THE ABOVE PROGRAM:-
int findMatch( int( &LotteryNum )[9], int( &UserNum )[9] )
{
int ValidNum( 0 );
for( int j( 0 ); j < 10; j++ )
{
for( int m( 0 ); m < 10; m++ )
if( UserNum[j] == LotteryNum[m] )
ValidNum++;
}
return ValidNum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.