Josephus Circle Help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Josephus Circle Help

lazy86lazy86 Posts: 5Member
I've typed the program and all, but i just need help with changing the input to where my command line is like this:

./a.out n m p

n=number of players

m=count used for every odd turn

p= count used for every even turn

help would be greatly appreciated thank you


here's the code:


#include
#include
using namespace std;

struct node {
int key;
node *next;};

int main(int argc, char* argv[]){
if(argc >2) {
int i, N = atoi(argv[1]), M = atoi(argv[2]);
node *t, *x;
t = new node;
t->key = 1;
x = t;
for(i = 2; i<=N; i++){
t->next = new node;
t = t->next;
t->key = i;
}
t->next = x;
while(t != t->next){
for(i = 1; inext;
cout << t->next->key<<" ";
x = t->next;
t->next = x->next;
delete x;
}
cout << t->key << endl;
}
return 0;
}

Comments

  • stephlstephl Posts: 422Member
    : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    :
    : ./a.out n m p
    :
    : n=number of players
    :
    : m=count used for every odd turn
    :
    : p= count used for every even turn
    :
    : help would be greatly appreciated thank you
    :
    :
    : here's the code:
    :
    :
    : #include
    : #include
    : using namespace std;
    :
    : struct node {
    : int key;
    : node *next;};
    :
    : int main(int argc, char* argv[]){
    : if(argc >2) {
    : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : node *t, *x;
    : t = new node;
    : t->key = 1;
    : x = t;
    : for(i = 2; i<=N; i++){
    : t->next = new node;
    : t = t->next;
    : t->key = i;
    : }
    : t->next = x;
    : while(t != t->next){
    : for(i = 1; inext;
    : cout << t->next->key<<" ";
    : x = t->next;
    : t->next = x->next;
    : delete x;
    : }
    : cout << t->key << endl;
    : }
    : return 0;
    : }
    :
    :
    Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.

    Steph
  • lazy86lazy86 Posts: 5Member
    : : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    : :
    : : ./a.out n m p
    : :
    : : n=number of players
    : :
    : : m=count used for every odd turn
    : :
    : : p= count used for every even turn
    : :
    : : help would be greatly appreciated thank you
    : :
    : :
    : : here's the code:
    : :
    : :
    : : #include
    : : #include
    : : using namespace std;
    : :
    : : struct node {
    : : int key;
    : : node *next;};
    : :
    : : int main(int argc, char* argv[]){
    : : if(argc >2) {
    : : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : : node *t, *x;
    : : t = new node;
    : : t->key = 1;
    : : x = t;
    : : for(i = 2; i<=N; i++){
    : : t->next = new node;
    : : t = t->next;
    : : t->key = i;
    : : }
    : : t->next = x;
    : : while(t != t->next){
    : : for(i = 1; inext;
    : : cout << t->next->key<<" ";
    : : x = t->next;
    : : t->next = x->next;
    : : delete x;
    : : }
    : : cout << t->key << endl;
    : : }
    : : return 0;
    : : }
    : :
    : :
    : Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.
    :
    : Steph
    :

    the purpose of this program is to simulate a game where there is a certain number players, and in each round a player is eliminated depending on the count. So if the count is 3 then every third person is eliminated
  • stephlstephl Posts: 422Member
    : : : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    : : :
    : : : ./a.out n m p
    : : :
    : : : n=number of players
    : : :
    : : : m=count used for every odd turn
    : : :
    : : : p= count used for every even turn
    : : :
    : : : help would be greatly appreciated thank you
    : : :
    : : :
    : : : here's the code:
    : : :
    : : :
    : : : #include
    : : : #include
    : : : using namespace std;
    : : :
    : : : struct node {
    : : : int key;
    : : : node *next;};
    : : :
    : : : int main(int argc, char* argv[]){
    : : : if(argc >2) {
    : : : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : : : node *t, *x;
    : : : t = new node;
    : : : t->key = 1;
    : : : x = t;
    : : : for(i = 2; i<=N; i++){
    : : : t->next = new node;
    : : : t = t->next;
    : : : t->key = i;
    : : : }
    : : : t->next = x;
    : : : while(t != t->next){
    : : : for(i = 1; inext;
    : : : cout << t->next->key<<" ";
    : : : x = t->next;
    : : : t->next = x->next;
    : : : delete x;
    : : : }
    : : : cout << t->key << endl;
    : : : }
    : : : return 0;
    : : : }
    : : :
    : : :
    : : Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.
    : :
    : : Steph
    : :
    :
    : the purpose of this program is to simulate a game where there is a certain number players, and in each round a player is eliminated depending on the count. So if the count is 3 then every third person is eliminated
    :
    OK, thanks for the explanation. Here follows what I understood.
    The program should create a circular linked list with each node containing 1,2,3,... up to the number of players. Is that right?
    What I do not understand is who provides the count which indicates the players eliminated (3 in your example)? And what is the purpose of your m and p parameters?
    May you be a bit clearer on what the program should do?

    Steph
  • lazy86lazy86 Posts: 5Member
    : : : : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    : : : :
    : : : : ./a.out n m p
    : : : :
    : : : : n=number of players
    : : : :
    : : : : m=count used for every odd turn
    : : : :
    : : : : p= count used for every even turn
    : : : :
    : : : : help would be greatly appreciated thank you
    : : : :
    : : : :
    : : : : here's the code:
    : : : :
    : : : :
    : : : : #include
    : : : : #include
    : : : : using namespace std;
    : : : :
    : : : : struct node {
    : : : : int key;
    : : : : node *next;};
    : : : :
    : : : : int main(int argc, char* argv[]){
    : : : : if(argc >2) {
    : : : : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : : : : node *t, *x;
    : : : : t = new node;
    : : : : t->key = 1;
    : : : : x = t;
    : : : : for(i = 2; i<=N; i++){
    : : : : t->next = new node;
    : : : : t = t->next;
    : : : : t->key = i;
    : : : : }
    : : : : t->next = x;
    : : : : while(t != t->next){
    : : : : for(i = 1; inext;
    : : : : cout << t->next->key<<" ";
    : : : : x = t->next;
    : : : : t->next = x->next;
    : : : : delete x;
    : : : : }
    : : : : cout << t->key << endl;
    : : : : }
    : : : : return 0;
    : : : : }
    : : : :
    : : : :
    : : : Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.
    : : :
    : : : Steph
    : : :
    : :
    : : the purpose of this program is to simulate a game where there is a certain number players, and in each round a player is eliminated depending on the count. So if the count is 3 then every third person is eliminated
    : :
    : OK, thanks for the explanation. Here follows what I understood.
    : The program should create a circular linked list with each node containing 1,2,3,... up to the number of players. Is that right?
    : What I do not understand is who provides the count which indicates the players eliminated (3 in your example)? And what is the purpose of your m and p parameters?
    : May you be a bit clearer on what the program should do?
    :
    : Steph
    :


    ok in the code I provided N is the number of players and M is the count you use, they're both input in the command line for example:

    ./a.out 7 3

    with 7 being the number of players and 3 being the count
  • stephlstephl Posts: 422Member
    : : : : : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    : : : : :
    : : : : : ./a.out n m p
    : : : : :
    : : : : : n=number of players
    : : : : :
    : : : : : m=count used for every odd turn
    : : : : :
    : : : : : p= count used for every even turn
    : : : : :
    : : : : : help would be greatly appreciated thank you
    : : : : :
    : : : : :
    : : : : : here's the code:
    : : : : :
    : : : : :
    : : : : : #include
    : : : : : #include
    : : : : : using namespace std;
    : : : : :
    : : : : : struct node {
    : : : : : int key;
    : : : : : node *next;};
    : : : : :
    : : : : : int main(int argc, char* argv[]){
    : : : : : if(argc >2) {
    : : : : : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : : : : : node *t, *x;
    : : : : : t = new node;
    : : : : : t->key = 1;
    : : : : : x = t;
    : : : : : for(i = 2; i<=N; i++){
    : : : : : t->next = new node;
    : : : : : t = t->next;
    : : : : : t->key = i;
    : : : : : }
    : : : : : t->next = x;
    : : : : : while(t != t->next){
    : : : : : for(i = 1; inext;
    : : : : : cout << t->next->key<<" ";
    : : : : : x = t->next;
    : : : : : t->next = x->next;
    : : : : : delete x;
    : : : : : }
    : : : : : cout << t->key << endl;
    : : : : : }
    : : : : : return 0;
    : : : : : }
    : : : : :
    : : : : :
    : : : : Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.
    : : : :
    : : : : Steph
    : : : :
    : : :
    : : : the purpose of this program is to simulate a game where there is a certain number players, and in each round a player is eliminated depending on the count. So if the count is 3 then every third person is eliminated
    : : :
    : : OK, thanks for the explanation. Here follows what I understood.
    : : The program should create a circular linked list with each node containing 1,2,3,... up to the number of players. Is that right?
    : : What I do not understand is who provides the count which indicates the players eliminated (3 in your example)? And what is the purpose of your m and p parameters?
    : : May you be a bit clearer on what the program should do?
    : :
    : : Steph
    : :
    :
    :
    : ok in the code I provided N is the number of players and M is the count you use, they're both input in the command line for example:
    :
    : ./a.out 7 3
    :
    : with 7 being the number of players and 3 being the count
    :
    I cannot figure out what your problem is. At first sight, I don't see any mistake in your code, except the fact that you do not free the last node which remains after the while loop.
    Here is an example of what I get:
    [code]
    D:DataCTemp>temp 7 3
    3 6 2 7 5 1 4
    [/code]

    Isn't it what you want?

    Steph
  • lazy86lazy86 Posts: 5Member
    : : : : : : I've typed the program and all, but i just need help with changing the input to where my command line is like this:
    : : : : : :
    : : : : : : ./a.out n m p
    : : : : : :
    : : : : : : n=number of players
    : : : : : :
    : : : : : : m=count used for every odd turn
    : : : : : :
    : : : : : : p= count used for every even turn
    : : : : : :
    : : : : : : help would be greatly appreciated thank you
    : : : : : :
    : : : : : :
    : : : : : : here's the code:
    : : : : : :
    : : : : : :
    : : : : : : #include
    : : : : : : #include
    : : : : : : using namespace std;
    : : : : : :
    : : : : : : struct node {
    : : : : : : int key;
    : : : : : : node *next;};
    : : : : : :
    : : : : : : int main(int argc, char* argv[]){
    : : : : : : if(argc >2) {
    : : : : : : int i, N = atoi(argv[1]), M = atoi(argv[2]);
    : : : : : : node *t, *x;
    : : : : : : t = new node;
    : : : : : : t->key = 1;
    : : : : : : x = t;
    : : : : : : for(i = 2; i<=N; i++){
    : : : : : : t->next = new node;
    : : : : : : t = t->next;
    : : : : : : t->key = i;
    : : : : : : }
    : : : : : : t->next = x;
    : : : : : : while(t != t->next){
    : : : : : : for(i = 1; inext;
    : : : : : : cout << t->next->key<<" ";
    : : : : : : x = t->next;
    : : : : : : t->next = x->next;
    : : : : : : delete x;
    : : : : : : }
    : : : : : : cout << t->key << endl;
    : : : : : : }
    : : : : : : return 0;
    : : : : : : }
    : : : : : :
    : : : : : :
    : : : : : Please, may you explain what the purpose of the program is? I do not know what Josephus Circle is.
    : : : : :
    : : : : : Steph
    : : : : :
    : : : :
    : : : : the purpose of this program is to simulate a game where there is a certain number players, and in each round a player is eliminated depending on the count. So if the count is 3 then every third person is eliminated
    : : : :
    : : : OK, thanks for the explanation. Here follows what I understood.
    : : : The program should create a circular linked list with each node containing 1,2,3,... up to the number of players. Is that right?
    : : : What I do not understand is who provides the count which indicates the players eliminated (3 in your example)? And what is the purpose of your m and p parameters?
    : : : May you be a bit clearer on what the program should do?
    : : :
    : : : Steph
    : : :
    : :
    : :
    : : ok in the code I provided N is the number of players and M is the count you use, they're both input in the command line for example:
    : :
    : : ./a.out 7 3
    : :
    : : with 7 being the number of players and 3 being the count
    : :
    : I cannot figure out what your problem is. At first sight, I don't see any mistake in your code, except the fact that you do not free the last node which remains after the while loop.
    : Here is an example of what I get:
    : [code]
    : D:DataCTemp>temp 7 3
    : 3 6 2 7 5 1 4
    : [/code]
    :
    : Isn't it what you want?
    :
    : Steph
    :
    sorry for long wait on the reply...but i finally got the code the way i needed it...appreciate the help though.

Sign In or Register to comment.