/* Power Ball Lottery (15 points) The Power Ball Lottery (http://www.powerball.com/ ) is quite popular in the US because it often has huge jackpots that could make the owner of a winning ticket an instant millionaire. Every Wednesday and Saturday night they draw 5 white balls from a drum with 59 numbered white balls and one red ball (the PowerBall) from a drum with 39 numbered red balls. To play the game you have to buy a $1 ticket and mark five white numbers and one powerball number. After the drawing you can check if your five numbers match the numbers on the five balls (in any order) and whether or not your sixth number matches the powerball number. There are nine possible ways to win a prize, as shown below: Your Numbers Mathch | Prize ----------------------------------- 5 Balls + PowerBall -> Jackpot 5 Balls -> $200,000 4 Balls + PowerBall -> $10,000 4 Balls -> $100 3 Balls + PowerBall -> $100 3 Balls -> $7 2 Balls + PowerBall -> $7 1 Balls + PowerBall -> $4 0 Balls + PowerBall -> $3 Part A: Write a complete C program that generates a random drawing of the Power Ball Lottery. In other words, it draws five numbers (white balls) in the range [1..59] and one number (red ball) in the range [1..39]. The white ball numbers can be drawn in any order but they are drawn without replacement, i.e., the same number cannot appear twice in the list. Part B: Modify your program from Part A to generate a list of 20 random computer generated tickets for the PowerBall Lottery that players can purchase (about 80% of all tickets sold are computer generated). For each ticket the program must print if the ticket wins any prize (given the randomly drawn numbers from part A). */ #include #include #include int main(void) { int i, n; srand ( time(NULL) ); // initialize the random number generator int balls[59]; // generate a sorted list of balls 1..59 for(i=0; i<59; i++) balls[i]=i+1; for(i=0; i<5; i++) // draw 5 without replacement { // by swapping the i-th and the n-th n = i + rand()% (59-i); // elements of the balls array 5 times // during the first 5 iterations // n will be in the range: // 0 + [0...58] -> [0...58] // 1 + [0...57] -> [1...58] // 2 + [0...26] -> [2...58] // 3 + [0...25] -> [3...58] // 4 + [0...24] -> [4...58] // swap int temp = balls[i]; balls[i] = balls[n]; balls[n] = temp; } int powerball = rand()%39 + 1; printf("White Balls: "); for(i=0; i<5; i++) printf("%2d, ", balls[i]); printf(" Powerball: %2d\n", powerball); system("pause"); }