Answered # combinations of an integer

mike_2000_17 2,669 Discussion Starter frogboy77 73 mike_2000_17 2,669 Discussion Starter frogboy77 73 Featured Reply NathanOliver 429 Discussion Starter frogboy77 73 NathanOliver 429 Discussion Starter frogboy77 73 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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).

0

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

0

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.

0

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

1

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
```

0

thanks gonna give that a try

still feel i should know what its doing though

one thing at a time i suppose

0

here is a good link to how next_permutation works. http://marknelson.us/2002/03/01/next-permutation

0

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 :)

This question has already been answered. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...