Question

Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

  1. Lottery Game (15 Numbers).

Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions)

Functions you need to implement:

  • Define function initialize() that takes array lotterNumbers[] 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 the way I showed you in class.

  • Define a search function check()that takes a number and the array lotterNumbers[] as parameters and returns true if the number matches one of the elements in the array, or false if none of the 15 elements 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.

  • Define a function generate()that takes the array lotterNumbers as a parameter and fills it with 15 random integers whose values are from 1 to 999. The numbers should not repeat (essential).

Hint: Use srand(), rand() and time() functions that we studied earlier to generate appropriate random numbers from 1 to 999 and fill the array. Before the selected number is entered into the array lotterNumbers, call function check() to make sure that the new number is not already in the array. If it is already there, ignore it and select another number.

The pseudocode for your generate() function may be as follows:

1.declare a variable "number of selected lottery numbers so far", and

2.initialize this variable to zero

3.while (the_number_of_selected_elements is less than the array size)

    3.1. select a new random number

3.2. call check() to see if this random number is already in the array

3.3. if the new random number is not in the array

   3.3.1. increment the number of selected elements

   3.3.2. add the newly selected element to the array

  • Define function input()that asks the user to enter a single number from 1 to 999 and returns this value.
  • Define function printOut()that outputs the selected numbers and user input.

The pseudocode your function main() should be as follows:

main(){

   declare array and other variables

   initialize (...) // fill array with 0

   generate(...)       // select 15 non-repeating random numbers

   input (...)      // get user input

   use check () to compare user input against lottery numbers

   and output whether the user won

   printOut(...)   // outputs selected lottery numbers

}

Note: A program that processes each element of the array separately (i.e. accesses all 15 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 (15) to the functions as a parameter or use a global constant to store it. Hard-coding the literal constant 15 in function definitions that receive the array as a parameter is a poor style. It should be avoided.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using namespace std;

#define size 15

void initialize(int*);

void generate(int *);

int check(int *,int);//to check duplicate lotterNumber

int input();

bool check1(int *,int);

void printOut(int *,int);

int main()

{

int lotterNumbers[15],num;

bool decision;

initialize(lotterNumbers);

cout<<"\nInitially displaying the lotterNumbers:\n";

for(int i=0;i<size;i++)

{

cout<<" "<<lotterNumbers[i];

}

generate(lotterNumbers);

cout<<"\n\nDisplaying the lottersNumbers:\n";

for(int i=0;i<size;i++)

{

cout<<" "<<lotterNumbers[i];

}

num=input();

decision=check1(lotterNumbers, num);

if(decision==true)

{

cout<<"\nYou have won!!!";

printOut(lotterNumbers,num);

}

else

cout<<"\nYou lost!!";

return 0;

}

void initialize(int *p)

{

int i=0;

for(i=0;i<size;i++)

{

*p=0;

*p++;

}

}

  

void generate(int *p)

{

int n,c,flag;

c=1;

int *p1=p;

do

{

n=rand()%1000 + 1;

flag= check(p1,n);

if(flag==1)

{

*p=n;

*p++;

c++;

}

else

continue;

}while(c<=size);

}

int check(int *p,int n)

{

int i,flag=1;

for(i=0;i<size;i++)

{

if(*p==n)

{

flag=0;

break;

}

*p++;

}

if(flag==1)

return 1;

else

return 0;

}

  

int input()

{

int num;

cout<<"\n\nEnter your lotter Number:=";

cin>>num;

return num;

}

bool check1(int *p,int n)

{

int i,flag=0;

for(i=0;i<size;i++)

{

if(*p==n)

{

flag=1;

}

*p++;

}

if(flag==1)

return true;

else

return false;

}

void printOut(int *p,int n)

{

int i;

for(i=0;i<size;i++)

{

if(*p==n)

{

cout<<"\nThe lottery number which matches your number is: "<<*p;

}

*p++;

}

}

Add a comment
Know the answer?
Add Answer to:
Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...
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
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
ADVERTISEMENT