Hi, I have a problem which has been puzzling me for a few days. i have been trying it with vectors and characters but will not work for me. Imagine an "ON" "OFF" binary sequence. I want to compute all combinations of that sequence of length N say.

e.g sequence 001011 of length 6, but want all possibilities arising of length 6

I was thinking about using the <BITSET> class and defining for example

std::bitset<100>

Is this the best way for a sequence of "ON" "OFF" code? Then from here what is the best way to make sure all the possibilities of the sequence can be generated?


(( On a side note, I can't have for instance "std::bitset<N>" where N is user defined or defined elsewhere. The error is 'expression must have a constant value' so I think its something to do with address?))

Thanks for your time in reading if you have gotten this far.

Here's one way. You'll have to translate my prose into code.

1) Start with a sequence of 0 bits.
2) Repeat steps 3-5 until the sequence is all 1-bits.
3) Locate the leftmost 0 bit. (This works because the sequence
is known not to be all 1-bits)
4) Set that bit to 1.
5) Set all the bits to the left of that bit to 0.

For example, if you have 11011000, the leftmost 0 bit is the third from the left. So you set that bit to 0 and the ones to its left to 1 and you get 00111000.

Each time you go through steps 3-5, you will get a different set of bits. Try it and see.

commented: Nice solution =) +1

If you use a char array then you can use a sequence of nested loops, wherein each char is associated with a loop, to change each place holder in the sequence in turn. Haven't used bitsets so can't say if similar manipulation possible with them.

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.