# How to program all possibility in an array?

This topic is 3003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Sorry noob question (started learning programming on my own last week). I am learning C on my own, and want to do this as personal practice, but couldn't figure it out. I wish to have a file and populate it with this: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 (which is all combination that is possible with 0 and 1 as values). how do I go about it? thank you.

##### Share on other sites
I'm assuming this is just an exercise you're doing and not an actual homework problem...

Anyway, here's a hint. What you've shown there are the binary representations of the integers in the range [0, 15]. So, you can generate those patterns by iterating over the integers in that range and using bitmasking to determine, for each, which bits are set to '1' and which bits are set to '0'.

##### Share on other sites
hello,

no it's not homework, i'm studying programming on my own as hobby.

##### Share on other sites
Quote:

There are other ways you could generate that sequence as well, such as a loop or set of (possibly implied) nested loops that iterated over all of the permutations.

##### Share on other sites
hello,

if i use loops within loops, for the sample above, i would need 4 loops right?

what if i need to populate something with 10 consecutive numbers (instead of the 4 numbers above where possible values are 0 and 1), then i need 10 loops? it's not efficient i think?

##### Share on other sites
Count from 1 to the number you need, then change their base to whatever you need.

##### Share on other sites
Quote:
Original post by jyk
Quote:

There are other ways you could generate that sequence as well, such as a loop or set of (possibly implied) nested loops that iterated over all of the permutations.

Correct me if I'm wrong, but wouldn't bit masking with logical operators be more of a C approach? In C++ I think you would define a union that contains an integer and a bit field structure.

Something like:

struct Bitfield{	bool a : 1;	bool b : 1;	bool c : 1;	bool d : 1;	bool e : 1;	bool f : 1;	bool g : 1;	bool h : 1;};union ByteToBits{	char Byte;	Bitfield Bits;};

Or at least something to that effect.

##### Share on other sites
Quote:
 Original post by lpcstrCorrect me if I'm wrong, but wouldn't bit masking with logical operators be more of a C approach? In C++ I think you would define a union that contains an integer and a bit field structure.Something like:Or at least something to that effect.

Actually that would still be C. In C++ you'd make heavy use of templates just because you can:

template<size_t N>class ToBinary {};template<size_t N>std::ostream& operator<<(std::ostream& str, const ToBinary<N>&){	str << ToBinary<N/2>();	str << (N & 1) ? '1' : '0';	return str;}template<>std::ostream& operator<<(std::ostream& str, const ToBinary<0>&){	return str;}template<size_t N>class RangeToBinary {};template<size_t N>std::ostream& operator<<(std::ostream& str, const RangeToBinary<N>&){	str << RangeToBinary<N-1>();	str << ToBinary<N>() << std::endl;	return str;}template<>std::ostream& operator<<(std::ostream& str, const RangeToBinary<0>&){	return str;}int main(){	std::cout << RangeToBinary<500>();}

Note: The larger your range, the angrier your compiler gets as it has to expand all the templates :) You could also modify the code a bit to add zero padding but meh.

##### Share on other sites
Quote:
 Original post by lpcstrCorrect me if I'm wrong, but wouldn't bit masking with logical operators be more of a C approach?
Quote:
 Original post by helloworld123I am learning C on my own...

Anyway, the bitmasking approach is just one way of doing what the OP asked; it's not necessarily the 'best' or most elegant way. (Personally, I'd probably use a loop, which I also suggested.)

##### Share on other sites
You can also use backtracking, which is a mechanism that can be adapted for many other enumeration problems.

#include <stdio.h>int array[4];void backtrace(int n) {  if (n==4) {    for (int i=0; i<4; ++i)      printf("%d",array);    printf("\n");  }  else {    for (array[n]=0; array[n]<2; ++array[n])      backtrace(n+1);  }}int main() {  backtrace(0);}

1. 1
2. 2
Rutin
22
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631735
• Total Posts
3001940
×