Can anyone help me with this homework program? Write a program to simulate a pic
ID: 3716761 • Letter: C
Question
Can anyone help me with this homework program?
Write a program to simulate a pick-5 lottery game. Your program should generate and store 5 distinct numbers between 1 and 9 (inclusive) into an array. The program prompts the user to enter five distinct numbers between 1 and 9 and stores the numbers into another array.
The program then compares and determines whether the two arrays are identical. If the two arrays are identical, then the user wins the game; otherwise the program outputs the number of matching digits and their position in the array.
Your program must contain a function that randomly generates the pick-5 lottery numbers.
Also, in your program, include the function sequential search to determine if a lottery number generated has already been generated.
Explanation / Answer
Hi Dear,
You have not mentioned about programming language.
So i have implemented in C++.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
//declare constants
#define MAX_PICKS 5
#define MAX_CHOICE 10
using namespace std;
//function prototypes
bool isDuplicate(int[], int, int);
int getUserNum(int);
void getWinners(int[], int);
void getChoices(int[], int);
void sortArray(int[], int);
void printWinners(int[], int[], int);
//declare main method
int main()
{
//declare local variables
const int input = 5;
int *userNum = new int[input];
int *winning = new int[input];
cout << " Pick " << input << " lottery ";
getChoices(userNum, input);
//call the getWinners method
getWinners(winning, input);
//call the sort method
sortArray(userNum, input);
sortArray(winning, input);
//call the printWinners method
printWinners(winning, userNum, input);
return 0;
}
//implement the sort Array method
void sortArray(int array[], int numValues)
{
int temp;
int pos;
for (int i = 0; i < numValues; i++)
{
temp = array[i];
pos = i;
for (int j = i; j < numValues; j++)
{
if (temp > array[j])
{
temp = array[j];
pos = j;
}
}
array[pos] = array[i];
array[i] = temp;
}
}
//call the getWinners method
void getWinners(int array[], int numValues)
{
srand(time(0));
for (int i = 0; i < numValues; i++)
{
int tempNum = (rand() % 10) + 1;
while (isDuplicate(array, numValues, tempNum))
{
tempNum = (rand() % 10) + 1;
}
array[i] = tempNum;
}
}
void getChoices(int array[], int numValues)
{
int tempNum = 0;
for (int i = 0; i < numValues; i++)
{
cout << "User choice " << i + 1 << ": ";
tempNum = getUserNum(MAX_CHOICE);
while (isDuplicate(array, numValues, tempNum) && i != 0) {
cout << "you have already used this number, reenter: ";
tempNum = getUserNum(MAX_CHOICE);
}
array[i] = tempNum;
}
}
/******************************************************************
Function: bool isDuplicate( int [], int, int )
Use: This function determines if a value has already been placed
into an array
Arguments: int []: the array holding all of the values
int: the number of values the array holds
int: the value to look for in the array
Returns: a boolean value that represents whether the value was or
was not found in the array. true if the value is a
duplicate. false if the value is not a duplicate
******************************************************************/
bool isDuplicate(int array[], int arraySize, int searchNum)
{
//Loop through the array to determine if it is holding searchNum
//If a match is found, return true
for (int sub = 0; sub < arraySize; sub++)
{
if (array[sub] == searchNum)
return true;
}
//At this point, no match was found in the array, so false is
//returned.
return false;
}
/******************************************************************
Function: int getUserNum( int upperBd )
Use: This function gets a number from the user that is between 1
and an upper bound
Arguments: int: the upper bound
Returns: an integer between 1 and the upper bound, inclusive
Note: This function will not display an initial prompt to the user.
******************************************************************/
int getUserNum(int upperBd)
{
int userNum;
//Get an integer number from the user
cin >> userNum;
//Validate the number that the user entered. If the //number is invalid,
//display an error message to the user and get another //number. Do this
//
while (userNum < 1 || userNum > upperBd)
{
cout << "The choice must be between 1 and " << upperBd << ", reenter: ";
cin >> userNum;
}
//Return the valid value
return userNum;
}
//implement printWinners
void printWinners(int winArray[], int userArray[], int numValues) {
cout << " The winning numbers are ";
for (int i = 0; i < numValues; i++)
cout << winArray[i] << " ";
int match = 0;
for (int i = 0; i < numValues; i++)
if (isDuplicate(userArray, numValues, winArray[i]))
match++;
cout << " You matched " << match << " number(s).";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.