In: Computer Science
This problem contains loops, sorting arrays, random generation,
and variable matching. Include an analysis (data requirements) and
algorithm in your reply.
Write a “lottery engine” program to simulate drawing lottery
numbers. You will draw 6 numbers, randomly selected from 1 to
22.
a) Allow the user to enter their own selection of numbers
first,
b) Then run your “lottery engine” to select the “winning numbers”.
Numbers are drawn randomly.
Use the clock as the seed value for your random function
(Hint: use “srand(clock());” ).
c) Print out the draw result as it was generated.
Example output
“
Draw unsorted: 2 – 12 – 20 – 18 – 7 – 13
“
d) Then print out the matching numbers selected by the user:
e) Also print the matching numbers to a TXT file called
“Results.txt”.
Example output in the TXT file
“
Matching numbers: 2 – 7
“
Here is the solution if you have any doubt then please write in the comment section.
Please give feedback.
Solution: I have written comments for each line for better understanding.
I am taking input and printing output with the help of two loops only because of printing purpose otherwise then can be in one loop. But the time complexity of both approaches is the same.
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main()
{
//declare variable n for storing number of input from user and i for counter
int n,i;
//read number of ticket numbers user want to input
printf("Enter number of Lottery ticket you want to buy: ");
scanf("%d",&n);
//declare array of size n
int a[n];
//read ticket numbers from user
printf("Enter your ticket numbers.......\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/*declare array of size 23 and initialize all element with 0, this array
will be used for matching*/
int winner[23]={0};
// Use seed for generating random number everytime
srand(clock());
int number;
printf("Draw unsorted:");
//set counter to 0
i=0;
//loop to generate first five numbers and print them on the screen
while(i<5)
{
//generating random number
number=rand()%22+1;
//if number is not generated already then add this
if(winner[number]!=1)
{
winner[number]=1;
printf(" %d -",number);
//incerment counter
i++;
}
}
//generate and print the last number the same as above only difference in printing
while(1)
{
number=rand()%22+1;
if(winner[number]!=1)
{
winner[number]=1;
printf(" %d\n",number);
break;
}
}
//file pointer for file handling
FILE *fptr;
// open file
fptr = fopen("Results.txt","w");
//error checking
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
/*In the below program printf is used for printing on consol and fprintf
is for printing in file */
printf("Matching numbers:");
fprintf(fptr,"Matching numbers:");
/*loop through all numbers of user inputs and will print only 1st number
that matched*/
for(i=0;i<n-1;i++)
{
//if number is in winner list then print
if(winner[a[i]])
{
printf(" %d",a[i]);
fprintf(fptr," %d",a[i]);
break;
}
}
//loop to print rest matching numbers
for(++i;i<n;i++)
{
if(winner[a[i]])
{
printf(" - %d",a[i]);
fprintf(fptr," - %d",a[i]);
}
}
printf("\n");
//close the file
fclose(fptr);
return 0;
}
Output:
Result.txt
Matching numbers: 5 - 6
If you have any type of doubts then please write in the comment section, I will feel happy to help you.
Please give feedback.
Thank You!