Question: C++ to remove every 'x' numbers of elements in a queue until 1 is left for example: During the Civil War, a group of Confederate

C++ to remove every 'x' numbers of elements in a queue until 1 is left for example:

During the Civil War, a group of Confederate officers are in a clandestine meeting when they suddenly discover they are surrounded by an overwhelming Union force. There is absolutely no hope for victory without reinforcements, and Stonewall Jackson's horse, Little Sorrel, is the only horse available that can be used to escape and summon help. The officers agree to a pact to determine which of them is to go for help.

The officers form a queue and they pick a number from a hat. Beginning with the first officer in the queue, they begin to count. As each officer counts off, he moves to the end of the queue. When the count reaches the number that was picked, that officer is removed from the queue, and the count begins again with the next man. Once an officer is removed from the queue, he is no longer counted. The last officer that remains in the queue is the one that rides away on Little Sorrel to summon help.

For example, let's suppose the following officers are stored in the queue: Beauregard, Pierre Gustave Toutant, General Bragg, Braxton, General Early, Jubal Anderson, Lieutenant General Ewell, Richard Stoddert, Lieutenant General Forrest, Nathan Bedford, Lieutenant General Hood, John Bell, General Jackson, Thomas Jonathan "Stonewall", Major General Johnston, Joseph Eggleston, General Lee, Robert Edward, General Longstreet, James, Lieutenant General Pickett, George Edward, Brigadier General Stuart, James Ewell Brown "JEB", General Van Dorn, Earl, Major General

Let's also suppose the number picked from the hat is 4. So the officers begin counting off and moving to the back of the queue:

1-Beauregard, 2-Bragg, 3-Early

Next is Ewell, and he is 4. Since that is the number drawn from the hat, he walks away (i.e., he is not put back into the queue), and he will not be the officer that goes for help.

Now, we begin counting again:

1-Forrest, 2-Hood, 3-Jackson

Johnston is 4, so he also walks away. We begin counting again

1-Lee, 2-Longstreet, 3-Pickett

Stuart is 4, so he walks away. The queue currently looks like this: Van Dorn, Earl, Major General Beauregard, Pierre Gustave Toutant, General Bragg, Braxton, General Early, Jubal Anderson, Lieutenant General Forrest, Nathan Bedford, Lieutenant General Hood, John Bell, General Jackson, Thomas Jonathan "Stonewall", Major General Lee, Robert Edward, General Longstreet, James, Lieutenant General Pickett, George Edward, Brigadier General

The process continues in this way until only one officer remains in the queue. The last officer remaining in the queue is the one that goes for help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!