How to program all possibility in an array?

Started by
15 comments, last by taz0010 13 years, 11 months ago
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.
Advertisement
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'.
hello,

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

how to do bitmasking thing?
Quote:how to do bitmasking thing?
Google/search for 'c bit masking', 'c bitwise operators', etc.

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.
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?
Count from 1 to the number you need, then change their base to whatever you need.
Quote:Original post by jyk
Quote:how to do bitmasking thing?
Google/search for 'c bit masking', 'c bitwise operators', etc.

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.
Quote:Original post by lpcstr
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:

<snip>

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.
Quote:Original post by lpcstr
Correct me if I'm wrong, but wouldn't bit masking with logical operators be more of a C approach?
Quote:Original post by helloworld123
I am learning C on my own...
Emphasis added, of course :)

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

This topic is closed to new replies.

Advertisement