Question

In: Computer Science

Powerball. In this assignment you are to code a program that selects 20 random non-repeating positive...

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:

  • Define function assign() that takes array wins[] as a parameter and assigns 0 to each element of the array.

    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];
      }
  • Define a predicate function check() that takes a number and the array wins[] as parameters and returns true if the number matches one of the elements in the array, or false if none of the elements do. That is, in the function, you should write the code that iterates over the array looking for the number passed as the parameter. You may assume that the number that check() is looking for is always positive.

    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;
      
      }
      
  • Define a function draw() that takes array wins as a parameter and fills it with 20 random integers whose values are from 1 to 100. The numbers should not repeat.

    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
    
       
  • Define function entry() that asks the user to enter a single number from 1 to 100 and returns this value.
  • Define function printOut() that outputs the selected numbers and user input.
    Hint: Outputting the array can be done similar to echoing it 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];
      }
      

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.

Solutions

Expert Solution

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:


Related Solutions

*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
Java code TIA I need this: Your program makes accommodations for repeating digits. For example if...
Java code TIA I need this: Your program makes accommodations for repeating digits. For example if the random numbers generated were 141 in that order. Then the user entered 271 in that order, be sure that the last one does not count a match to the first and third numbers of the random numbers. They only matched one number in this case. Now if the random numbers are 141 in that order and the user enters 113, then they did...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted...
PROGRAM DESCRIPTION: In this assignment, you are provided with working code that does the following: 1....
PROGRAM DESCRIPTION: In this assignment, you are provided with working code that does the following: 1. You input a sentence (containing no more than 50 characters). 2. The program will read the sentence and put it into an array of characters. 3. Then, it creates one thread for each character in the sentence. 4. The goal of the program is to capitalize on each letter that has an odd index. The given program actually does this but lacks the synchronization...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
C program //In this assignment, we will find the smallest positive integer that // can be...
C program //In this assignment, we will find the smallest positive integer that // can be expressed as a sum of two positive cube numbers in two distinct ways. // More specifically, we want to find the smallest n such that n == i1*i1*i1 + j1*j1*j1, // n == i2*i2*i2 + j2*j2*j2, and (i1, j1) and (i2, j2) are not the same in the sense that // not only (i1, j1) not euqal to (i2, j2), but also (i1, j1)...
Consider the program which you can find on Moodle that draws random lines. In this assignment,...
Consider the program which you can find on Moodle that draws random lines. In this assignment, you will extend that program to draw triangles and quadrilaterals. Create classes Traingle and Quadrilaterals. Declare a constructor in each class as follows: public Triangle (int x1, int y1, int x2, int y2, int x3, int y3, Color color) public Quadrilateral (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Color color) Each class of these two...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
Programming Assignment No. 5 Write a program that asks the user to enter a positive odd...
Programming Assignment No. 5 Write a program that asks the user to enter a positive odd number less than 20. If the number is 5, 11 or 15, draw a square whose width is the same as the number entered, if 3, 9, or 17, draw a box (a box is a hollowed out square) with the width the same as the number entered, otherwise just present an message saying "To be determined". Write a method for the square that...
Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT