Question: For this computer assignment, you are to write and implement a C++ program to simulate and solve the Josephus problem. The input to the program

For this computer assignment, you are to write and implement a C++ program to simulate and solve the Josephus problem. The input to the program is the number M and a list of N names, which is clockwise ordering of the circle, beginning with the soldier from whom the count starts. After each removal, the program should print the name tags of all soldiers in the circle until only one soldier remains. However, to save the printing space, print the name tags for the remaining soldiers after only K elimination where K >= 1 is also an input to the program. The input arguments N, M, and K can be entered from stdin in the given order.

1. Name the soldiers in a circle in the following sequence: A1, A2, , A9, B1, B2, , B9, C1, C2, Enter the input arguments when the program prompts for them, and use a vector < string > to store the name tags of N soldiers.

2. In addition to the main ( ) routine, implement the following subroutines in your program:

void init_vals ( vector < string >& v, inargs& in ) : It reads the input values of N, M, and K of the struct object in when the program prompts for them and prints out those values on stdout. You can find the definition of the struct inargs in the header file prog5.h, which is defined as struct inargs { unsigned N, M, K; }; This routine also changes the size of the vector v to N and fills in the name tags for all soldiers in v.

void print_vector ( const vector < string >& v, const unsigned& cnt ) : It prints out the contents of the vector v at the beginning and after removing K name tags from the vector until only one name tag remains, where cnt has an initial value of 0 and it simply indicates the total number of removals so far. At the end, it also prints the name tag of the last remaining soldier. For printout, print only up to 12 name tags in a single line, where the name tags are separated by single spaces.

3. The main ( ) routine first calls init_vals ( ) and initializes cnt to 0, and then calls the print_vector ( ) to print out the names of soldiers in circle. After that it locates the M-th name tag in the vector, and using the member function erase ( ), it removes that name tag from the vector, and calling print_vector ( ) to print out the contents of the vector. This process continues (in a loop) until only one name tag remains in the vector. Note: (a) If i (with the initial value 0) indicates the position of the soldier in the vector, then the statement ( i + M 1 ) % v.size ( ) returns the position of the Mth soldier from the starting position i. (b) Since the input argument to the erase ( ) function is an iterator, you can convert an index value i to an iterator by the statement: v.begin ( ) + i.

To store the name tags in an empty vector, first change the size of the vector to N, and then use the function generate ( ) in the STL. The last argument of this function is the function object SEQ ( N ), which is defined in the header file prog5.h.

Please sepearte program into a header file (prog5.h) and prog5.cc

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!