/* 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 balls from a drum with 59 numbered white balls and one 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 numbers and one powerball number. After the drawing you can check how many of your five numbers match the numbers on the five white 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: (5 points) 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: (10 points) 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 and the table above). SAMPLE RUN: White Balls: 57, 45, 28, 42, 37, PowerBall: 25 <--DRAWING White Balls: 3, 6, 22, 47, 1, PowerBall: 13 nothing (0+0) White Balls: 59, 7, 37, 53, 4, PowerBall: 32 nothing (1+0) White Balls: 45, 33, 10, 24, 7, PowerBall: 27 nothing (1+0) White Balls: 17, 39, 26, 47, 5, PowerBall: 4 nothing (0+0) White Balls: 4, 12, 33, 14, 53, PowerBall: 10 nothing (0+0) White Balls: 2, 25, 44, 7, 8, PowerBall: 26 nothing (0+0) White Balls: 10, 3, 35, 20, 29, PowerBall: 25 $3 (0+1) White Balls: 21, 35, 20, 33, 27, PowerBall: 27 nothing (0+0) White Balls: 39, 13, 8, 1, 36, PowerBall: 6 nothing (0+0) White Balls: 30, 36, 28, 1, 15, PowerBall: 29 nothing (1+0) White Balls: 50, 4, 5, 1, 14, PowerBall: 1 nothing (0+0) White Balls: 23, 3, 55, 20, 1, PowerBall: 29 nothing (0+0) White Balls: 4, 47, 12, 20, 26, PowerBall: 36 nothing (0+0) White Balls: 10, 58, 46, 24, 18, PowerBall: 32 nothing (0+0) White Balls: 3, 13, 44, 19, 20, PowerBall: 25 $3 (0+1) White Balls: 15, 1, 55, 10, 54, PowerBall: 5 nothing (0+0) White Balls: 7, 25, 49, 5, 8, PowerBall: 24 nothing (0+0) White Balls: 42, 4, 52, 7, 18, PowerBall: 25 $4 (1+1) White Balls: 55, 3, 39, 52, 47, PowerBall: 6 nothing (0+0) White Balls: 21, 4, 25, 33, 9, PowerBall: 4 nothing (0+0) */ #include #include #include void generateNumbers(int numbers[]) { int i,n; int balls[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; // copy into the array for(i=0; i<5; i++) numbers[i]=balls[i]; numbers[5]=powerball; } void printNumbers(int numbers[]) { int i; printf("White Balls: "); for(i=0; i<5; i++) printf("%2d, ", numbers[i]); printf(" PowerBall: %2d ", numbers[5]); } void matchNumbers(int drawing[], int ticket[]) { int PowerBallMatch =0; if(drawing[5] == ticket [5]) // check if we matched the powerball PowerBallMatch = 1; int i, j; // Count how many white ball numbers we matched int nMatching=0; for(i=0; i<5; i++) for(j=0; j<5; j++) if (drawing[i] == ticket[j]) nMatching++; switch(nMatching) // Print what the ticket wins, if anything { case 5: if(PowerBallMatch) printf("Jackpot"); else printf("$200,000"); break; case 4: if(PowerBallMatch) printf(" $10,000"); else printf(" $100"); break; case 3: if(PowerBallMatch) printf(" $100"); else printf(" $7"); break; case 2: if(PowerBallMatch) printf(" $7"); else printf(" nothing"); break; case 1: if(PowerBallMatch) printf(" $4"); else printf(" nothing"); break; case 0: if(PowerBallMatch) printf(" $3"); else printf(" nothing"); break; } printf(" (%d+%d)\n", nMatching, PowerBallMatch); } int main(void) { srand ( time(NULL) ); // initialize the random number generator int drawing[6]; generateNumbers(drawing); printNumbers(drawing); printf(" <--DRAWING\n\n"); int t; int numbers[6]; for(t=0; t<20; t++) { generateNumbers(numbers); printNumbers(numbers); matchNumbers(drawing, numbers); } printf("\n"); system("pause"); }