In: Computer Science
To play the PowerBall lottery, you buy a ticket that has five unique numbers in the range of 1–69, and a “PowerBall” number in the range of 1–26. (You can pick the numbers yourself, or you can let the ticket machine randomly pick them for you.) Then, on a specified date, a winning set of numbers is randomly selected by a machine. If your first five numbers match the first five winning numbers in any order, and your PowerBall number matches the winning Pow-erBall number, then you win the jackpot, which is a very large amount of money. If your numbers match only some of the winning numbers, you win a lesser amount, depending on how many of the winning numbers you have matched. In the student sample programs for this book, you will find a file named pbnumbers.txt, containing the winning PowerBall numbers that were selected between February 3, 2010 and May 11, 2016 (the file contains 654 sets of winning numbers). Figure 8-6 shows an example of the first few lines of the file’s contents. Each line in the file contains the set of six numbers that were selected on a given date. The numbers are separated by a space, and the last number in each line is the PowerBall number for that day. For example, the first line in the file shows the numbers for February 3, 2010, which were 17, 22, 36, 37, 52, and the PowerBall number 24. Write a program that works with this file to display the 10 most common numbers, ordered by frequency.
17 22 36 37 52 24 14 22 52 54 59 04 05 08 29 37 38 24 10 14 30 40 51 01 07 08 19 26 36 15 13 27 37 41 54 22 04 17 35 50 57 12 18 47 51 53 58 20 07 09 14 45 49 23 10 29 33 41 59 15 17 21 37 41 50 01 06 16 20 31 36 08 24 26 45 48 55 08 09 36 39 44 45 09 14 20 24 39 49 07 07 21 32 44 52 10 05 13 17 45 54 12 10 15 31 52 59 04 04 36 40 44 52 23 21 22 49 52 58 24 06 14 32 38 52 20 05 21 22 41 49 15 11 34 41 49 55 20 01 12 53 56 57 05 12 22 25 28 44 24 16 23 25 49 58 20 13 34 40 47 57 11 05 22 34 41 57 21 37 51 52 53 58 18 15 21 23 28 36 20 02 07 29 55 58 17 19 20 40 47 57 19 01 06 10 13 20 22 01 03 24 28 41 10 04 09 14 39 43 18 18 34 40 48 59 25 14 22 27 32 49 05 09 12 13 35 38 20 08 11 18 29 36 06 09 30 31 50 54 19 11 30 45 47 48 10 13 30 32 38 57 25 06 38 43 47 48 17 03 10 14 52 53 03 10 41 44 48 56 04 20 21 27 28 56 04 20 21 23 38 42 06 22 27 35 37 45 03 16 22 30 51 58 25 20 30 38 46 59 17 01 11 20 25 27 02 01 16 17 41 57 15 19 28 30 37 53 26 04 22 26 31 52 20 07 10 22 23 52 19 09 33 36 50 58 21 04 32 33 47 55 19 07 10 12 22 27 26 16 17 29 31 36 23 04 22 27 32 56 13 17 20 21 40 51 19 11 14 22 33 42 18 10 35 39 51 57 20 07 17 20 36 59 23 07 20 21 34 43 24 01 18 37 39 44 13 10 24 36 52 55 15 08 16 27 35 42 20 13 44 51 52 55 20 12 20 30 36 47 25 14 26 37 41 46 24 02 06 32 42 49 25 12 22 32 34 46 02 11 12 15 16 28 11 07 17 20 39 59 17 02 07 16 20 46 24 20 24 25 53 59 15 01 07 27 36 49 19 34 38 39 45 50 23 07 12 23 34 38 23 05 08 11 40 44 10 17 30 48 51 54 19 14 16 53 54 59 05 10 12 38 53 57 01 08 20 21 32 37 04 10 30 37 47 54 19 05 10 11 12 20 02 13 24 27 31 42 22 08 11 25 41 58 16 01 08 10 19 20 23 10 11 18 32 45 18 04 11 19 33 43 14 11 33 44 46 47 12 01 17 38 50 52 24 03 16 18 20 37 20 18 22 37 47 54 26 22 26 32 38 40 07 06 07 26 33 52 24 19 21 23 40 48 17 09 13 22 23 37 21 22 36 51 56 59 22 30 31 34 45 51 23 04 05 36 47 58 06 24 28 45 49 52 02 03 14 33 53 57 26 15 37 41 56 59 05 07 11 39 42 51 20 11 32 36 48 52 19 09 13 21 23 48 24 03 12 34 37 42 26 29 32 36 39 49 19 04 13 17 21 45 10 07 31 50 51 58 06 02 23 31 42 48 21 12 20 28 40 48 08 01 04 12 41 47 03 28 39 40 48 53 09 03 11 20 27 46 08 05 15 26 28 32 09 04 10 11 19 33 17 19 20 42 56 58 17 06 22 34 43 45 23 10 18 41 55 56 15 05 14 32 53 56 11 04 23 39 49 50 19 21 33 44 45 55 07 09 24 34 36 43 17 03 11 47 48 58 19 04 24 40 44 55 05 06 13 15 32 41 03 03 15 27 29 41 24 02 11 27 47 55 15 09 17 32 43 45 21 08 17 18 40 44 16 07 12 13 42 49 16 02 08 40 49 50 26 04 23 31 42 50 23 12 20 43 51 55 11 08 18 38 46 56 21 17 19 39 41 58 21 14 37 44 45 53 19 16 18 27 36 50 08 19 20 38 41 43 19 12 21 22 38 41 18 12 15 19 46 59 12 18 36 39 41 57 12 24 30 45 57 59 26 01 11 18 29 51 22 11 15 24 50 55 08 01 09 11 23 31 06 08 18 19 32 54 08 24 28 48 50 54 25 01 04 38 40 42 17 01 07 27 38 48 20 38 40 41 51 59 23 20 40 41 47 55 19 13 19 21 28 49 11 25 30 54 57 59 06 11 18 36 41 46 18 09 12 35 50 58 04 18 28 31 48 52 17 02 17 23 28 47 26 09 13 47 49 53 19 02 12 25 54 58 14 13 19 35 47 57 19 15 25 52 53 54 02 03 05 18 27 54 13 04 19 22 32 53 24 16 41 42 50 59 05 06 20 22 32 43 11 12 47 48 52 55 13 03 04 12 27 44 26 30 41 50 51 53 08 01 12 23 27 43 21 07 20 43 46 54 17 03 27 35 37 45 21 10 12 23 43 47 18 05 10 24 38 43 01 16 26 35 52 58 02 03 08 23 30 58 13 01 18 21 39 55 06 11 16 40 51 56 18 12 14 34 39 46 26 02 33 39 40 43 26 05 35 57 58 59 12 04 35 36 51 56 08 13 22 25 39 51 18 09 16 17 28 30 11 04 30 35 57 59 25 20 37 39 45 55 18 02 06 34 35 47 22 05 18 33 43 45 08 03 14 20 39 40 17 04 19 33 41 59 09 02 24 46 52 56 19 13 28 49 51 59 23 10 13 15 31 54 18 14 16 30 51 52 19 16 21 27 41 45 14 05 23 25 28 40 24 21 35 46 47 50 02 03 21 24 38 39 24 05 19 29 45 47 25 10 30 36 38 41 01 06 29 34 44 50 18 12 24 43 44 45 07 04 19 28 29 47 05 05 33 41 54 59 13 08 13 17 34 59 25 15 23 43 45 56 07 17 28 38 39 51 23 01 10 37 52 57 11 11 12 32 52 56 11 23 28 50 56 59 05 07 16 17 39 51 22 06 11 42 53 54 07 01 04 11 23 26 14 29 30 45 47 49 25 12 35 45 46 47 12 05 14 17 20 41 05 01 08 41 46 59 24 11 14 49 55 58 20 32 43 53 55 56 06 01 15 35 37 47 08 11 16 29 50 58 23 05 14 36 54 58 17 01 24 33 45 49 06 05 13 17 20 30 18 16 23 42 44 47 02 14 15 16 19 24 02 20 22 39 46 49 19 06 08 20 42 51 16 04 25 29 34 43 19 31 39 40 57 58 23 07 08 33 38 50 19 09 12 20 44 59 23 01 07 11 55 56 01 10 24 35 53 58 22 03 07 21 28 43 02 08 13 35 46 51 20 04 07 26 53 59 22 13 14 41 49 59 14 09 10 24 52 56 14 09 10 17 29 45 23 19 30 33 48 59 17 18 22 45 56 57 17 07 10 14 33 57 18 08 14 15 16 27 26 11 17 29 56 57 14 01 03 41 44 53 20 06 34 40 46 58 06 07 15 20 41 44 22 14 19 35 39 56 23 03 05 29 39 59 19 05 22 36 49 55 23 04 16 32 37 46 13 02 05 20 23 57 03 09 31 38 54 56 20 03 14 35 38 46 16 05 06 13 36 50 13 03 16 48 56 58 04 19 30 48 53 55 18 03 07 11 15 28 12 04 13 39 46 51 01 06 27 46 51 56 21 14 26 41 55 59 01 22 29 31 47 55 19 01 06 07 20 49 23 25 28 49 54 56 18 08 11 21 44 49 22 04 19 26 42 51 19 06 20 34 44 48 19 24 33 36 48 56 06 03 20 26 43 48 01 01 05 08 39 50 23 02 16 18 40 42 23 13 26 39 41 42 10 14 18 28 29 57 08 17 23 36 55 59 10 15 26 34 36 59 25 18 26 29 35 43 18 02 05 25 26 49 18 01 07 10 23 42 25 04 21 28 31 44 10 03 18 21 23 50 04 22 32 34 36 56 23 01 27 31 45 48 05 04 07 09 30 54 25 32 34 45 52 58 20 32 42 50 54 55 22 08 10 30 44 58 13 03 15 27 58 59 20 08 18 24 30 39 26 22 32 37 44 50 24 05 16 22 23 29 06 03 10 19 36 46 03 13 17 19 27 38 12 07 23 26 40 53 21 08 10 25 36 44 18 15 23 40 44 55 14 05 08 20 23 30 03 01 18 35 39 44 11 11 13 23 43 54 04 36 46 50 52 55 14 18 20 28 35 53 20 26 30 49 51 54 25 11 13 20 27 59 26 10 14 21 23 47 07 09 21 28 32 51 25 08 28 29 34 38 25 11 12 24 43 45 09 03 22 26 41 49 18 14 16 32 47 52 16 11 16 33 40 41 24 05 27 36 38 41 12 05 06 16 36 58 03 12 23 25 27 43 19 15 16 46 50 58 19 03 17 19 25 32 17 02 05 31 39 41 19 03 14 20 34 48 21 03 08 13 41 56 16 06 10 23 41 45 01 10 37 40 46 52 12 05 09 28 32 38 19 03 07 21 44 53 16 13 14 17 43 54 15 17 29 31 52 53 21 07 37 43 48 52 16 11 23 26 46 55 17 01 06 08 12 35 03 04 07 08 29 39 24 01 36 40 52 53 20 10 12 31 56 57 23 13 18 36 48 58 18 06 08 30 39 48 20 09 19 31 56 59 02 03 23 48 54 55 05 22 26 31 54 55 18 07 12 26 36 40 17 21 22 26 30 57 17 06 13 19 23 43 16 02 11 26 34 41 22 10 13 14 22 52 11 09 31 35 41 57 26 02 06 19 21 27 25 09 14 17 49 57 02 22 28 33 53 59 14 04 26 33 36 55 22 02 11 22 26 32 19 16 22 23 42 55 22 28 36 40 48 55 01 07 46 47 52 57 17 13 19 23 33 57 18 01 18 33 39 46 23 08 28 30 53 56 16 03 06 29 40 51 04 02 13 35 36 52 11 30 31 45 55 59 17 02 08 22 35 37 06 01 22 34 38 42 17 14 25 27 38 58 06 09 29 40 44 54 07 09 23 40 53 58 06 08 24 39 49 59 05 21 24 36 42 45 15 05 25 30 58 59 22 04 12 14 37 58 13 04 11 17 43 51 20 18 21 46 54 56 23 30 40 42 46 48 23 12 17 25 45 59 19 06 07 09 19 32 13 02 07 25 40 56 20 02 09 26 45 47 11 02 19 22 26 45 24 11 19 33 42 52 23 01 17 25 37 44 20 07 10 22 32 35 19 12 17 45 54 58 13 02 07 17 49 53 23 14 47 52 53 54 05 04 06 25 42 51 17 11 12 17 39 40 05 03 09 19 33 38 18 08 10 26 57 58 04 03 26 28 34 42 18 09 33 54 56 57 05 03 23 31 34 47 13 04 06 34 49 56 19 02 36 40 49 54 10 13 23 24 27 40 17 01 05 10 15 49 22 03 09 37 49 56 22 05 31 50 55 56 09 10 29 37 44 59 10 04 18 23 32 45 07 05 12 43 52 55 10 18 25 50 55 57 17 05 26 44 45 57 19 06 09 11 31 44 15 13 20 32 45 48 17 01 10 13 18 19 17 14 25 32 33 41 24 07 24 37 39 40 01 25 36 40 45 51 08 23 28 38 39 56 22 08 35 44 51 56 18 15 24 40 48 52 23 19 20 37 41 58 14 10 28 39 47 58 22 10 15 33 48 54 24 07 08 09 24 29 25 13 14 19 31 38 25 01 02 07 09 55 19 08 12 18 55 57 02 11 23 28 32 47 20 05 12 15 27 38 07 08 17 32 57 59 24 24 25 34 37 54 19 36 44 49 52 57 01 02 09 14 21 23 03 01 17 35 49 54 24 02 03 13 14 54 04 11 12 17 38 42 02 03 08 25 30 47 13 03 07 09 26 54 19 10 14 24 32 41 20 14 15 28 37 54 10 02 05 34 51 58 09 02 19 23 34 43 14 13 28 31 55 58 15 28 33 41 44 59 21 02 03 12 27 38 17 08 13 19 22 53 24 11 21 26 33 34 19 09 14 44 48 49 19 14 26 45 54 55 20 34 39 42 44 59 08 05 06 29 35 51 21 19 25 29 36 48 12 03 07 22 30 33 20 02 09 11 19 50 22 05 15 16 46 49 26 17 29 31 48 49 24 04 31 41 47 55 01 07 33 39 52 55 23 23 32 39 47 49 22 04 20 34 39 58 21 15 16 28 49 55 18 02 24 28 32 59 25 15 27 31 34 48 01 01 07 10 22 49 24 28 30 35 58 59 15 14 18 25 33 49 23 09 33 42 45 54 20 06 09 29 52 59 07 05 06 37 41 54 26 10 20 25 50 53 25 08 18 45 53 58 25 24 34 36 57 58 11 09 25 42 55 57 14 02 03 07 23 51 26 05 15 18 26 32 25 10 17 25 45 53 09 04 10 12 22 31 03 24 28 30 38 39 16 13 30 42 49 53 19 12 26 44 46 47 19 01 08 24 28 49 24 03 12 31 34 51 24 08 37 39 40 52 24 07 08 17 48 59 09 04 08 21 38 40 03 28 32 35 36 52 21 17 24 26 45 46 19 05 28 31 52 59 17 02 16 43 45 51 25 09 29 31 43 50 18 02 14 39 40 43 13 01 06 16 37 53 17 18 25 36 48 50 23 22 23 30 37 39 16 07 14 21 24 41 26 02 11 35 52 54 13 01 04 18 20 45 07 13 18 24 25 33 21 05 16 31 46 50 18 10 19 37 38 39 18 05 07 19 27 28 20 20 26 27 36 54 19 29 30 40 42 50 16 06 10 51 54 57 12 25 28 48 57 59 16 01 03 13 25 38 17 02 11 19 21 42 24 09 19 33 38 54 15 37 39 51 52 55 11 13 16 33 35 51 18 06 36 38 48 51 17 23 49 53 54 57 25 16 17 22 46 54 25 13 24 30 42 48 17 25 30 32 46 54 26 12 15 22 43 49 14 34 44 48 54 55 10 05 13 28 43 55 23 22 31 38 47 48 15 14 15 19 31 56 05 11 12 46 47 50 22 07 10 11 14 36 15 17 27 37 40 53 25 04 18 43 46 55 25 14 15 47 49 59 10 02 09 19 28 29 19 02 04 10 41 53 22 15 16 23 27 36 09 11 12 15 28 57 23 16 19 20 29 33 10 12 24 35 36 49 01 05 11 16 26 50 24 24 36 51 52 56 22 05 10 21 34 58 23 11 13 25 39 54 19 01 24 44 45 51 18 01 09 29 32 49 22 10 14 18 34 51 26 17 19 21 32 39 08 11 17 25 28 46 12 08 12 15 35 50 22 34 36 38 42 50 23 11 24 31 40 44 17 08 14 39 46 47 18 14 25 30 33 47 08 11 16 30 38 42 07 07 19 23 50 54 14 02 04 06 12 38 17 02 30 33 39 44 01 33 39 40 41 54 18 01 19 45 46 58 19 01 12 32 42 58 12 01 16 21 29 40 20 13 22 23 29 31 17 10 14 25 39 53 18 21 33 35 38 45 12 01 26 34 38 51 06 02 06 11 30 31 23 23 24 27 39 41 20 04 15 17 35 58 17 01 25 29 31 47 07 24 29 38 48 52 22 01 12 28 35 44 25 09 15 17 31 43 16 08 15 34 53 59 23 08 09 25 56 57 22 06 08 13 37 40 11 08 13 18 27 43 15 31 32 48 49 53 25 29 41 48 52 54 19 20 21 22 41 54 07 09 10 16 20 57 15 03 05 10 22 32 07 18 28 35 46 49 17 07 24 26 31 41 25 03 06 14 18 24 21 04 15 25 27 30 18 11 39 46 52 54 03 13 16 34 45 50 11 06 37 39 45 55 23 12 31 43 44 57 11 27 29 34 41 44 02 04 22 27 28 52 25 07 13 24 49 57 15 09 11 14 16 42 19 09 34 48 52 54 15 08 13 29 38 52 18 03 13 17 42 52 24 06 08 43 48 50 07 04 12 14 21 55 07 02 22 32 45 56 12 18 21 25 28 29 16 17 22 30 46 56 16 10 16 18 29 45 19 44 45 47 50 51 08 02 03 13 16 35 17 05 07 24 31 39 07 12 17 26 43 48 24 08 29 41 51 58 05 23 31 42 50 57 05 21 39 40 55 59 17 06 26 33 44 46 04 18 30 40 48 52 09 12 27 29 43 68 01 15 20 29 31 40 01 48 49 57 62 69 19 30 32 42 56 57 11 20 31 56 60 64 02 04 54 56 62 63 10 09 20 25 47 68 07 02 12 17 20 65 17 07 16 25 50 53 15 04 26 32 55 64 18 14 22 37 45 66 05 17 40 41 46 69 06 37 47 50 52 57 21 16 29 53 58 69 21 02 06 47 66 67 02 14 18 19 32 64 09 13 27 33 47 68 13 07 10 16 46 56 01 02 14 19 30 62 22 09 10 32 42 55 06 28 30 41 59 68 10 16 38 55 63 67 25 27 40 44 59 65 20 12 36 38 54 61 22 05 06 15 29 42 10 02 11 47 62 63 17 16 19 32 34 57 13 04 08 19 27 34 10 03 51 52 61 64 06 05 39 44 47 69 24 22 32 34 40 69 19 03 12 40 52 67 21 05 12 16 31 43 18 26 28 31 60 67 23 04 13 31 36 52 08 02 03 40 50 62 05 07 15 18 19 36 20 07 17 27 29 40 25 11 12 15 16 54 25 21 31 64 65 67 05 10 11 21 22 53 18 12 13 44 52 62 06 03 27 34 59 69 19 14 23 32 34 68 03 11 28 50 57 62 23 10 12 13 46 50 21 11 23 43 54 60 03 05 08 15 22 49 25 11 23 42 52 68 06 24 44 53 55 63 19 09 28 30 40 61 03 04 28 49 60 65 25 14 22 23 41 61 09 30 33 35 38 64 22 03 18 25 32 51 03 12 25 30 52 62 08 19 35 46 59 62 13 02 25 33 39 64 17 03 12 16 32 34 14 30 47 57 66 69 03 05 25 26 44 66 09 20 32 52 66 69 23
Lottery.cpp
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
struct freqNumbers
{
int num; //Frequent mumber
int freq; //Frequency of frequent Numbers
};
//comparion function to sort numbers according to frequency
int comp(const void *x,const void *y)
{
int l = ((struct freqNumbers*)x)->freq;
int r = ((struct freqNumbers*)y)->freq;
return (r-l);
}
int main()
{
int numbers[70]={0}; //array to count Frequency of
each number
freqNumbers fnum[70]; //array to structure
freqNumbers
int n;
ifstream ifile;
ifile.open("lotteryNumbers.txt"); //open text file of
lottery numbers
if(!ifile)
{
cerr<<"Can't open
file";
return 0;
}
while(ifile>>n)
{
numbers[n]++; //read numbers from
text file and count frequncy
}
//insert numbers and frequency into array to
freqNumbers
for(int i=1;i<70;i++)
{
fnum[i].num= i;
fnum[i].freq=numbers[i];
}
//sort numbers in descending order of frequency
qsort(fnum,69,sizeof(struct freqNumbers),comp);
//print 10 most frequent numbers order by
frequency
cout<<"Number Frequency"<<endl;
for(int i=0;i<10;i++)
cout<<fnum[i].num<<"
"<<fnum[i].freq<<endl;
return 0;
}
OUTPUT