Question

In: Computer Science

This problem contains loops, sorting arrays, random generation, and variable matching. Include an analysis (data requirements)...

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

Solutions

Expert Solution

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!


Related Solutions

using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
4. Consider the random variable Z from problem 1, and the random variable X from problem...
4. Consider the random variable Z from problem 1, and the random variable X from problem 2. Also let f(X,Z)represent the joint probability distribution of X and Z.  f is defined as follows: f(1,-2) = 1/6 f(2,-2) = 2/15 f(3,-2) = 0 f(4,-2) = 0 f(5,-2) = 0 f(6,-2) = 0 f(1,3) = 0 f(2,3) = 1/30 f(3,3) = 1/6 f(4,3) = 0 f(5,3) = 0 f(6,3) = 0 f(1,5) = 0 f(2,5) = 0 f(3,5) = 0 f(4,5) = 1/6...
Probability Mass Functions, Random Variables Find a table of the Binomial random variable (include a picture...
Probability Mass Functions, Random Variables Find a table of the Binomial random variable (include a picture of the table in your submission) and obtain the probability that in 20 independent trials, each of which has probability of success equal to 0.1, the number of successes is less than or equal to 3. Repeat the problem using instead of a Binomial a Poisson with suitable parameter lambda.
In this problem there are two random variables X and Y. The random variable Y counts...
In this problem there are two random variables X and Y. The random variable Y counts how many times we roll the die in the following experiment: First, we flip a fair coin. If it comes Heads we set X= 1 and roll a fair die until we get a six. If it comes Tails, we set X= 0 and roll the die until we get an even number (2, 4 or 6). a). What are the possible values taken...
(1 point) The problem below describes an experiment and defines a random variable. For this problem:...
(1 point) The problem below describes an experiment and defines a random variable. For this problem: (a) Find the distribution of the random variable (and provide it as a chart). (b) Calculate the expected value of the random variable. Roll two fair, six-sided dice. Let X be the (absolute value of the) difference between the numbers they will land on. a. The distribution of the random variable (enter possible values in numerical order): xx P(x)P(x) b. The expected value of...
You will be performing an analysis on a dataset that contains data on fertility and life...
You will be performing an analysis on a dataset that contains data on fertility and life expectancy for 198 different countries. All data is from the year 2013. The fertility numbers are the average number of children per woman in each of the countries. The life expectancy numbers are the average life expectancy in each of the countries. You will be turning in a paper that should include section headings, graphics and tables when appropriate and complete sentences which explain...
The dataset in the file Lab11data.xlsx contains data on Crimini mushrooms. The factor variable is the...
The dataset in the file Lab11data.xlsx contains data on Crimini mushrooms. The factor variable is the weight of the mushroom in grams and the response variable is the total copper content in mg. 1. Plot Copper vs. Weight and describe. 2. Find least squares regression line and interpret slope in the words of the problem. 3. Find the coefficient of determination (R2) and interpret in context. 4. Find the correlation coefficient (R) and interpret in context. 5. Find and interpret...
In this problem, we will perform multiple regression on the Boston housing data. The data contains...
In this problem, we will perform multiple regression on the Boston housing data. The data contains 506 records with 14 variables. The variable medv is the response variable. Solve the following problems in R and print out the commands and outputs : To assess the data use library(MASS) data(Boston) (a) First perform a multiple regression with all the variables, what can you say about the significance of the variables based on only the p-values. Next use the ”step” function to...
Mastery Problem: Variable Costing for Management Analysis Absorption vs. Variable Operating income is one of the...
Mastery Problem: Variable Costing for Management Analysis Absorption vs. Variable Operating income is one of the most important items reported by a company. Depending on the decision-making needs of management, operating income can be determined using absorption costing or variable costing. Select whether the following characteristics are most often associated with absorption costing or variable costing. Required under generally accepted accounting principles (GAAP) Often used for internal use in decision making Cost of goods manufactured includes only variable manufacturing costs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT