C++ Programming The Powerball lottery allows participants to pick 5 numbers from
ID: 3665092 • Letter: C
Question
C++ Programming The Powerball lottery allows participants to pick 5 numbers from numbers ranging from 1 to 69 and a Powerball number ranging from 1 to 26. The odds of winning is calculated by using the formula: (n! / (n-r)! * r!) * 26. So, the odds of winning the lottery is (69! / (64! * 5!)) * 26 = 292201338. NOTE that n! means the factorial of the number n. Because, Lottery process may change, write a program to read the upper value of the range of the numbers to pick (i.e. 69), number of numbers to select (i.e., 5) and the upper value of the range of the Powerball number (i.e., 26). Calculate the display the odds of winning the Lottery using the above formula. USE FUNCTIONS. For example, I need a function that calculates the factorial of any number. NOTE: Factorial calculation is a very simple calculation of the product of the numbers starting from 1 to the number desired for the factorial calculation. For example, factorial of 5 is 1*2*3*4*5 = 120.
Explanation / Answer
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int winning[6];
int ticket[6];
int ticketCounter;
int matchCounter;
const int whiteball_MAX = 40;
const int powerball_MAX = 20;
const int matchCounter_MAX = 6;
int whiteball[whiteball_MAX];
void fillArrayBalls();
void shuffleBalls();
void generateWinningNumbers();
void checkNumbers();
int main()
{
srand(time(NULL));
fillArrayBalls();
generateWinningNumbers();
shuffleBalls();
cout << "// Powerball lottery" << endl;
cout << "// Match all numbers and win 1.5 Billion dollars!" << endl << endl;
cout << "Winning numbers: " << winning[0] << " " << winning[1] << " " << winning[2] << " " << winning[3] << " " << winning[4] << " PWR " << winning[5] << endl;
cout << "--------------------------------------" << endl;
cout << "Generating tickets, please wait..." << endl << endl;
while (matchCounter != matchCounter_MAX)
{
shuffleBalls();
matchCounter = 0;
for (int i = 0; i < 5; i++)
{
ticket[i] = whiteball[i];
}
ticket[5] = rand() % powerball_MAX + 1;
checkNumbers();
ticketCounter++;
}
cout << "|------------Lottery Ticket------------|" << endl;
cout << " Your numbers: " << ticket[0] << " " << ticket[1] << " " << ticket[2] << " " << ticket[3] << " " << ticket[4] << " PWR " << ticket[5] << endl << endl;
cout << " Total matched: " << matchCounter << endl;
cout << " Total tickets used: " << ticketCounter << endl;
cout << " Total cash spent: $" << ticketCounter * 2 << endl;
cout << "|--------------------------------------|" << endl << endl;
cout << "Congratulations, you're a winner!" << endl << endl;
return 0;
}
void generateWinningNumbers()
{
shuffleBalls();
for (int i = 0; i < 5; i++)
{
winning[i] = whiteball[i];
}
winning[5] = rand() % powerball_MAX + 1;
}
void fillArrayBalls()
{
for (int i = 0; i < whiteball_MAX; i++)
{
whiteball[i] = i + 1;
}
}
void shuffleBalls()
{
for (int i = 0; i < whiteball_MAX; i++)
{
int temp = whiteball[i];
int randomIndex = rand() % whiteball_MAX;
whiteball[i] = whiteball[randomIndex];
whiteball[randomIndex] = temp;
}
}
void checkNumbers()
{
for (int i = 0; i < 5; i++)
{
if (ticket[i] == winning[0])
{
matchCounter++;
}
else if (ticket[i] == winning[1])
{
matchCounter++;
}
else if (ticket[i] == winning[2])
{
matchCounter++;
}
else if (ticket[i] == winning[3])
{
matchCounter++;
}
else if (ticket[i] == winning[4])
{
matchCounter++;
}
}
if (ticket[5] == winning[5])
{
matchCounter++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.