Jump to content
  • Advertisement
Sign in to follow this  
helloworld123

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!