Desgin a lottery draw program using C programming. Implement a Lottery draw prog
ID: 673883 • 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 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 (NULL)), 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>
typedef struct Node
{
char data[255];
struct Node *next;
}node;
void insert(node *pointer, char *data)
{
node *start = pointer;
/* Iterate through the list till we encounter the last node.*/
while(pointer->next!=start)
{
pointer = pointer -> next;
}
/* Allocate memory for the new node and put data in it.*/
pointer->next = (node *)malloc(sizeof(node));
pointer = pointer->next;
strcpy(pointer->data, data);
pointer->next = start;
}
void print(node *start,node *pointer, int i)
{
if(pointer==start)
{
return;
}
printf("Contestant [%d]: %s ", i, pointer->data);
print(start,pointer->next, ++i);
}
int main()
{
/* start always points to the first node of the linked list.
temp is used to point to the last node of the linked list.*/
node *start,*temp;
int n,i,random;
char t[255];
start = (node *)malloc(sizeof(node));
temp = start;
temp -> next = start;
srand(time(NULL));
printf("How many contestants: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Last name of contestant [%d]: ", (i+1));
scanf("%s", t);
insert(start, t);
}
print(start, start->next, 1);
random = (rand() % n);
while (random != 0) {
start = start->next;
random--;
}
printf("The lucky winner is %s", start->data);
}