cpp.arrays.0: Using C++ to Create a Megamillions Program

***This tutorial neithers promotes nor condones gambling.**
This tutorial shows how solve a real-life problem with C++. We have been tasked to create a C++ program that ouputs an unlimited number of Megamillions numbers for the program's user.

Learning Objectives:

  • Explain how Megamillions works.
  • Explain pseudocode.
  • Explain what an array is.
  • Explain the difference between a C++ array and a C++ vector.
  • Explain how to create a C++ array.
  • Walk through writing the Megamillions program.

cpp.arrays.0: Using C++ To Create A Megamillions... by Djere University

The LibreOffice Impress-generated presentation for this lecture is here:
http://djere.com/lectures/node23lecture/node23.using_cpp_to_create_a_meg...


***Source code begins immediately below***

// 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;
}

***Source code ends immediately above***