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 6 1986
Matt Chwastek wrote in message
<11************ **********@k70g 2000cwa.googleg roups.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<int Vint;
// fill the Vint object.
std::bitset<32S bit;
Sbit = Vint.at(0);
size_t Count = Sbit.count(); // number of bits that are set
--
Bob R
POVrookie
"Matt Chwastek" <Ma************ **@gmail.comwro te in message
news:11******** **************@ k70g2000cwa.goo glegroups.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::stringstre am ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;
int NumOfDigits = 0;
std::string::si ze_type Pos = 0;
while ( Pos != std::string::np os )
{
Pos = StrInt.find( CheckDigit, Pos );
if ( Pos != std::string::np os )
{
++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 );
}
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:FT******** ****@newsfe06.l ga...
"Matt Chwastek" <Ma************ **@gmail.comwro te in message
news:11******** **************@ k70g2000cwa.goo glegroups.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::stringstre am ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;
int NumOfDigits = 0;
std::string::si ze_type Pos = 0;
while ( Pos != std::string::np os )
{
Pos = StrInt.find( CheckDigit, Pos );
if ( Pos != std::string::np os )
{
++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::stringstre am ConvertToStr;
ConvertToStr << Value;
std::string StrInt;
ConvertToStr >StrInt;
int NumOfDigits = 0;
std::string::si ze_type Pos = std::string::np os;
while ( (Pos = StrInt.find( CheckDigit, Pos + 1 )) !=
std::string::np os )
{
++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 );
}
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
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_permutati on'
function:
#include <algorithm>
#include <string>
#include <iostream>
int main()
{
std::string a("00011111") ;
do {
std::cout << a << std::endl;
}
while (std::next_perm utation(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_permutati on' 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
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). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
10/2/2003
10/4/2003
10/2/2003
10/3/2003
|
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) { }
double &operator()(int i) { return myX; }
|
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 conjunction with
vb.net. Now I can make a CeSetThreadPriority call using a C dll (from
the VB app), which does allow me to change the process priority to
something other than the 'supposed' allowed priorities (which are
251-255 for those who are...
|
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 worth of records and the numbering has restarted from 1.
I realize now that I should have set it to double. Can someone please advise
how I can save my existing records and restart the numbering from say
|
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:
http://www.cert.org/secure-coding/
The purpose of this library is to provide a collection of utility
functions that can assist software developers in writing C programs that
are free from common integer problems such as integer overflow, integer...
| |
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 have.
SELECT CarSales.Car_Reg,MAX (Purchased_Price) AS Highest_Sale
FROM CarSales
WHERE Purchased_Price=(SELECT MIN(Purchased_Price)AS Smallest_Sale
FROM...
|
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 question, but what would be another way of finding the highest and lowest. I remember hearing something in the lines of "cout <<" in class, but forgot. Also I could try the if else statement, but would seem like alot of work. I hope I am making this...
|
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 figuring out how to display the highest and lowest number from the numbers the user typed in and then print them. This is suppose to a very basic code using selection structures and variables, but I can't figure it out. I'm not if I can use the 'if...
|
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 array and add them together. The output will be the result in this format: LOWEST+HIGHEST=SUM
#include<stdio.h>
void main (){
int r, low, hig, ans, index;
for(index=0, index<4, index++){
printf("Enter number");
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |