How would I go about modifying this code to accept characters instead of integers?

#include <iostream>
#include <cstdlib>
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; i<M; i++)
        t = t->next;
        cout << t->next->key<<" has dropped out "<< endl;
        x = t->next;
        t->next = x->next;
        delete x;
    }
    cout << "winner is " << t->key << endl;
    }
    return 0;
}

Recommended Answers

All 10 Replies

Don't understand the question. Where do you want it to accept characters? In the command-line arguments (argv), in the node structure? And what characters do you want it to use?

I would like for the program to accept the characters in the command line argument. The program currently accepts integers in the command line and proceeds with the josephus circle algorithm. When I run the program from the command line the first integer I enter sets the number of players, the second integer sets the count for the number of moves it will make before it removes the one it lands on.

So why do you want characters?

They're the instructions given to me.

what characters do you want it to accept? What are those characters supposed to represent?

I suspect you may have misunderstood the assignment. Post the exact wording of the assignment that asks you about characters.

Write a program to solve the Josephus problem(posted above), with the following
modification:

Sample Input:

./a.out n m p

where n is the number of players and m is the count used for every odd
turn while p is the count used for every even turn.

./a.out 5 2 3

Sample Output:

Round 1:

1 -> 3 -> 4 -> 5

Round 2:

1 -> 3 -> 4

Round 3:

1 -> 4

Round 4:

1

Winner is 1.

That is not asking you to enter characters, n m and p are just place holders for the three integers as explained in the instructions. The program you posted is missing the third argument -- p -- which you need to implement.

int i, N = atoi(argv[1]), M = atoi(argv[2]), P = atoi(argv[3]); right?

what line would I increment P?

Would I need a seperate while loop for the p argument since P is the count used for every even turn

#include <iostream>
#include <cstdlib>
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]), P = atoi(argv[3]);
    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;
    int count = 1;
    while(t != t->next){
        if(count%2 == 1){
        for(int i = 1; i < M; i++)
            t = t->next;
        }

        if(count%2 == 0){
            for(int i = 1; i < P; i++)
            t = t-> next;
        }
        cout << t->next->key<<" has dropped out "<< endl;
        x = t->next;
        t->next = x->next;
        delete x;
        count++;
    }
    cout << "winner is " << t->key << endl;
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.