In: Computer Science
Powerball. In this assignment you are to code a program that selects 20 random non-repeating positive numbers (the Powerball Lottery numbers), inputs three numbers from the user and checks the input with the twenty lottery numbers. The lottery numbers range from 1 to 100. The user wins if at least one input number matches the lottery numbers.
As you program your project, demonstrate to the lab instructor displaying an array passed to a function. Create a project titled Lab6_Powerball. Declare an array wins of 20 integer elements.
Define the following functions:
Hint: Array elements are assigned 0, which is outside of lottery numbers' range, to distinguish elements that have not been given lottery numbers yet.
Passing an array as a parameter and its initialization is done similar to the code in this program.
#include <iostream> using std::cout; using std::endl; using std::cin; // initializes the array by user input void fillUp(int [], int); int main( ) { const int arraySize=5; int a[arraySize]; fillUp(a, arraySize); cout << "Echoing array:\n"; for (int i = 0; i < arraySize; ++i) cout << a[i] << endl; } // fills upt the array "a" of "size" void fillUp(int b[], int size) { cout << "Enter " << size << " numbers: "; for (int i = 0; i < size; ++i) cin >> b[i]; }
Hint: Looking for the match is similar to looking for the minimum number in this program.
#include <iostream> using std::cout; using std::endl; using std::cin; int main(){ const int arraySize=5; int numbers[arraySize]; // array of numbers // entering the numbers cout << "Enter the numbers: "; for(int i=0; i < arraySize; ++i) cin >> numbers[i]; // finding the minimum int minimum=numbers[0]; // assume minimum to be the first element for (int i=1; i < arraySize; ++i) // start evaluating from second if (minimum > numbers[i]) minimum=numbers[i]; cout << "The smallest number is: " << minimum << endl; }
Hint: Use srand(), rand() and time() functions that we studied earlier to generate appropriate random numbers from 1 to 100 and fill the array. Before the selected number is entered into the array wins, call function check() to make sure that the new number is not already in the array. If it is already there, select another number.
The pseudocode for your draw() function may be as follows:
declare a variable "number of selected lottery numbers so far", initialize this variable to zero while (the_number_of_selected_elements is less than the array size) select a new random number call check() to see if this new random number is already in the array if the new random number is not in the array increment the number of selected elements add the newly selected element to the array
#include <iostream> using std::cout; using std::endl; using std::cin; // initializes the array by user input void fillUp(int [], int); int main( ) { const int arraySize=5; int a[arraySize]; fillUp(a, arraySize); cout << "Echoing array:\n"; for (int i = 0; i < arraySize; ++i) cout << a[i] << endl; } // fills upt the array "a" of "size" void fillUp(int b[], int size) { cout << "Enter " << size << " numbers: "; for (int i = 0; i < size; ++i) cin >> b[i]; }
The pseudocode your function main() should be as follows:
main(){ declare array and other variables assign(...) // fill array with 0 draw(...) // select 20 non-repeating random numbers iterate three times entry(...) // get user input use check () to compare user input against lottery numbers if won state and quit printOut(...) // outputs selected lottery numbers }
Note: A program that processes each element of the array separately (i.e. accesses all 20 elements of the array for assignment or comparison outside a loop) is inefficient and will result in a poor grade.
Note 2: For your project, you should either pass the array size (20) to the functions as a parameter or use a global constant to store it. Hard-coding the literal constant 20 in function definitions that receive the array as a parameter is poor style. It should be avoided.
Solution:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void assign(int wins[], int size);
bool check(int wins[], int size, int guess);
void draw(int wins[], int size);
int entry();
void printOut(int wins[], int size, int guesses[], int
total_guesses);
int main(){
const int ARRAY_SIZE = 20;
int wins[ARRAY_SIZE];
int guesses[3]; //numbers entered by user
int total_guesses; //total numbers entered by user
bool status = false; //status of win
/*
* assigning all zeroes to wins[]
*/
assign(wins, ARRAY_SIZE);
/*
* assingning 20 random numbers to wins[]
*/
draw(wins, ARRAY_SIZE);
for(total_guesses = 0; total_guesses < 3;
total_guesses++){
/*
* getting 3 inputs from user
*/
guesses[total_guesses] = entry(); //getting a number from
user
/*
* checking whether the number present in wins[]
*/
status = check(wins, ARRAY_SIZE, guesses[total_guesses]);
if(status){
/*
* user entered number found in wins[]. and he won.
*/
status = true;
total_guesses++;
break;
}
}
if(status){
/*
* displaying winner meaage
*/
cout << endl << "Congratulations, You won !!!" <<
endl;
}
else{
/*
* displaying unmatched message
*/
cout << endl << "Sorry, no matches found." <<
endl;
}
/*
* displaying user inputs and wins[]
*/
printOut(wins, ARRAY_SIZE, guesses, total_guesses);
return 0;
}
void assign(int wins[], int size){
/*
* assigning wins[] all zeroes
*/
for(int i = 0; i < size; i++){
wins[i] = 0;
}
}
bool check(int wins[], int size, int guess){
/*
* checking for a match in wins[]
*/
for(int i = 0; i < size; i++){
if(wins[i] == guess){
return true;
}
}
return false;
}
void draw(int wins[], int size){
/*
* Assign 20 random numbers into wins array
*/
int random_no;
bool found;
/*
* srand() method is used to set seed for the rand() function
*
* the time(0) sets the current time as the seed
*
*/
srand(time(0));
for(int i = 0; i < size; i++){
found = false;
do{
/*
* generating one random number
*/
random_no = rand();
/*
* Making the random number within the range of 1 to 100
*/
random_no %= 100;
if(random_no == 0)
random_no++;
/*
* checking whether the new number already there in the array
*/
found = check(wins, size, random_no);
/*
* Repeat the process until get a unmatched random number
*/
}while(found);
wins[i] = random_no;
}
}
int entry(){
/*
* getthing a number from user
*/
int guess;
cout << "Enter a number in between 1 and 100: ";
cin >> guess;
return guess;
}
void printOut(int wins[], int size, int guesses[], int
total_guesses){
/*
* displaying the winners array
*/
cout << "The winners array is : ";
for(int i = 0; i < size; i++){
cout << wins[i] << " ";
}
cout << endl;
/*
* displaying numbers entered
*/
cout << "Nombers you entered are: ";
for(int i = 0; i < total_guesses; i++){
cout << guesses[i] << " ";
}
cout << endl << endl;
}
Program screenshot:
Output screenshots: