answersLogoWhite

0

The following program simulates a queue of people waiting to be served. Initially the queue will have between 3 and 10 customers (selected at random). We then start the main loop where we begin serving the first customer in the queue. A customer can randomly take up to 6 loops to be served (as determined by the serving counter variable). Whilst serving a customer, there's a 1 in 6 chance that a new customer will be added to the queue during each loop. The loop ends when all customers have been served and the queue is empty (thus ending the program).

#include

#include

#include

#include

int main()

{

// random number generator

std::default_random_engine generator;

generator.seed (time(NULL));

// uniform distributions (ranges for generator)

std::uniform_int_distribution initial_customers (1, 10);

std::uniform_int_distribution serving_counter (1, 6);

std::uniform_int_distribution no_add_customer (0, 5);

// seed the queue with up to 10 customers

std::queue q;

unsigned customers = initial_customers (generator);

unsigned customer = 0;

while (customer

q.push (++customer);

// print the current queue status

std::cout << "There are " << q.size() <<

" customers waiting to be served\n" << std::endl;

// initialise the serving counter

unsigned serving = 0;

unsigned current = 0;

do

{

// if we're not currently serving...

if (!serving)

{

// serve the first customer in the queue

current = q.front();

q.pop ();

std::cout << "Serving customer " << current << '\n';

// determine how many loops to serve the current customer (1 to 6 loops)

serving = serving_counter (generator);

}

else

{

// we are currently serving, so decrement the serving counter

--serving;

}

// if the serving count is now zero...

if (!serving)

{

// ...then we're finished with the current customer

std::cout << "Finished serving customer " << current << '\n' << std::endl;

}

else if (!no_add_customer( generator))

{

// 1 in 6 chance a new customer joins the queue

q.push (++customer);

std::cout << "Customer " << customer << " has joined the queue\n";

}

} while (serving or !q.empty());

// Replace the word 'or' with the actual OR symbol in the previous line.

// Note: the OR symbol cannot be shown on this website for some reason.

std::cout << "The queue is empty and " << customer << " customers were served\n" << std::endl;

}

Example Output

There are 10 customers waiting to be served

Serving customer 1

Finished serving customer 1

Serving customer 2

Finished serving customer 2

Serving customer 3

Customer 11 has joined the queue

Finished serving customer 3

Serving customer 4

Customer 12 has joined the queue

Finished serving customer 4

Serving customer 5

Finished serving customer 5

Serving customer 6

Finished serving customer 6

Serving customer 7

Customer 13 has joined the queue

Finished serving customer 7

Serving customer 8

Finished serving customer 8

Serving customer 9

Customer 14 has joined the queue

Finished serving customer 9

Serving customer 10

Finished serving customer 10

Serving customer 11

Customer 15 has joined the queue

Finished serving customer 11

Serving customer 12

Customer 16 has joined the queue

Finished serving customer 12

Serving customer 13

Customer 17 has joined the queue

Customer 18 has joined the queue

Finished serving customer 13

Serving customer 14

Finished serving customer 14

Serving customer 15

Finished serving customer 15

Serving customer 16

Customer 19 has joined the queue

Finished serving customer 16

Serving customer 17

Finished serving customer 17

Serving customer 18

Finished serving customer 18

Serving customer 19

Finished serving customer 19

The queue is empty and 19 customers were served

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

If a juvenile has first degree burglary and is sent to a residential program what would happen if he runs from that program but turns himself in when he turns eighteen years old?

He has not served the full term of his sentence and was an escapee. It is likely that he will be sentenced to to serve the un-served portion of his original sentence, plus whatever the penalty for absconding/escape is.


Where did C plus plus program come from?

C++ is an extension of C, and was invented by Bjarne Stroustrup.


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


Where can one download MSN Plus?

MSN plus is a windows messenger extension. You can download the program for free from MSN. Most windows now come with this program built in but if not visit MSN.


Write a program in c plus plus to compute first of non-terminal?

there is no solution of this problem...........that's it..........


How to restart c plus plus program?

Exit the program and relaunch it.


Can you program games with c plus plus?

Yes, you can program games with C++.


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


Write a program in c plus plus for finding the sum of first 10 even numbers?

int i, sum = 0; for (i=0; i&lt;20; i+=2) sum+=i;


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


What are the release dates for Insight - 1960 Plus Time Served?

Insight - 1960 Plus Time Served was released on: USA: 3 May 1979