Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Answer must be in C++ The problem is known as the Josephus problem and postulates a group of people of size N are standing in

Answer must be in C++

The problem is known as the Josephus problem and postulates a group of people of size N are standing in a circle waiting to be eliminated. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of M people are skipped, the next person is eliminated. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains.

For example, suppose that M = 3 and there are N = 5 people named A, B, C, D and E. We count three people starting at A, so that C is eliminated first. We then begin at D and count D, E and back to A, so that A is eliminated next. Then we count B, D and E, and finally B, D and B, so that D is the one who remains last.

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 person from whom the count is to start. After each removal, the program should print the names of all people in the circle until only one person remains. However, to save the printing space, print the names of the remaining people only after K elimination, where K 1 is also an input argument to the program. The input arguments N, M and K can be entered from the stdin in the given order.

Programming Notes:

1. Name the people in the circle in the following sequence: A1, A2 A9, B1, B2 B9, C1, C2 , and start counting from the person A1. Enter the input values N and M when the program prompts for them and use a list container to store the names of N people.

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

void init_vals ( list < string >& L, args& in ): This routine reads the input values N, M and K of the struct args in when the program prompts for them and prints out those values on stdout. You can find the definition of the struct args in the header file prog5.h, which is defined as struct args { unsigned N, M, K; }; The routine prints out those values on stdout, and fills the names of people in the list L.

void print_list ( const list < string >& L, const unsigned& cnt ): This routine prints out the contents of the list L at the beginning and after removing K names (each time) from the list, until only one name remains in the list, where cnt has an initial value 0 and it indicates the total The Josephus Problem 2 number of removals so far. At the end, it also prints the name of the last remaining person. For printout, print only up to 12 names in a single line, where the names are separated by single spaces.

3. The main ( ) routine first calls the routine int_vals ( ) and initializes cnt to 0, and then calls the print_list ( ) to print out the names of people in circle. After that it locates the M-th person in the list, and using the member function erase ( ), it removes that person from the list, and by calling the print_list ( ) prints out the current contents of the list. This process continues (in a loop) until only one person remains in the list. Note: (a) If i (with the initial value 0) indicates the position of a person in the list L, then the statement: j = ( i + M 1 ) % L.size ( ) returns the position of the M-th person from the position i. (b) Since the argument to the erase ( ) function is an iterator, you can convert the index value j to an iterator by the advance ( p, j ) function, where p = L.begin ( ).

4. To store the names in an empty list, first change the size of the list to N, and then use the generate ( ) function in the STL. The last argument to this function is the function object SEQ ( N ), which is defined in the header file prog5.h. To use the header file in your program, insert the following statement at the top of your source file: #include /home/cs340/progs/18f/p5/prog5.h.

For your program, a proper documentation is required, and no global variables are permitted, and for I/O operations, youre not allowed to use the functions in the C library. When your program is ready, submit its source file to your TA, by executing: mail_prog.340 prog5.cc.

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

Students also viewed these Databases questions

Question

Identify three personal human relations goals for the course.

Answered: 1 week ago