Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Desgin a lottery draw program using C programming. Implement a Lottery draw prog

ID: 673864 • Letter: D

Question

Desgin a lottery draw program using C programming.

Implement a Lottery draw program, where (you) the user will input a set of names belonging to the contestants. A random number generator will determine the winner out of the pool of contestants. You should use a circular singly linked-list holding ail the names of the contestants. You can use add to the head or add to the end functionality to build up your list. Once your linked list is completed, the built-in rand() function should give you a random number from 0 to at least 32767. Lookup the rand() function using the following link http://www.cplusplus.com/reference/cstdlib/rand/or in your book. Determine which header file you need to include. Your random number generator will need to be 'seeded', by using srand (time, before calling the rand() function. Use the returned random number X to iterate through your singly linked-list X times, and display the winner on the console. Use reasonable member variables for the linked-list struct and give it appropriate names An output of your program should look like the following:

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct node
{
char name[50];
struct node *next;
};

void insert_beg(struct node **tail, char lastName[50])
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
strcpy(temp->name, lastName);
if(*tail == NULL)
{
*tail = temp;
(*tail)->next = *tail;
}
else
{
temp->next = (*tail)->next;
(*tail)->next = temp;
}
}

void displayContestants(struct node *tail, int num)
{
struct node *movingHead;
int i;
movingHead = tail->next;
for(i = 1; i <= num; i++)
{
printf("Contestant [%i]: %s ",i,movingHead->name);
movingHead = movingHead->next;
}
}

void declareWinner(struct node *tail, int randNumber)
{
struct node *movingHead;
int i;
movingHead = tail->next;
for(i = 1; i <= randNumber; i++)
movingHead = movingHead->next;
printf("The lucky winner is: %s ",movingHead->name);
}

int main()
{
struct node *tail = NULL;
int num, randNumber, i;
char lastName[50];
printf("How many contestants: ");
scanf("%i",&num);
for(i = 0; i < num; i++)
{
printf("Last name of contestant[%i]: ",i+1);
scanf("%s",lastName);
insert_beg(&tail, lastName);
}
srand(time(NULL));
randNumber = rand();
displayContestants(tail, num);
declareWinner(tail, randNumber);
}