943,681 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1757
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 8th, 2009
0

Binary combinations

Expand Post »
Hi,

I wish to generate all the binary combinations for an input size n. So, the number of combinations in this case would be (2^n) and if n=2 my output should be :

00
01
10
11

How can i do it? What would be the best way?

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
guest7 is offline Offline
109 posts
since Mar 2008
Jan 8th, 2009
0

Re: Binary combinations

Chris

edit: just add one to the number lol and loop through??
Last edited by Freaky_Chris; Jan 8th, 2009 at 4:44 pm.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 8th, 2009
0

Re: Binary combinations

You mean varible++; , or variable += 1; ?
Perhaps you could go to the extreme of doing it with bit operators(I have for fun), or using the inline assembler to do it. So many options, but which is right for you?
Last edited by MosaicFuneral; Jan 8th, 2009 at 5:19 pm.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 8th, 2009
0

Re: Binary combinations

hi,

could you give me an example of both bit operators and inline assembler.

Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster
guest7 is offline Offline
109 posts
since Mar 2008
Jan 8th, 2009
0

Re: Binary combinations

Hi,

following is my code.

i wish to store the combinations in a vector as :

000
001
010
011
100
101
110
111
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int binary(int, vector<int> &);
void print_vec();

int n = 4;	
int comb = pow(2,n)-1;

std::vector<int> ros(comb);
std::vector<vector<int> > col(n);
	
int main() {
	int number;
	cout << comb <<"\n";
	
	for(int k=0; k<=comb; k++)
	{
		number=k;
		if (number < 0) 
			cout << "That is not a positive integer.\n";
		else 
		{
			//cout << number <<" converted to binary is: ";
			int count1=binary(number, ros);
			while(count1<n)
			{
				ros.push_back(0);
				count1++;
			}
			col.push_back(ros);
			ros.clear();
			cout << endl;
		}
	}
	void print_vect();
	return 0;
}

int binary(int number, vector<int> &ros1) 
{
	
	int remainder,count=1;
	if(number <= 1) 
	{
		count = 0;
		ros1.push_back(number);
		//cout << number;
		count++;
	}
	else
	{
		/* There is a right shift operator */
		remainder = number%2;
		count++;
		binary(number >> 1, ros1);    
		//cout << remainder;
		ros1.push_back(remainder);
	}
	return count;
}

void print_vec()
{
	for(int it=0; it<col.size(); it++)
	{
		for(int itt=0; itt<col[it].size(); itt)
		{
			fprintf(stderr,"Here = %d",col[it][itt]);
		}
	}	
}

The problem is my vector is empty when i print it. I am not ale to figure out what is wrong

Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster
guest7 is offline Offline
109 posts
since Mar 2008
Jan 8th, 2009
0

Re: Binary combinations

Look up "C++ bit operations"

Inline assembler is platform and compiler specific.
http://www.penguin.cz/~literakl/intel/intel.html
http://www.ibiblio.org/gferg/ldp/GCC...bly-HOWTO.html
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 8th, 2009
0

Re: Binary combinations

if would be great if u can post an example
Reputation Points: 10
Solved Threads: 0
Junior Poster
guest7 is offline Offline
109 posts
since Mar 2008
Jan 8th, 2009
0

Re: Binary combinations

Sounds like a subset generating problem to me.

I did almost the same thing a while ago, try reading this.

Hope that helps.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jan 8th, 2009
0

Re: Binary combinations

Why are you making this so complex???

C++ Syntax (Toggle Plain Text)
  1. vector<char> container;
  2.  
  3. cout << "i = 0; i++ till 255" << endl;
  4.  
  5. for(int i = 0; i < 256; i++)
  6. {
  7. container.push_back(i);
  8. cout << '.';
  9. }
  10.  
  11. cout << endl << "Printing: ";
  12.  
  13. for(int i = 0; i < container.size(); i++)
  14. {
  15. cout << bitset<numeric_limits<char>::digits>(container[i]) << endl;
  16. }
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 9th, 2009
0

Re: Binary combinations

Are you pushing ints into a vector, or strings? If your pushing 010 into a vector of ints your going to have issues. You're going to need to use stringstream to push numbers into a string then push those onto a vector. The binary numbers themselves can be generated with really simple for loop constructs, as forementioned.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
773 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reccommend any books?
Next Thread in C++ Forum Timeline: ++ Operator Overloading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC