Question: Start with a pillbox having 10 empty slots. Your job is to put 10 pills (lettered A, B, C... J) by selecting a slot randomly

Start with a pillbox having 10 empty slots. Your job is to put 10 pills (lettered A, B, C... J) by selecting a slot randomly for each pill until finding an empty slot. For each pill, record the number of attempts it took to find an empty slot for that pill.

Step 1: Include the iostream, iomanip, memory, and ctime libraries.

Step 2: Create a structure named Pill which will hold two variables:

  1. the letter of the pill occupying a slot in the pillbox (A - J)
  2. the number of attempts it took to find an available slot for this pill

The structure also should have a constructor which sets the initial values for each of the variables above.

Step 3: In main, Set the seed of the C++ random number generator to create a different set of numbers each time your program is run.

 srand(time(NULL)) ; 

Step 4: Create an array of 10 unique pointers to Pill structures

 unique_ptr slots[10] ; 

By using unique pointers, we guarantee that each Pill can be referenced by one and only one slot in the pillbox.

Step 5: Create a loop for which processes a single pill (lettered A, B, C... through J).

Step 6: For that pill, generate another loop that creates a random number between 0 and 9. Make sure that you track the number of attempts it takes to find an open slot in the pillbox.

Note: To test if a slot is available, say slot[index], use:

 if (!slot[index]) ... 

Step 6a: If the slot at that index is not available, go back and try another random number, increasing the number of attempts by 1.

Step 6b: If the slot is available, then create a new Pill structure for this pill letter and the number of attempts it took to find an available slot for this pill.

 unique_ptr new_pill(new Pill(pill_letter, attempts)) ; slots[index] = move(new_pill) ;

Notice the use of the move function, instead of using an assignment statement,

Exit the loop generating random numbers, and start looking for a slot for the next pill..

Step 7: When all 10 pills have been placed into a slot in the pillbox, print a report showing:

  • The slot number (0 - 9)
  • The pill letter assigned to that slot (slots[i]->pill_number)
  • The number of attempts it took to find an available slot for that pill letter (slots[i]->attempts)
  • A grand total of the number of attempts

Notice that pill A (the first pill) should take only one attempt, since all ten slots are available when placing that pill. For pill B, the number of attempts should also be a very low number, since 9 of the 10 slots are still available.

However, as the pill numbers climb, so should the number of attempts. For example when trying to place pill J, 9 of the 10 slots are already filled.

Your report might look like the following (the numbers will be different).

 Slot Pill Attempts ---- ---- -------- 0 G 4 1 A 1 2 H 4 3 I 9 4 B 1 5 E 3 6 F 1 7 J 7 8 C 2 9 D 1 Total Attempts 31

Run your program at least twice. Submit your program along with Printscreens show the results of both runs.

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!