Code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_TICKETS 10000
#define NUM_NUMBERS 5
#define MAX_NUMBER 70
#define MAX_POWERBALL 30
void generateNumbers(int arr[], int n, int max) {
int i, j, temp;
for (i = 0; i < n; i++) {
arr[i] = rand() % max + 1;
for (j = 0; j < i; j++) {
if (arr[j] == arr[i]) {
i--;
break;
}
}
}
}
void printNumbers(int arr[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int checkMatch(int ticket[], int winningNumbers[], int powerball) {
int i, numMatch = 0;
for (i = 0; i < NUM_NUMBERS; i++) {
if (ticket[i] == powerball) {
numMatch++;
break;
}
}
for (i = 0; i < NUM_NUMBERS; i++) {
int j;
for (j = 0; j < NUM_NUMBERS; j++) {
if (ticket[i] == winningNumbers[j]) {
numMatch++;
break;
}
}
}
return numMatch;
}
int main() {
int i, j;
int winningNumbers[NUM_NUMBERS];
int winningPowerball;
int ticket[NUM_NUMBERS + 1];
int jackpot = 0, match5 = 0, match4 = 0, match3 = 0, matchPowerball = 0;
srand(time(NULL));
generateNumbers(winningNumbers, NUM_NUMBERS, MAX_NUMBER);
winningPowerball = rand() % MAX_POWERBALL + 1;
printf("Winning numbers: ");
printNumbers(winningNumbers, NUM_NUMBERS);
printf("Powerball: %d\\", winningPowerball);
for (i = 0; i < NUM_TICKETS; i++) {
generateNumbers(ticket, NUM_NUMBERS, MAX_NUMBER);
ticket[NUM_NUMBERS] = rand() % MAX_POWERBALL + 1;
int numMatch = checkMatch(ticket, winningNumbers, winningPowerball);
switch (numMatch) {
case 6:
jackpot++;
break;
case 5:
match5++;
break;
case 4:
match4++;
break;
case 3:
match3++;
break;
case 1:
if (ticket[NUM_NUMBERS] == winningPowerball) {
matchPowerball++;
}
break;
}
}
printf("Jackpot winners: %d\\", jackpot);
printf("Match 5 winners: %d\\", match5);
printf("Match 4 winners: %d\\", match4);
printf("Match 3 winners: %d\\", match3);
printf("Match powerball winners: %d\\", matchPowerball);
return 0;
}
Step-by-step explanation:
- This program generates the winning numbers and powerball using the generateNumbers() function, which ensures that each number is unique. It then generates 10,000 lotto tickets and checks each one using the checkMatch() function, which returns the number of matching numbers and the powerball. The results are tallied and printed at the end