Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the following C code. Modify the code so that the program asks the user to enter the number of chances for each user/job. Input

Modify the following C code. Modify the code so that the program asks the user to enter the number of chances for each user/job.

Input example:

"Enter the # of chances for user (-1 to exit)" : 200

"Enter # of chances for job (-1 to exit)": 10

"Enter # of chances for job (-1 to exit)": 20

"Enter the # of chances for user (-1 to exit)" : 150

"Enter # of chances for job (-1 to exit)": 15

"Enter # of chances for job (-1 to exit)": 42

#include #include #include #include

// global ticket count int gtickets = 0;

struct node_t { int tickets; struct node_t *next; };

struct node_t *head = NULL;

void insert(int tickets) { struct node_t *tmp = malloc(sizeof(struct node_t)); assert(tmp != NULL); tmp->tickets = tickets; tmp->next = head; head = tmp; gtickets += tickets; }

void print_list() { struct node_t *curr = head; printf("List: "); while (curr) { printf("[%d] ", curr->tickets); curr = curr->next; } printf(" "); }

int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "usage: lottery "); exit(1); } int seed = atoi(argv[1]); int loops = atoi(argv[2]); srandom(seed);

// populate list with some number of jobs, each // with some number of tickets insert(50); insert(100); insert(25);

print_list(); int i; for (i = 0; i < loops; i++) { int counter = 0; int winner = random() % gtickets; // get winner struct node_t *current = head;

// loop until the sum of ticket values is > the winner while (current) { counter = counter + current->tickets; if (counter > winner) break; // found the winner current = current->next; } // current is the winner: schedule it... print_list(); printf("winner: %d %d ", winner, current->tickets);

} return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions