Hi, total beginner here, trying to learn at home.
Just thought i'd get that in.

if i have a number eg 123
trying to figure out a way of calculating all the possible cominations
123
132
213
231
312
321

can do it with pencil and paper but don't know where to start with program
can anyone help?

Recommended Answers

All 8 Replies

Well, as a start, you need to split the number into each base 10 digit, for example:

std::vector<int> SplitDigits(int Number) {
  std::vector<int> result;
  while(Number > 0) {
    result.push_back(Number % 10);
    Number /= 10;
  };
  return result;
};

That's a start, now you go from there and figure out how to construct each possible combination (hint: use recursion, think of how a factorial is calculated).

not being funny but thats the part of the prblem i knew how to do.
if i have it correct recursion is a function calling itself
have written a factorial program before but just used a while loop

Think the problem here is not telling the computer how to answer my question
the problem is i dont know what the question is

Here the recursion idea is that you start with a vector of digits v_in to pick from and an empty vector of digits v_out where a combination can be put. Then at this point any one of the digits you have in v_in can be placed in the next slot of v_out to start a new combination. So you can loop for every digit in v_in, add it with v_out to get v_next, copy all the other digits of v_in into v_remain, and recursively call back the same function with v_next -> v_out and v_remain -> v_in. Once there is no more digits in v_in, you should have a full combination, and you just print it out with cout.

thanks for the help but think this is just beyond me at the moment
need to do some more reading i think
was following some of your explanation but go lost
need to learn more about recursion and functions
THANKS FOR THE HELP

you can always use the next_permutation function located in <algorithm>. As mike said you will need to break up then number into each digit and put them into some kind of container. Vectors work well for this. then you will need to sort the container to have the smallest digit at the front of the array. Then you can use a counter variable and a while statement to add up all of the permutations you can get.

int counter = 0;
vector<int> container; // put each digit here
// sort the vector
while(next_permutation(container.begin(), container.end())
{
    counter++;
}
// now counter has the number of permutations

thanks gonna give that a try
still feel i should know what its doing though
one thing at a time i suppose

thanks guys
next_permutation did the job nicely
now gonna go and find out how it works
feel like i should know how to write my own algorithms
this might slow my learning but hate using something i dont understand
just me being me :)

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.