Hi:

I am trying to create a loop for a code. My loop is supposed to find all the combinations using the numbers 1 and 0 depending on the length of my string. For example if I set up my string to be 3 then the program should give me:
000, 001, 010, 100, 011, 101, 110, 111.
After this the code will use my code to calculate the maximum amount of binary numbers for each string. I have completed the second part, the problem I am having is setting up the loop to give me all the possible combinations. Also the problem is that I need to find the combination up to a string of 32 numbers. I can give you what code I have but I am not sure if that will help, I am just looking for input or ideas of how to set up the loop. Thanks for any comments. An of course I am not looking for you to give me a code, because I need to learn to do it, I am just looking for starting points.

Thanks

G.

Hi:

I am trying to create a loop for a code. My loop is supposed to find all the combinations using the numbers 1 and 0 depending on the length of my string. For example if I set up my string to be 3 then the program should give me:
000, 001, 010, 100, 011, 101, 110, 111.
After this the code will use my code to calculate the maximum amount of binary numbers for each string. I have completed the second part, the problem I am having is setting up the loop to give me all the possible combinations. Also the problem is that I need to find the combination up to a string of 32 numbers. I can give you what code I have but I am not sure if that will help, I am just looking for input or ideas of how to set up the loop. Thanks for any comments. An of course I am not looking for you to give me a code, because I need to learn to do it, I am just looking for starting points.

Thanks

G.

if its for 1 and 0 then there is a very simple solution to it. Its as follows:

say your input is 3
the output should be
000, 001, 010, 011, 100, 101, 110, 111
which is actually
0,1,2,3,4,5,6,7 in decimal.
similarly if your input is 4
the outputs will be binary of
0,1,2,.....15 i.e. 0-(2^4-1)


so my suggestion is something like

/*'length' wil have the length of string*/
int i;
for(i=0; i<pow(2, length);i++)
       showInBinary(i, length);

/*show binary method*/
void showInBinary(int num, int len)
{
      /* display the num in binary. if the length of the binary num is less than 'len' then precede it with as many 0's so that the length  becomes equal to 'len' */
}

this was a solution if the available symbols are 0 and 1 only.
But this wont work for other symbols.

This article has been dead for over six months. Start a new discussion instead.