1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
UNO [17]
3 years ago
12

Write a program that simulates a lottery. The program should havean array of five integers named lottery, and shouldgenerate a r

andom number in the range of 0 through 9 for eachelement in the array. The user should enter five digits whichshould be stored in an integer array named user. Theprogram is to compare the corresponding elements in the two arraysand keep a count of the digits that match. For example, thefollowing shows the lottery array and the user array with samplenumbers stored in each. There are two matching digits (elements 2and 4).
lottery array: 7,4,9,1,3
user array: 4,2,9,7,3
The program should display the random numbers stored in the lotteryarray and the number of matching digits. If all the digits match,display a message proclaiming the user as a grand prize winner.

Computers and Technology
1 answer:
Fiesta28 [93]3 years ago
5 0

Answer:

Here is the C++ program that simulates lottery:

#include<iostream>  //to use input output functions

using namespace std;  //to access objects cin cout

int main(){  //start of main function

int size= 5; // size of arrays

int range = 10;  //range of random numbers from 0 to 9

int user[size];  // user array

int lottery[size];  // lottery array

for(int i=0;i<size;i++)  //iterates through the array

lottery[i]=rand()%range;  // generates random number in specified range

cout<<"Enter "<<size<<" digits: ";  //prompts user to enter 5 digits

for(int i=0;i<size;i++)  //loops through the user array

cin>>user[i];  //reads input digits from user

int match=0;  //stores number of matching digits

for(int i=0;i<size;i++)  //iterates through array to compare user and lottery arrays

if(user[i]==lottery[i])  //if digit at i-th index of user array matches to that of lottery array

match++;  //increments count of matching digits by 1

cout<<"Lottery array: ";  // display digits of lottery array

for(int i=0;i<size;i++)  //iterates through elements of lottery array

cout<<lottery[i]<<" ";  //prints elements of lottery array with a space between them

cout<<endl;  //prints a new line

cout<<"  User  array: ";  // prints digits of user array

for(int i=0;i<size;i++) //iterates through elements of user array

cout<<user[i]<<" ";  //prints elements of user array

cout<<endl;  // prints a new line

if(match==size){  //if all the digits match

cout<<"Congratulations. You are a grand prize winner! "; }//displays this message proclaiming the user as a grand prize winner

else{  // if all digits of user array do not match with lottery array digits

cout<<"Number of digits matched: "<<match<<endl; //displays the number of matched digits

cout<<"Sorry! Better luck next time!" ;    //displays this message when all digits don't match

}

}    

Explanation:

If you want to keep the size and range fixed then you can use #define before the main() function to give a constant value to size and range as:

#define size 5

#define range 10

The program uses rand() function in order to generate random numbers for lottery array in the range of 0 through 9.

Then the program declares a user array with size = 5 and prompts user to enter 5 digits to stores in user array.

Next the program matches each digit of user array to the corresponding digit of lottery array and if the two digits match then the program increments the match variable by 1 to count the matched digits. For example, lets say lottery has the following elements:

lottery: 7,4,9,1,3

Lets say user array has following elements input by user at console

user: 4,2,9,7,3

Now at first iteration the digit 7 at 0-th index of lottery array is matched to the digit 4 at 0-th index of user array. This index is specified using variable i. These means the first element of lottery array is matched with first element of user array. As these digits are not equal so they don't match and the variable i is incremented to point to the position of next digit.

Now at second iteration the digit 4 in lottery array is matched to the digit 2 of user array. As these digits are not equal so they don't match and the variable i is incremented.

at third iteration the digit 9 in lottery array is matched to the digit 9 of user array. As these digits equal so they match and the variable match is incremented to 1 in order to count the number of matches. So value of match = 1

Now at fourth iteration the digit 1 in lottery array is matched to the digit 7 of user array. As these digits are not equal so they don't match and the variable i is incremented. The value of match also remains the same i.e. 1

at fifth iteration the digit 3 in lottery array is matched to the digit 3 of user array. As these digits equal so they match and the variable match is incremented to 1 in order to count the number of matches. So value of match = 2

Next the loop ends because the value of exceeds 5 which is the size of the array.

Next if(match==size) statement has an if condition which checks if all digits match. This is checked by comparing the count of match variable to the size of the array. The value of match is 2 and value of size is 5 so this means that all the digits do not match. Hence this condition evaluates to false. So the statement in if part will not execute and program moves to the statement: cout<<"Number of digits matched: "<<match<<endl; which displays the value of match variable i.e. 2 as:

Number of digits matched: 2

followed by statement cout<<"Sorry! Better luck next time!" ;  which displays the message:

Sorry! Better luck next time!

You might be interested in
Which agricultural CTSO has programs for middle-school and high-school students, as well as college students?
Airida [17]
National FFA is the answer to your question my guy.
4 0
3 years ago
Read 2 more answers
Write the pseudocode for this flowchart
MrMuchimi

Answer:

Explanation:

class TimeToSleep() {

  main bunch of stuff (string argos) {

       int age;

       string comedy;

       bool guess;

        public bool imTheMasterMethod() {

           guess = true;

           while (guess) {

              comedy = "You forgot to put the flowchart, you meathead!";

              println(comedy);

               

                scan("%d", &age);

                if (age < 18) {

                    println("Go to sleep kiddo!");

                }

           }

        }

   }

}

       

6 0
2 years ago
To find the area, Andrea tiled a rectangle and sketched her answer. Sketch Andrea's rectangle, and find the area. Show your mult
seraphim [82]

Answer:

Area = 6.25

Explanation:

Given

Length = 2.5

Width = 2.5

See attachment for rectangle

Required

Determine the area of the rectangle

Area is calculated as:

Area = Length * Width

This gives:

Area = 2.5 * 2.5

Area = 6.25

<em>The area of Andrea's sketch is 6.25</em>

8 0
2 years ago
Which of the following statements is NOT a valid way to create a date object named birthday?
dem82 [27]

Answer:

var birthday = "12/2/1978";

Explanation:

It does not create a date object named birthday because in this statement the birthday is a string which contains 12/2/1978 a date it is not date object it is a string.All other three options are correct way to create a date object.

var birthday = new Date();

creates a date object with the current date and time.

we can also pass date string in the new Date().So var birthday = new Date("12/2/1978"); is also correct.

we can also pass year,month,day in new Date() In this order only.So option fourth is also correct.

4 0
3 years ago
Which type of operating system is usually used in personal computers?
joja [24]

Answer:

Microsoft Windows

Explanation:

7 0
3 years ago
Read 2 more answers
Other questions:
  • Software that people commonly use in the workplace to make their lives easier is called
    5·1 answer
  • You are unpacking and setting up workstations
    13·1 answer
  • Which types of scenarios would the NETWORKDAYS function help calculate? Check all that apply.
    7·1 answer
  • Concentric circles on a disk platter where data is stored;________
    8·1 answer
  • The set of rules for how computers talk to one another
    6·1 answer
  • A ________ is a question you ask about data stored in a database
    9·1 answer
  • Whay is processing?Explain with an example.​
    9·2 answers
  • Create a PHP page that contains an array of at least 20 movie titles or book titles (your choice). The HTML page should have an
    7·1 answer
  • What are examples of Table Tools options that can help edit data?
    15·2 answers
  • A _ is the acknowledgment of a potential noncomformity, or an opinion regarding an issue that is not covered by a standard
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!