(E) Lottery games

Goal: I will learn to create functions in a C++ program.

Instructions: Create a new project: student/02/lotto/.

There is a multitude of different lottery games in the world. What they all have in common is that a few numbered balls will be randomly selected from a certain amount of balls. Depending on how lucky the gambler is in guessing which ball numbers come up, the gambler is awarded a prize.

In this exercise, we are interested in the probabilities of a player to win the jackpot, that is, to guess correctly all the numbers of the balls drawn. For example, in the case of the Finnish lottery: what is the probability that a player guesses all 7 of the drawn balls correctly if they fill out only one lottery ticket.

This problem is connected to combinatorics: how many different ways there are to choose 7 balls out of 39 balls. Let us presume that n is the amount of all lottery balls, and p is the amount of the drawn balls. Therefore, the amount of different lottery combinations that exist is

\[\frac{n\,!}{(n - p)\,! * p\,!}\]

lines. The !-operator you saw above is used in mathematics to present the factorial of the preceding number. The factorial means the product of all the positive integers that are smaller or equal to the number in question. For example,

\[\begin{split}\begin{align*} 1! &= 1 \\ 2! &= 1 * 2 = 2 \\ 3! &= 1 * 2 * 3 = 6 \\ 4! &= 1 * 2 * 3 * 4 = 24 \\ 5! &= 1 * 2 * 3 * 4 * 5 = 120 \end{align*}\end{split}\]

and so forth. In addition, the factorial of the number 0 has been defined to be 1, i.e.

\[0! = 1.\]

Based on what has been told above, we are able to calculate the number of all the possible different lottery combinations of the Finnish lottery (where you draw 7 numbers out of 39):

\[\frac{39\,!}{(39 - 7)\,! * 7\,!} = 15380937.\]

So, the possibility to have a ticket with 7 correct numbers by filling one lottery ticket is

\[\frac{1}{15380937}\,.\]

Note

The automatic evaluation system will not test the program with numbers this large. It is enough for you to use the data type unsigned long int in your calculation.

Implement a program that asks the user for two input values:

  • the amount of lottery balls (numbers)
  • the amount of balls (numbers) to be drawn.

After that, it prints the probability of winning the jackpot if you fill only one ticket (1 divided by the amount of different lottery combinations).

Also, add the following error messages to your program:

  • ”The number of balls must be a positive number.”
  • ”The maximum number of drawn balls is the total amount of balls.”

Both the total amount of balls and the amount of drawn balls should be asked before error checks. First you need to check whether the amounts of balls are positive. If there is an error, print the error message and terminate the program immediately. In case both of the given numbers are positive, you should consider the amounts of balls. If you find an error, you should again print the error message and terminate the program.

An example of how the program works:

Enter the total number of lottery balls: 20
Enter the number of drawn balls: 4
The probability of guessing all 4 balls correctly is 1/4845

Enter the total number of lottery balls: -3
Enter the number of drawn balls: 4
The number of balls must be a positive number.

Tips for completing the exercise:

  • First, consider which parts of the program you should implement as functions.

  • Remember that testing a large program in parts (one function at a time) is easier than testing the whole program at once. After you have completed a function, your next step should be testing it.

  • Testing small parts of a program was easy with Python, because Python is a language that uses an interpreter. With C++, you always need a whole program, including the main function, if you want to compile and execute a program.

  • For example, if you created the function foo that takes an integer as its parameter and returns another integer, you could write the following test lines in the beginning of the main function:

    cout << "foo with parameter 0 returns value" << foo(0) << endl;
    cout << "foo with parameter 1 returns value" << foo(1) << endl;
    cout << "foo with parameter 5 returns value" << foo(5) << endl;
    

Food for thought:

  • Remember what you learned about the limitations of C++ data types on the previous round of this course. This program was created on the previous programming course using Python, and back then, the example in the instructions was the Finnish lottery game:

    Enter the total number of lottery balls: 39
    Enter the number of drawn balls: 7
    The probability of guessing all 7 balls correctly is 1/15380937
    
  • Will the program you now implemented with C++ work right when calculating the probability of the Finnish lotto? (Please recall the earlier note: The automatic evaluation system will not test the program with numbers this large. It is enough for you to use the data type unsigned long int in your calculation.)

A+ presents the exercise submission form here.