68.1k views
1 vote
C program

You are to write a program which will do the Lotto.
The lotto consists of 5 numbers from 1-70 and a power ball from numbers 1-30.
The first 5 numbers should not repeat (same for the winning numbers). The power ball can repeat with any of the first 5 numbers.

You are going to purchase 10,000 lotto tickets. Each ticket has 6 numbers (5 num and 1 pow).
Give each ticket random numbers, and compare to the winning numbers (winning numbers generated only once).

Match the 5 numbers and the power ball number and you win the jackpot!
Match 5 numbers only and you win $1,000,000.
Match 4 numbers only and you win $50,000.
Match 3 numbers only and you win $7.
Match under 3 numbers, but you got the power ball and you win $4.

anything else wins nothing.

Need help, program must work!!!

1 Answer

4 votes

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