The problem is to write a program that checks if all the input numbers cover 1 to 99.
Each Pick-10 lotto ticket has 10 unique numbers ranging from 1 to 99. Suppose you buy a lot of tickets and would like to have them cover all numbers from 1 to 99. Write a program that reads the ticket numbers from a file and checks whether all numbers are covered. Assume the last number in the file is 0. Suppose the file contains the numbers
80 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76
0
Your program should display
The tickets cover all numbers
Suppose the file contains the numbers
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
0
Your program should display
The tickets don’t cover all numbers
How do you mark a number as covered? You can declare an array with 99 bool elements. Each element in the array can be used to mark whether a number is covered. Let the array be isCovered. Initially, each element is false, as shown in Figure 7.2a. Whenever a number is read, its corresponding element is set to true. Suppose the numbers entered are 1, 2, 3, 99, 0. When number 1 is read, isCovered[1 – 1] is set to true (see Figure 7.2b). When number 2 is read, isCovered[2 – 1] is set to true (see Figure 7.2c). When number 3 is read, isCovered[3 – 1] is set to true (see Figure 7.2d). When number 99 is read, set isCovered[99 – 1] to true (see Figure 7.2e).
The algorithm for the program can be described as follows:
for each number k read from the file,
mark number k as covered by setting isCovered[k – 1] true;
if every isCovered[i] is true
The tickets cover all numbers
else
The tickets don’t cover all numbers
The complete program is given in Listing 7.2
Listing 7.2 LottoNumbers.cpp
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 bool isCovered[99];
7 int number; // number read from a file
8
9 // Initialize the array
10 for (int i = 0; i < 99; i++)
11 isCovered[i] = false;
12
13 // Read each number and mark its corresponding element covered
14 cin >> number;
15 while (number != 0)
16 {
17 isCovered[number – 1] = true;
18 cin >> number;
19 }
20
21 // Check if all covered
22 bool allCovered = true; // Assume all covered initially
23 for (int i = 0; i < 99; i++)
24 if (!isCovered[i])
25 {
26 allCovered = false; // Find one number not covered
27 break;
28 }
29
30 // Display result
31 if (allCovered)
32 cout << “The tickets cover all numbers” << endl;
33 else
34 cout << “The tickets don’t cover all numbers” << endl;
35
36 return 0;
37 }
Suppose you have created a text file named LottoNumbers.txt that contains the input data 2 5 6 5 4 3 23 43 2 0. You can run the program using the following command:
g++ LottoNumbers.cpp -o LottoNumbers.exe
LottoNumbers.exe < LottoNumbers.txt
The program can be traced as follows:
The program declares an array of 99 bool elements (line 6) and initializes each element to false (lines 10-11). It reads the first number from the file (line 14). The program then repeats the following operations in a loop:
- If the number is not zero, set its corresponding value in array isCovered to true (line 17);
- Read the next number (line 18).
When the input is 0, the input ends. The program checks whether all numbers are covered in lines 22-28 and displays the result in lines 31-34.
Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.