| | |
Combinations of 0 and 1 for a given number of digits in C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 0
Hello guys,
I ve been searching for a long time for an algorithm (in c++) that would take the number of digits and give all the possible combinations between 0 and 1. For example for number of digits 3 will give
100
010
001
110
101
011
111
The funny thing is, that i think it is simple and well used BUT i cannot find it for days now. Please if anyone could help me in this i would appreciate.
Have a nice day!
Dimitris
I ve been searching for a long time for an algorithm (in c++) that would take the number of digits and give all the possible combinations between 0 and 1. For example for number of digits 3 will give
100
010
001
110
101
011
111
The funny thing is, that i think it is simple and well used BUT i cannot find it for days now. Please if anyone could help me in this i would appreciate.
Have a nice day!
Dimitris
http://en.wikipedia.org/wiki/Permutation
http://regentsprep.org/regents/Math/permut/Lperm.htm
One way, start at zero and increase(i.e.
http://regentsprep.org/regents/Math/permut/Lperm.htm
One way, start at zero and increase(i.e.
++variable; ), till you reaching the target. Last edited by MosaicFuneral; Jan 12th, 2009 at 7:51 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
If n is 1, then the set is {0, 1}.
If you have a set for n, the set for n+1 can be generated by appending a 0 and then a 1 to all elements of the set n.
For example, the set for n = 2 will be 0 + {0, 1} and 1 + {0, 1} which becomes {00, 01} and {10, 11} or (more simply) {00, 01, 10, 11}.
An easy method to implement that would involve recursion.
If you have a set for n, the set for n+1 can be generated by appending a 0 and then a 1 to all elements of the set n.
For example, the set for n = 2 will be 0 + {0, 1} and 1 + {0, 1} which becomes {00, 01} and {10, 11} or (more simply) {00, 01, 10, 11}.
An easy method to implement that would involve recursion.
Last edited by grumpier; Jan 12th, 2009 at 7:58 pm.
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
http://en.wikipedia.org/wiki/Permutation
http://regentsprep.org/regents/Math/permut/Lperm.htm
One way, start at zero and increase(i.e.++variable;), till you reaching the target.
Can you give me an example of what do you mean? Is there any writen code for this?
Thank you again!
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
If n is 1, then the set is {0, 1}.
If you have a set for n, the set for n+1 can be generated by appending a 0 and then a 1 to all elements of the set n.
For example, the set for n = 2 will be 0 + {0, 1} and 1 + {0, 1} which becomes {00, 01} and {10, 11} or (more simply) {00, 01, 10, 11}.
An easy method to implement that would involve recursion.
Dimitris
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Thank you for replying, thats a good approach, i will try to implement it. Do you have a code for this approach btw?
Dimitris
My understanding of Mosaic Funeral's approach is that you would need some helper function that would convert an integer to a string (isolate each binary digit and convert it to a char, then append it to the string). Thus under Mosaic Funeral's approach, there would be no need to store the strings. A helper function could be this:
C++ Syntax (Toggle Plain Text)
string BinaryString (int number, int numDigits) // if passed (14, 4), it would return "1110"
I think Grumpier's approach requires you to take a collection of permutations from n-digits and create all the permutations from n+1 digits from those. So you'd pick whatever storage type you'd want, let's say vector (might not be the best choice, but it could work). A helper function could be this:
C++ Syntax (Toggle Plain Text)
vector <string> permutations (vector <string> nPermutations)
So your function could be passed all eight permutations of 3 digits stored as strings and would return all 16 permutations of 4 digits stored as string, for example.
Anyway, that's my sense of it. I'm not sure whether I correctly discerned their ideas, but hopefully I did. Decide which approach you are more comfortable with and go for it. There are other methods too.
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
We can't give you the full code. One thing you need to decide when you use this approach is what you intend to store and how. Both Grumpier and Mosaic Funeral have approaches that will work, but I think they are different approaches, so I would pick one of them and stick with it rather than take a little from each. That's my take on it.
My understanding of Mosaic Funeral's approach is that you would need some helper function that would convert an integer to a string (isolate each binary digit and convert it to a char, then append it to the string). Thus under Mosaic Funeral's approach, there would be no need to store the strings. A helper function could be this:
C++ Syntax (Toggle Plain Text)
string BinaryString (int number, int numDigits) // if passed (14, 4), it would return "1110"
I think Grumpier's approach requires you to take a collection of permutations from n-digits and create all the permutations from n+1 digits from those. So you'd pick whatever storage type you'd want, let's say vector (might not be the best choice, but it could work). A helper function could be this:
C++ Syntax (Toggle Plain Text)
vector <string> permutations (vector <string> nPermutations)
So your function could be passed all eight permutations of 3 digits stored as strings and would return all 16 permutations of 4 digits stored as string, for example.
Anyway, that's my sense of it. I'm not sure whether I correctly discerned their ideas, but hopefully I did. Decide which approach you are more comfortable with and go for it. There are other methods too.
Thank you all guys for your help.
Dimitris
The true funny thing is that it's a VERY simple "algorithm": make a loop from 0 to 2^n-1 and print the binary representation of the loop counter (a very simple thing too).
Don't worry about n > 32: you can't print (or store) more than all 4 billion combinations for n = 32 in practice. If you want to do it, get 64-bit long long counter (in VC++, for example) and buy disk array with more than 10^21 bytes capacity just before
...
Don't worry about n > 32: you can't print (or store) more than all 4 billion combinations for n = 32 in practice. If you want to do it, get 64-bit long long counter (in VC++, for example) and buy disk array with more than 10^21 bytes capacity just before
... ![]() |
Similar Threads
- [0-9] combination Problem (C++)
- Numbers into Letters (C++)
- C Program to find combinations (C)
- I need help with the following function ::: (C++)
- numbering systems (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: noobish questions
- Next Thread: dark GDK
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






