Question
Write a program that simulates a lottery. The program should have array of 6 integers named winning, with a randomly generated number in the range
Write a program that simulates a lottery. The program should have array of 6 integers named winning, with a randomly generated number in the range of 1 through 9 for each element in the array. The program should ask user to enter 6 numbers and store them in another integer array named player. The program then compares the numbers in 2 arrays to find out how many numbers match. If the two numbers are on the same position in both arrays, it is a match. (This is using parallel array concept to compare two arrays.) The program output should display the winning numbers, players numbers, and how many numbers matched. For instance, if your winning numbers are 3,5,9,1,4,7 and your players numbers are 2,5, 7,1, 9,8, then you have two matches (5 and 1). 5 is on the second position and 1 is on the fourth position in both arrays. Input Validation: Do not accept players number out of range of 1-9.
To create this program, you can use the following design tips 1. Create two integer arrays with 5 elements each. 2. You can create methods such as generateWinningNumber, getPlayerNumber, getMatches, and printNumber. 3. Here is the example of how to create method generateWinningNumber; static void generateWinningNumber(int[] wList) { Random randNumber = new Random(); for (int i = 0; i < wList.Length; i++) { wList[i] = randNumber.Next(101); } } 4. Get player number and use while loop to validate user input as follows;
static void getPlayerNumber(int[] pList) { for (int i = 0; i < pList.Length; i++) { Console.Write("Enter a number 1-9:"); while (int.TryParse(Console.ReadLine(), out pList[i])== false || pList[i] < 1 || pList[i] > 9) { Console.Write("Enter a valid number 1-9:"); } C# } } To compare two arrays to find out the matches, use the following code; static int getMatches(int[] pList, int[] wList) { int match = 0; for (int i = 0; i < pList.Length; i++) { if (pList[i] == wList[i]) match++; } return match; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started