- #1
Kingyou123
- 98
- 0
I'm trying to write a program that plays the Josephus Problem.
I'm running into a problem in the remove_nth_member part:
I can't seem to figure out how to write the code so that it refreshes the queue without the member who gets kicked out.
My pseudocode:
1.Move all the names in a temp queue.( did this)
2.Delete the nth member and downsizes the original queue. (stuck on this part)
3.Delete the temp queue
4.Do this till the queue size is 1 and the last name is the survivor.
Links to the full code:
main.cpp = https://gist.github.com/anonymous/862bdd9b679ef03c25df99b7b4cd00eb
queue2.cpp= https://gist.github.com/anonymous/f00c910da6c6cd90859ae234855566a3
queue2.h= https://gist.github.com/anonymous/a90f03aa6d0f56a6fd7601bccb66e40b
I'm running into a problem in the remove_nth_member part:
Code:
string Cqueue2::remove_nth_member(int n) //for Josephus problem to remove a member
{
string name;
int i;
int index;
int start;
name = Queue[(n - 1) % Size];
//we'll be deleteing the soldier at index n%Size
//subtract 1 to account for the fact that indexing starts at 0
start = n%Size;
string * TempQ = new string[Size - 1];
//some looping has to go on here, to move stuff in current queue to temp queue that is smaller
for (i = 0; i < Size; i++)
{
index(Start++i) % Max;
tempQ[i] = Queue[index];
}
//refresh Queue with the member who is kicked out gone
//Change its size
//delete Queue
for (int j = 0; j < Size; j++)
{
Size--;
}
//create a new Queue with new Size
//iterate and move things from tempQ into Queue
//deduct from the Max variable
return name;
}
My pseudocode:
1.Move all the names in a temp queue.( did this)
2.Delete the nth member and downsizes the original queue. (stuck on this part)
3.Delete the temp queue
4.Do this till the queue size is 1 and the last name is the survivor.
Links to the full code:
main.cpp = https://gist.github.com/anonymous/862bdd9b679ef03c25df99b7b4cd00eb
queue2.cpp= https://gist.github.com/anonymous/f00c910da6c6cd90859ae234855566a3
queue2.h= https://gist.github.com/anonymous/a90f03aa6d0f56a6fd7601bccb66e40b