Re: mathematical combination



On Nov 21, 9:44 pm, keny...@xxxxxxxxx wrote:
Hi!
I'm new to C++ but I have a challenge to write a program to find the
different mathematical combinations of p and t objects respectively
from a total of n objects i.e. nCp and nCt.The program should display
the various combinations.
A further aspect is to find the intersection between nCp and nCt
i.e.each set of nCp that contains each of the possible combinations of
nCt without considering the order of appearance.
Let me try to be clearer,pls:
say n=7,p=5 and t=3
1. I want a program to find 7C5 and 7C3
2. I want a display of the possible combinations in each case i.e
for 7C5; (12345),(12346),(12347),(13456)etc and
for 7C3; (123),(124),(125),(126)etc
3. Finally, the intersection e.g.
(123) intersects (12345),(12346),...
(124) intersects (12345),(12346)...
(125) intersects (12345)
and so on.
Your help will be appreciated.Thanks.

Try this:

class Committees {
public:
int operator [] (int index) {
assert(0 <= index && index < int(_array.size()));
return _array[index];
}

bool next() {
int pIndex = _array.size() - 1;
while (pIndex >= 0) {
if (_array[pIndex] < int(_total - _array.size() + pIndex)) {
_array[pIndex]++;
for (size_t i = pIndex + 1; i < _array.size(); i++) {
_array[i] = _array[i - 1] + 1;
}
return true;
}
pIndex--;
}
return false;
}

Committees(int total, int size) : _total(total), _array(size) {
assert(size <= total);
for (size_t i = 0; i < _array.size(); i++ ) {
_array[i] = i;
}
}
private:
int _total;
vector<int> _array;
};
.



Relevant Pages

  • mathematical combination
    ... A further aspect is to find the intersection between nCp and nCt ... I want a display of the possible combinations in each case i.e ... intersects,,... ...
    (comp.programming)
  • Re: mathematical combination
    ... A further aspect is to find the intersection between nCp and nCt ... I want a display of the possible combinations in each case i.e ... intersects,,... ... Look up "Pascal's Triangle" for a table lookup approach to the nCr ...
    (comp.programming)