| | |
Binary combinations
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Last edited by Freaky_Chris; Jan 8th, 2009 at 4:44 pm.
Knowledge is power -- But experience is everything
You mean
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?
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.
"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: Mar 2008
Posts: 102
Reputation:
Solved Threads: 0
Hi,
following is my code.
i wish to store the combinations in a vector as :
000
001
010
011
100
101
110
111
The problem is my vector is empty when i print it. I am not ale to figure out what is wrong
Thanks
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
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
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
"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: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Sounds like a subset generating problem to me.
I did almost the same thing a while ago, try reading this.
Hope that helps.
I did almost the same thing a while ago, try reading this.
Hope that helps.
Why are you making this so complex???
C++ Syntax (Toggle Plain Text)
vector<char> container; cout << "i = 0; i++ till 255" << endl; for(int i = 0; i < 256; i++) { container.push_back(i); cout << '.'; } cout << endl << "Printing: "; for(int i = 0; i < container.size(); i++) { cout << bitset<numeric_limits<char>::digits>(container[i]) << endl; }
"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: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
![]() |
Similar Threads
- Help needed filling array with unique random numbers (C++)
- PIC Basic programming with RF modules (Assembly)
- Problem in checkboxes set (C#)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Problem copying a .DMG file to a USB hard disk (Windows NT / 2000 / XP)
- Producing Power set (C++)
Other Threads in the C++ Forum
- Previous Thread: reccommend any books?
- Next Thread: ++ Operator Overloading
| 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





