C++ Tutorial: Creating a C++ Megamillions Program

These two tutorials came out VERY well. The first one shows you how to write a C++ program that generates an unlimited number of Megamillions lottery numbers. The second tutorial shows how to print those numbers out to a file. These tutorials are excellent for building your ability to work with random numbers. Both tutorials have High Definition Youtube videos, and the full source code is available for both of them.

The first tutorial is at http://djere.com/node/23
The second is at http://djere.com/node/24

Here is a sample of the node 23 code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// cpp.arrays.0.cpp
// cpp.arrays.0: Using C++ to Create a Megamillions Program
// written by Rex Djere

// This C++ program simulates the Megamillions lottery.
// It outputs as many sets of numbers as the user wants.
// Megamillions program:
//   - selects 5 random white balls numered from 1 to 56, 
//   - selects 1 gold mega ball numbered from 1 to 46
//   - outputs white balls 1 to 5 in ascending order, followed by gold ball 6

// The source code and tutorial for this program will be maintained at 
// http://djere.com/node/23

#include <iostream> // needed for input and output e.g. from keyboard and to screen
#include <vector> // needed for vector functions
#include <algorithm> // needed for the srand and random_shuffle functions
#include <ctime> // we need system time to seed random number generator
#include <iomanip> // needed for the setw function which sets the column width of the output

using namespace std;

int main()
{

   srand(time(0)); // seeds the random number generator


   int balls[56]; // an array of 56 elements holds the white balls
   int megaBall[46]; // an array of 46 elements holds the gold Mega balls
   int ballsOut[5]; // this array stores the five white ball numbers
   int megaBallOut; // this variable stores the  Mega ball number
   int i,j, k; // counters
   int numberOfSets; // how many sets of numbers the user would like to create

   cout << "How many sets of MegaMillions numbers would you like to generate?" << endl;
   cin >> numberOfSets; // inputs desired number of sets
   cout << endl; // new line for spacing
   cout << "Here are your numbers: " << endl << endl; // prepares user to receive output

   for (k=0; k<numberOfSets; k++)
   {
      // The two lines below fill the white ball and Mega ball arrays. 
      for (i = 0; i < 56; i++) balls[i] = i+1;  // The white ball array is filled sequentially from 1-56.
      for (j = 0; j < 46; j++) megaBall[j] = j+1; // The Mega ball array is filled sequentially from 1 to 46.

     random_shuffle(&balls[0], &balls[56]); // shuffles the white balls
     random_shuffle(&megaBall[0], &megaBall[46]); // shuffles the Mega balls
	 
	 

     for (i = 0; i < 5; ++i) ballsOut[i] = balls[i]; // fills ballOut array with values of the 5 white balls
     megaBallOut = megaBall[1]; // outputs the Megaball value


     sort(ballsOut, ballsOut + sizeof(ballsOut)/sizeof(ballsOut[0])); // sorts the white balls from lowest to highest
     
     // the two lines below print the output
     for (i=0; i<5;i++) cout << setw(5) << ballsOut[i] <<" ";
     cout << setw(6) << megaBallOut << endl;
   }
   
  return 0;
}
Last edited on
Why is this using arrays, especially when it #include'd <vector>

Why is the use of arrays so inconsistent (compare the way begin/end iterators are obtained at lines 47/48 and at line 56)

Speaking of 47/48 and many other lines, why so many hardcoded constants?

Why are so many objects created at lines 29-34 for no reason, with no meaningful values?

Why does it repopulate balls and megaBall on every iteration when it's sufficient to only shuffle?

This is hardly "VERY well" written.
cout << endl; // new line for spacing
Never. Never write comments like this.

You should write comments that are helpful in understanding obscure code. This means you should comment what you intend to do, not what the code does like for kindergarten-level programmers. And then there's the second point: don't write obscure code!

Example:
1
2
3
i++; // increment i by one
outb(0x20, 0x20); // output byte 0x20 to port 0x20
outb(0x20, 0x20); // send reset signal to master PIC 


Edit: wording for less ambiguity.
Last edited on
I agree you shouldn't write comments as detailed as this in "real" code but if the code is written to be read by absolute beginners to learn from it can be a good thing.
>>Why is this using arrays, especially when it #include'd <vector>

@Cubbi I use arrays because the size of the balls and Megaball pools are static. This is exactly the intent of an array: when you don't need a dynamically re-sizable container. I forgot to delete the vector header file, I originally intended on using vectors, but I then realized that an array made more sense.

>>Why is the use of arrays so inconsistent (compare the way begin/end iterators are obtained at lines 47/48 and at line 56)

Two different functions....

>>Why does it repopulate balls and megaBall on every iteration when it's sufficient to only shuffle?
This is just me being retentive. I prefer to have each iteration start from the same condition...the balls array aligned sequentially from 1 to 56, and the Megaballs array aligned from 1 to 46.

Topic archived. No new replies allowed.