Question

In C program #include<stdio.h> Write a program to simulate a pick-5 lottery game. Your program must...

In C program

#include<stdio.h>

Write a program to simulate a pick-5 lottery game.

Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array.

The program prompts the user for:

  • an integer random seed
  • five distinct integers between 1 and 9 (which are stored in another array)

The program then compares the two arrays to determine if they 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.

For Example:

Enter an integer random seed: 45
To play the Pick-5 game, enter five integers bewteen 1 and 9 (inclusive): 1 2 3 4 5

You didn't match any numbers!
The winning numbers are: 6, 3, 5, 1, 4

Enter an integer random seed: 45
To play the Pick-5 game, enter five integers bewteen 1 and 9 (inclusive): 6 3 1 2 3

You only matched 2 numbers.
Your numbers matched the Pick-5 numbers at position(s): 0, 1
The winning numbers are: 6, 3, 5, 1, 4

Enter an integer random seed: 45
To play the Pick-5 game, enter five integers bewteen 1 and 9 (inclusive): 6 3 5 1 4

Congratulations!!! You WIN!!!
The winning numbers are: 6, 3, 5, 1, 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code with comments (screenshot is also attached) :

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
   int seed;
   int random_array[5]; // this array will store random array
   int user_array[5]; // this array will store user's input array
   int index[5]; // this will store indexes at which values matches, it is initialized to -1 initally
   printf("%s","Enter an integer random seed: ");
   scanf("%d",&seed);
   srand(seed); // this is to seed with the input value of seed
   for(int i = 0;i < 5;i++)
   {
       random_array[i] = rand()%9 + 1; // this will generate random no. from 1 to 9
       index[i] = -1; // initialize index with -1
   }
   printf("%s","To play the Pick-5 game, enter five integers bewteen 1 and 9 (inclusive): ");
   for(int i = 0;i < 5;i++)
   {
       scanf("%d",&user_array[i]);
   }
   int matches = 0; // variable which tells how many numbers matches in two arrays
   int j = 0; // j is variable which is used for index array to store matched indexes
   for(int i = 0;i < 5;i++)
   {
       if(random_array[i] == user_array[i])
           {
               index[j++] = i; // when values matches then store it to jth position of index
               // and then increment j
               matches++; // also increase number of matches
           }
   }
   if(matches == 0) // if no matches
   {
       printf("%s\n","You didn't match any numbers!");
   }
   else if(matches == 5) // if all matched
   {
       printf("%s\n","Congratulations!!! You WIN!!!");
   }
   else
   {
       printf("You only matched %d numbers\n",matches);
       printf("%s","Your numbers matched the Pick-5 numbers at position(s): ");
       for(int i = 0;i < matches;i++)
           printf("%d ",index[i]); // printing values of index array which stores indices of matched numbers
       // and there are only "matches" no. of match there loop from 0 to matches
       printf("\n");
   }
   printf("%s","The winning numbers are ");
   for(int i = 0;i < 5;i++)
       printf("%d ",random_array[i]);
   printf("\n");
   return 0;
}

Note : If you have any doubts then please comment

Add a comment
Know the answer?
Add Answer to:
In C program #include<stdio.h> Write a program to simulate a pick-5 lottery game. Your program must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should...

    in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding element in the two arrays and keep a count of the digits that match. For...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...

    Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery game, the player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers). More specifically, a player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). "Distinct" means that none of the...

  • Write a C program to simulate an online lottery game. Assume that the winning numbers are...

    Write a C program to simulate an online lottery game. Assume that the winning numbers are 10 and their values are within [0, 100]. Write a C program in a way to control the winning numbers. In particular, the "cheat" is that 3 of the winning numbers in each lottery should have also been drawn in the previous one. The C program asks the user to play, and if the answer is no, the program terminates.

  • Write a C program with a filename netID_intarray.c that has 2 arrays. One is a two-dimensional...

    Write a C program with a filename netID_intarray.c that has 2 arrays. One is a two-dimensional array of strings which contains 5 words, each word can be a max of 20 characters long. The list of strings should be static in your code. The second integer array is populated by prompting the user for 10 integers to be stored in the array. Here is the simple output: $ ./run Number Work Enter integer 1 for the array: 45 Enter integer...

  • Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through...

    Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through 9 and put this in an array. Then these user numbers are compared to the random numbers the computer generated. The program will display both the computer random number and below the user selected numbers. The program will tell the user how many matches are made. If the user guesses all five you let them know they are the grand winner. Here is the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT