473,466 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Counting down an integer vector from the highest degree (111) to the lowest degree (000)

Anyone who can help,

I am curretnly attempting to write some code that will allow iteration
using a vector<intfrom the highest possilbe degree of a combination
of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering
of element containing the same number of ones is not important to the
code, just the fact that the highest number of ones are iterated first.
I would prefer not to use a binary string as eventually the
combinations will change to combination of any numbers (0,1,2,3,etc.)

I have looked at several implementation of Gray Codes, but have not
been able to figure out a way to complete this iteration cleanly.

Any help or suggestions on where else to look would be greatly
appreciated.

Thanks

Matt

Nov 15 '06 #1
6 1971

Matt Chwastek wrote in message
<11**********************@k70g2000cwa.googlegroups .com>...
>Anyone who can help,

I am curretnly attempting to write some code that will allow iteration
using a vector<intfrom the highest possilbe degree of a combination
of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering
of element containing the same number of ones is not important to the
code, just the fact that the highest number of ones are iterated first.

I would prefer not to use a binary string as eventually the
combinations will change to combination of any numbers (0,1,2,3,etc.)

I have looked at several implementation of Gray Codes, but have not
been able to figure out a way to complete this iteration cleanly.

Any help or suggestions on where else to look would be greatly
appreciated.
Thanks
Matt
Maybe the 'std::bitset' could help you:

std::vector<intVint;
// fill the Vint object.
std::bitset<32Sbit;
Sbit = Vint.at(0);
size_t Count = Sbit.count(); // number of bits that are set

--
Bob R
POVrookie
Nov 15 '06 #2
"Matt Chwastek" <Ma**************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Anyone who can help,

I am curretnly attempting to write some code that will allow iteration
using a vector<intfrom the highest possilbe degree of a combination
of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering
of element containing the same number of ones is not important to the
code, just the fact that the highest number of ones are iterated first.
I would prefer not to use a binary string as eventually the
combinations will change to combination of any numbers (0,1,2,3,etc.)

I have looked at several implementation of Gray Codes, but have not
been able to figure out a way to complete this iteration cleanly.

Any help or suggestions on where else to look would be greatly
appreciated.

Thanks

Matt
It could be done with very funky math, or simply converting the int to a
string, then counting.

I'm sure you can optimize this quite well:

#include <iostream>
#include <sstream>

int CountDigits( const int Value, const int Check )
{
if ( Check < 0 || Check 9 )
return 0;
char CheckDigit = '0' + Check;

std::stringstream ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;

int NumOfDigits = 0;
std::string::size_type Pos = 0;
while ( Pos != std::string::npos )
{
Pos = StrInt.find( CheckDigit, Pos );
if ( Pos != std::string::npos )
{
++Pos;
++NumOfDigits;
}
}

return NumOfDigits;

}

int main()
{
int CheckThis = 1011010;
std::cout << "Number of 1's in " << CheckThis << " is " <<
CountDigits( CheckThis, 1 ) << "\n";
std::cout << "Number of 0's in " << CheckThis << " is " <<
CountDigits( CheckThis, 0 ) << "\n";
std::cout << "Number of 9's in " << CheckThis << " is " <<
CountDigits( CheckThis, 9 ) << std::endl;

std::string wait;
std::getline( std::cin, wait );
}
Nov 15 '06 #3

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:FT************@newsfe06.lga...
"Matt Chwastek" <Ma**************@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>Anyone who can help,

I am curretnly attempting to write some code that will allow iteration
using a vector<intfrom the highest possilbe degree of a combination
of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering
of element containing the same number of ones is not important to the
code, just the fact that the highest number of ones are iterated first.
I would prefer not to use a binary string as eventually the
combinations will change to combination of any numbers (0,1,2,3,etc.)

I have looked at several implementation of Gray Codes, but have not
been able to figure out a way to complete this iteration cleanly.

Any help or suggestions on where else to look would be greatly
appreciated.

Thanks

Matt

It could be done with very funky math, or simply converting the int to a
string, then counting.

I'm sure you can optimize this quite well:

#include <iostream>
#include <sstream>

int CountDigits( const int Value, const int Check )
{
if ( Check < 0 || Check 9 )
return 0;
char CheckDigit = '0' + Check;

std::stringstream ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;

int NumOfDigits = 0;
std::string::size_type Pos = 0;
while ( Pos != std::string::npos )
{
Pos = StrInt.find( CheckDigit, Pos );
if ( Pos != std::string::npos )
{
++Pos;
++NumOfDigits;
}
}

return NumOfDigits;

}

int main()
{
int CheckThis = 1011010;
std::cout << "Number of 1's in " << CheckThis << " is " <<
CountDigits( CheckThis, 1 ) << "\n";
std::cout << "Number of 0's in " << CheckThis << " is " <<
CountDigits( CheckThis, 0 ) << "\n";
std::cout << "Number of 9's in " << CheckThis << " is " <<
CountDigits( CheckThis, 9 ) << std::endl;

std::string wait;
std::getline( std::cin, wait );
}
Here's a little better while loop (I had actually tried this before but it
wasn't working, I forgot that != seems to take precidence over = )

#include <iostream>
#include <sstream>

int CountDigits( const int Value, const int Check )
{
if ( Check < 0 || Check 9 )
return 0;
char CheckDigit = '0' + Check;

std::stringstream ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;

int NumOfDigits = 0;
std::string::size_type Pos = std::string::npos;
while ( (Pos = StrInt.find( CheckDigit, Pos + 1 )) !=
std::string::npos )
{
++NumOfDigits;
}

return NumOfDigits;

}

int main()
{
int CheckThis = 1011010;
std::cout << "Number of 1's in " << CheckThis << " is " <<
CountDigits( CheckThis, 1 ) << "\n";
std::cout << "Number of 0's in " << CheckThis << " is " <<
CountDigits( CheckThis, 0 ) << "\n";
std::cout << "Number of 9's in " << CheckThis << " is " <<
CountDigits( CheckThis, 9 ) << std::endl;

std::string wait;
std::getline( std::cin, wait );
}
Nov 15 '06 #4
The problem with converting a value to a string is that in the form of
the problem which I am trying to implement consecutive values (i.e. 8,9
or 255, 256) do not follow the path through which the iteration is
intended

I am trying to implement in code a graph theoretic problem where the
highest degree combinations (i.e. largest number of 1s in the sequence)
are iterated first.

For example:

In the case of a vector of size 10 the iteration would begin with the
vector [1 1 1 1 1 1 1 1 1 1], whose degree is 10, and the next 10
iterations would contain all unique combinations of 9 1s in the vector
of size 10, i.e. [1 1 1 1 1 1 1 1 1 0], [1 1 1 1 1 1 1 0 1], etc,
whose degree is 9. The iteration would continue in the manner until it
reached [0 0 0 0 0 0 0 0 0] whose degree is 0.

So far it seems to me to be very difficult to implement this in the
code, and being as I could be described as a novice when it comes to
programming, any help would be greatly appreciated.

Thanks for your help

Nov 20 '06 #5
Matt Chwastek wrote:
The problem with converting a value to a string is that in the form of
the problem which I am trying to implement consecutive values (i.e.
8,9 or 255, 256) do not follow the path through which the iteration is
intended

I am trying to implement in code a graph theoretic problem where the
highest degree combinations (i.e. largest number of 1s in the
sequence) are iterated first.

For example:

In the case of a vector of size 10 the iteration would begin with the
vector [1 1 1 1 1 1 1 1 1 1], whose degree is 10, and the next 10
iterations would contain all unique combinations of 9 1s in the vector
of size 10, i.e. [1 1 1 1 1 1 1 1 1 0], [1 1 1 1 1 1 1 0 1], etc,
whose degree is 9. The iteration would continue in the manner until
it reached [0 0 0 0 0 0 0 0 0] whose degree is 0.

So far it seems to me to be very difficult to implement this in the
code, and being as I could be described as a novice when it comes to
programming, any help would be greatly appreciated.

Thanks for your help
If you know the number of 0s and 1s in your string, you can *easily*
enumerate (go through) all combinations of those using 'next_permutation'
function:

#include <algorithm>
#include <string>
#include <iostream>

int main()
{
std::string a("00011111");
do {
std::cout << a << std::endl;
}
while (std::next_permutation(a.begin(), a.end()));
}

(the length of the string is the total number of your digits, and you
need to set the *first* N chars of it to '0', and the rest to '1').

The only difference I see in this from your requirements is that for
any certain number of 0s and 1s, using 'next_permutation' counts
*forward* in terms of the value of the resulting binary number, not
back. But I am sure you can overcome that tiny obstacle.

Good luck!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 20 '06 #6

Matt Chwastek wrote:
The problem with converting a value to a string is that in the form of
the problem which I am trying to implement consecutive values (i.e. 8,9
or 255, 256) do not follow the path through which the iteration is
intended

I am trying to implement in code a graph theoretic problem where the
highest degree combinations (i.e. largest number of 1s in the sequence)
are iterated first.

For example:

In the case of a vector of size 10 the iteration would begin with the
vector [1 1 1 1 1 1 1 1 1 1], whose degree is 10, and the next 10
iterations would contain all unique combinations of 9 1s in the vector
of size 10, i.e. [1 1 1 1 1 1 1 1 1 0], [1 1 1 1 1 1 1 0 1], etc,
whose degree is 9. The iteration would continue in the manner until it
reached [0 0 0 0 0 0 0 0 0] whose degree is 0.
So far it seems to me to be very difficult to implement this in the
code, and being as I could be described as a novice when it comes to
programming, any help would be greatly appreciated.

Thanks for your help
There are several reasonable approaches to this problem depending upon
whether you have rooms to save the entire enumeration or not.

If you have room to save your entire enumeration, you can simply
generate your entire enumeration, score the values as to how high they
are ranked as you generate them and sort them. This will extend well
when you have values other than just one and zero also. It will always
work as long as

If you don't have room to save the entire enumeration and sort it,
there is a better way:
You have to create a function that maps an integer between 0 and one
less than the number of vectors that you care about into the vector.
This is done by what is known as the "Principle of Vacant Spaces"

To do the latter, for the binary case, you have to recognize that if
*I* is less than the sum of the number of combinations of "N* (the
length of your vector) things taken *M* at a time varying M from 0 by 1
until the sum is greater than or equal to *I*, that there are *M* zeros
in the vector. Now you just have to figure out how to distribute those
*M* according to how big the residue is from the previous sum.

If you are just trying to generate them all, you skip the first
caluclation and set up a loop like this:
for (i = 0; i < vsize; i ++)
{
max_combin = combin (vsize, i);
for (j = 0; j < max_combin; j ++)
{
distribzeros (vsize, i, j);
}
}

Now you just have to make distribzeros generate a vector that is the
jth one that has that many zeros. I'll give you a better answer later
(after you've had a moment to think about how this works).

Nov 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nick_faye | last post by:
hi guys, im still a newbie in ms-access and i would like to ask how can i get the highest and the lowest date in ms-access report? for example, i have these entries: 10/1/2003 10/5/2003...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
1
by: sean | last post by:
I'm trying to change my applications priority to something besides the basic highest, above average, etc. I'm developing for an embedded processor (Windows CE 4.2), thus I'm using evc++ in...
5
by: Nonoize | last post by:
Hi all, Really dumb question but I don't know how to resolve it. Looked in help and evry book I have. I have a table where the primary key was set as an Integer and its reached over 140K...
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
5
by: owz | last post by:
Hi again had some more problems, all help welcome. Access 2000, SQL My problem is as stated in the title. I want 2 display the highest and lowest priced car sold for this month. This is what I...
13
by: td0g03 | last post by:
Hello again guys. I have a question. I'm working on a program for my class and I'm stuck on how to find the lowest and highest number of a user input. We aren't at arrays yet so that's out of the...
4
by: someone124 | last post by:
Hi, everyone. I'm trying to find the lowest and the highest numbers that a user has had punched in. The code includes a user punching in 4 different numbers, each on a different line. I need help...
5
by: ryuchi311 | last post by:
In C++ using arrays. I need to create a C Program that will ask for five integers input from the user, then store these in an array. Then I need to find the lowest and highest integers inside that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.