

Apologies for the shouting but this is important.
When answering a question please:
 Read the question carefully
 Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
 If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
 If the question is inappropriate then click the 'vote to remove message' button
Insults, slapdowns and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Cofounder
Microsoft C++ MVP





For those new to message boards please try to follow a few simple rules when posting your question. Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
 Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
 Keep the subject line brief, but descriptive. eg "File Serialization problem"
 Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
 Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
 Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
 If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
 Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
 Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
 Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
 If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
 No advertising or soliciting.
 We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Cofounder
Microsoft C++ MVP





Hello,
For my new project, I have to develop an algorithm called frequency sweep.
The function parameters are nFreqStart, nFreqEnd and dSweepTime'.
As an example: for nFreqStart = 800Hz, the period T of this signal is 1.25ms, for nFreqEnd = 1600Hz, T is 0.625ms and the sum of all frequencies from nFreqStart to nFreqEnd must not be higher than dSweepTime.
My first version of this function is:
uint16_t nFrequencies;
void sweep_initialize(const uint16_t nFreqStart, const uint16_t nFreqEnd, const double dSweepTime)
{
double dTotalTime = 0.0;
double dIncrement = 0.0;
uint16_t nStep = 0;
double dTmpFreq = 0.0;
bool bSweep = true;
double dInitialTime = (1.0 / nFreqStart) + (1.0 / nFreqEnd);
nFrequencies = 2;
while (bSweep == true)
{
dTotalTime = dInitialTime;
dIncrement = (double)(nFreqEnd  nFreqStart);
dIncrement /= (double)(nFrequencies);
dTmpFreq = (double)nFreqStart;
nStep = nFrequencies  1;
for (uint16_t i = 0; i < nStep; i++)
{
dTmpFreq += dIncrement;
dTotalTime += 1.0 / dTmpFreq;
}
nFrequencies += 1;
if (dTotalTime >= dSweepTime)
{
bSweep = false;
break;
}
}
}
The next thing is, I need a counter (cpu clocks) for each frequency:
uint16_t* pSweep_Array;
void sweep_getCounter()
{
if (pSweep_Array != NULL)
{
free(pSweep_Array);
pSweep_Array = NULL;
}
pSweep_Array = (uint16_t*)malloc(sizeof(uint16_t) * nFrequencies);
if(pSweep_Array != NULL)
memset(pSweep_Array, 0, sizeof(uint16_t) * nFrequencies);
double dFrequency = (double)nFreqStart;
double dIncrement = (double)(nFreqEnd  nFreqStart);
dIncrement /= (double)(nFrequencies  1);
double dCounter = 0.0;
for (uint16_t i = 0; i < nFrequencies; i++)
{
dCounter = 65536  (F_CPU / dFrequency);
pSweep_Array[i] = (uint16_t)round(dCounter);
dFrequency += dIncrement;
}
}
But as a result, the computation complexity is O((n1)! * n^{2}), where n is the number of frequencies between nFreqStart and nFreqEnd, including nFreqStart & nFreqStop.
I need help for a less complex solution (function).





Hello, so I have to write a term paper on the subject of Root sorting. Google doesn't give me anything useful so I am here to ask someone who understands data structures and algorithms to give me some pointers on the subject. As far as I got is that I figured out it has something to do with sorting a binary tree and using the root of a binary tree. Can you please give me some insight on what algorithms should I look for or what book should I read?





Is that really what they said? It doesn't look like it's a thing.





I am doing an introductory course on algorithms. I've come across this problem which I'm unsure about. I would like to know which of the 2 are dominant
f(n): 10 log n or g(n): log(n^2)
Given the definitions of each of: Ω, Θ, O
I assumed f(n), so fn = Ω(g(n))
Reason being that log(n^2) is equivalent to 2 log n, so 10 log n is dominant.
Could somebody verify or offer an alternative?





Why don't you draw the graph and see which one is closer to the bottom? Basically, these are just the grading system of the algorithms (used to classify the algorithms based on their complexity), to find out which is better as compared to which is not. I personally use only Big O notation (instead of others, lower bounds, both bounds etc. instead of all of that just consider Big O, and draw the graph). Go here and do that, https://www.desmos.com/calculator[^]. The larger that a function grows with each input, the more time and space that it consumes (time and space might be relative to the data source or the algorithm being used). That is why, a better algorithm is the one that doesn't take much time. In my opinion, log (n^{2}) is a better function — look at the slopes that each of them makes.
Secondly, Quote: which of the 2 are dominant What do you mean by dominant? The better algorithm would have a lower slope as compared to the one with a higher slope (the higher slope one is a bad algorithm depending on the data source).
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out  Eminem
~! Firewall !~





thanks for the link.
I assumed that a "dominant" function is equivalent to f = Ω(g), where f is 10 log n.
So we are in agreement, since conversely g is O(f), where g is log(n^2). I was simply attempting to use a mathematical approach to prove or disprove.





hello all
i found this task:
"Given an undirected graph G = (V,E), and an integer k, the cycleelimination problem is to decide if all the cycles of the graph can be eliminated by deleting at most k edges from the graph. Either show that the problem is NPcomplete, or describe a polynomialtime algorithm for it
"
do you have some ideas for the solution ?
shall i reduce the task to Feedback arc set problem ?
Thank you very much






If I want to compute/calculate A*B, then:
In russian peasant's algorithm:
declare and define new helper variable C and P for product, initialized to 0, i.e. all bits are initialized to 0.
NOW:
1. copy A to C
2. Select 1 random bit 1 (whose value/state is 1) of B and left shift C the number of bits that come in right of the selected bit, i.e. if B=0000100, then C should be left shifted 2 bits, because there are 2 bits in right of the single bit 1 of B.
The new bits of C after the left shift are set to 0.
3. Add C to P
4. Turn the selected bit to 0 and if B becomes 0, i.e. all bits are 0, then P is the product of A and B, i.e. P=A*B, otherwise/else repeat all steps, i.e. redo step 1, 2, 3, 4 and etc.
Is that correct/right? If yes, then why CPU/ALU is designed to use Booth's algorithm instead of Russian Peasant's algorithm? In some cases, Russian Peasant's algorithm can be much easy to perform than Booth's algorithm, when most bits of the multiplier B, are 0, and only few are 1.





That is correct (though usually you wouldn't select a random 1 but a particular 1, eg going from low to high or from high to low, you can use some additional tricks that way). Typical hardware implementations don't use this because it takes a lot of steps. They typically don't really use Booth's algorithm either, but some of the ideas are reused.
Yes it may take very few steps, but it can also take many steps (consider 1 * 1). It's bad enough already that it can take many steps, but the variation is really bad as well  throws a wrench into the usual pipelining and forwarding tricks. I suppose you might pipeline it by unrolling that entire loop, not very pleasant in terms of area, and then it has multiple exits which on top of being generally annoying means you can't do the usual thing of dispatching an instruction the cycle before its inputs will be ready and then rely on forwarding because you don't know when the multiplication ends until it does (ok you kind of could but it sucks).. it's horrible.
So other algorithms were invented, such as the Dadda multiplier and the Wallace tree. The idea (for both of them) is to first compute all pairwise singlebit products, then add them up using as many simple adders as possible (not full word adders, but the 3:2 compressor kind). That works out really well, because the first step (the single bit products) are all completely independent, and then you get a ton of product bits that you can reduce to fewer bits with independent full adders. It takes some number of layers of such adders, and the layers are dependent, but within a layer everything is parallel.
And then you still need a multibit adders in the end, where you'd use any fast adder design you like. You only need one of them so go crazy, unlike an unrolled russian peasant multiplication (RPM) where you'd need a metric ton of big adders (you could use ripple carry but then it sucks). So it's smaller and faster than unrolled RPM, and pipelinable unlike iterative RPM.
That's not the end though, for example you can use slightly larger modules that add up 5 bits into 3 in a particular way and build the same sort of layered tree out of those, which is a bit faster. And you can shuffle the connections of those modules around a bit so that you connect slow bits (the modules don't calculate their outputs all at the same time) to "fast inputs" (the delay through a module is not the same for every input) and vice versa to compensate the timings.
Now Booth's encoding, specifically the modified Booth encoding of radix4 (if you go higher the circuits become annoying, not worth it), can be used to improve that even more, because you get fewer partial products .
Anyway, on a typical modern processor, multiplication takes maybe 3 to 5 times as much time as an addition. You can't match that with RPM unless there are very few bits set in the multiplicand, on average it would lose bit a huge margin.





Thanks for the quick and detailed answer. Now I am satisfied.





I am creating a DOORS database script that will be used to get all differences between 2 versions of a particular DOORS table (new vs old). I want to get all differences between the 2 tables (additions, deletions and changes). For anyone who doesn't know about DOORS, when an object is removed from a DB, the program marks the item's attribute as deleted, but you can still see it when you loop through all items. What I want to do is create a function that will display any items that have been changed since the older version.
Here is the algorithm I'm using:
for each newItem in newVers
if (newItem.deleted) {
check if deleted in oldVers
if (!oldVers.deleted) { }
} else {
if (newItem.id exists in oldVers) {
if (newVers != oldVers) {
}
} else {
}
}
}
Does this look like the way I should do this, or is there a better way? Just to reiterate, I am only looking for what, in the newer version has changed since the older one. I am not worried about changes in the opposite direction.
Any ideas would be appreciated.
Chris
modified 26Sep16 9:02am.





Your approach is accurate, but could be slow. I'm guessing that most of the items are unchanged, so
if (newItem.id exists in oldVers) could be an expensive operation, particularly if id is not indexed.
If your database supports fast sequential access on that field, then there are algorithms that "walk" the old and new, much the way text editors do file differencing.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





This is part of my last post here, but I made it much smaller in question.
I'm trying to write a function that will compress items measured by inches.
So say; I have 10 of these
Length = 72
Width = 4
Height = 3
And I package 6 of them into this, leaving the next 4 to be in another package.
Length = 72
Width = 24
Height = 3
I want to further compress them by decreasing the width and increasing the height such as this. I took 2 of them and stacked them taller increasing the height.
Length = 72
Width = 12
Height = 6
I have this, which works in 1 pass, but I'm trying to figure out a way to make multiple passes until there are no more passes to make. But I can't seem to figure out a method in which to do this.
If (item.Width > item.Height) Then
If (item.Qty > 2) Then
item.Width = p.Width * 3
item.Height += p.Height
End If
End If





Debug the programme.
I do believe that below line is causing that "it works only in 1 pass"
If (item.Width > item.Height) Then





Well... i'm pretty sure, you have to rethink your concept of programme...
Imagine, you have a box (container):
Height (3)
_ _ _ _ _ _
/ 
/  
/ _ _ _ _ _ _
/ / /
/ / /
 / /
 / / Length (72)
/_ _ _ _ _ _/
Width (24)
In that container 6 smaller boxes(containers {72x4x3}) are there. You want to repack these boxes into container with changed width and height:
Height (6)
_ _ _ _
/ 
/  
/  
/  
/  
 _ _ _ _
 / /
 / /
 / /
 / / Length (72)
/_ _ _ _/
Width (12)
Assuming, that you want to find out, if there's possibility to repack these innercontainers, your algorithm have to check, if:
 any dimension of innercontainer does not exceed the size of outercontainer,
 occupied area inside a box (sum of innercontainers areas) is smaller than outercontainer.
Till above condition is met, you can add another box (innercontainer)!
Note, that:
Area (of box) = Width * Length * Height
Occupied area = sum of innercontainers areas.
And finall note: do not change the size of outerbox, create new one and repack all boxes
Try!





I thought about that.
It would be easier to say will this fit in the space rather than try to create the space.
Not sure If I mentioned this, but the point of the program is to just get a rate from UPS or Fedex by creating virtual packages of dimensional weight, gravity weight, and to not create large package conditions by making them smaller.
Maciej Los wrote: Area (of box) = Width * Length * Height
That's dimensional weight, yes I could just sum up the dWeight till I hit a target.
So If I were to sum up say
3 x 3 x 3 = 27
and I have 10 of them for 270
then 3 x 3 x 30 should equal 270 and equals 270. hmm ...
My question was to adjust the size of 3 x 3 x 30 to 15 x 6 x 3 then try 1 more time.
I should just hire someone to write this for me.





I should just hire someone to write this for me.
Jim, never give up! There's a ton of members ready to help you (at least me).
Seems, you forgot to use loop!
If (item.Qty>2) Then
Do While (item.Width > item.Height)
item.Width = p.Width * 3
item.Height += p.Height;
Loop
End If
Am i right?
modified 28Sep16 2:03am.





I have no clue how to paste in my Linked In link, tried many times.
I actually have that sample code above working better now.
If your curious about the program ....
This is the code packaged in a console app in VS2015
There is a class called sample data, that you can remove the remarks to load extra data
In the main class, you can unmark data to load.
It outputs packages in the console. In the real program, those packages will be submitted to UPS for a rate quote, and be packaged in the packages XML section and looped around for a single multipackage quote.
The rules are long items will be shipped seperate
Isolated items are items that will ship in the Original box, slap a sticker on them
Common items are the rest of the stuff that will be tossed in a box
If items trigger the Large Package Indicator, then start a new package to save the customer money.
Ar last, see if we can further combine packages to keep the count down.
Dropbox  RTST Rev 2016092701.zip[^]
I'm not trying to get someone to do my work for me, I'm just having trouble wrapping my head around this.
I use to just add up all the mass until a package is created, then cube the mass into a square. But that failed for long items from 6FT to 8Ft long.
And it failed for isolated items as well.
Thin items are tough, I was doing thin separate, but have now combined them into common.
[edit]
This is a clean copy of the program, in which I started again today. I put the thin items back in and will try to streamline the packaging, see Rev5 class comments.
Dropbox  RTSR_Console 3.zip[^]
modified 28Sep16 2:03am.






Thank you for looking at it. I'm working on it today with some new ideas.





After writing it about 12 times, I came up with this. Works pretty well, but could be better.
Theirs a program called combine_virtual_packages_common that could use better refining.
I was thinking, perhaps I should treat a batch of items like water. Take a box that is 24x17x14 and calculate the item line as water level, and fill the box, say 1 or 2 inches at a time.
Dropbox  RTSR Console 2016093001.zip[^]





Sorry, Jim, i've been bit busy...
I've downloaded the latest version of RTSR Console. As it turned out i can't install .net framework 4.6.1 on my laptop (win10). After that, i tried to find combine_virtual_packages_common function, but i've failed. I've found only combine_virtual_Common .
Can you accept my invitation on LinkedIn? Then we'll be able to continue discussion in private.
Cheers
Maciej





The later is the correct name
combine_virtual _common
Sorry about the .net issue, and yes I'm running Win 10 and VS2015
Yes I can accept the invite, just need to receive it first.





Strange,
In my LinkedIn profile i see: "Invitation pending". I'll try to send you a message via InMail (on LinkedIn).
Maciej





It was pending in my queue, just accepted





Hi
I have a textfile and need to build a algorithm join lines
The lines have 14 characters that can be
1,2,3,4,5,6,or 7
Example
44444444444444
This numbers represent bit representation,
1= 001
2=010
3=011
4=100
5=101
6=110
7=111
For to join line I must to observe if two (or more) line have 1 or 2 characters differente then to XOR between characters
Example
Line 01=> 11122444444444
line 02==>12142444444444
First character each line is 1 zero different
Second character first line is 1 and second is 2 then 1 different
Same to 4th character (2 and 4), The others are equal.
due to having only two differences I DO a XOR bits
Second character
1 xor 2 ==> 3
001 xor 010 ==> 011
4th character
2 xor 4 ==> 6
010 or 100 ==> 110
I need build best algorithm to join lines with rules
1)I can have more than two lines being used
2) Each line can have a maximum 1 or 2 , character 7 (parameterized) after join
3) each line used in another must, can not be used more
Somebody know how can I to build a recursive algorithm ?
Tia





By specifying that you need the "best" algorithm, you have severely limited any response.
Oh, and that's "Octal" you're fiddling with.





Thank you,
better perhaps put examples
81 LINES
11111111111111
11121111111111
11141111111111
11211111111111
11221111111111
11241111111111
11411111111111
11421111111111
11441111111111
12111111111111
12121111111111
12141111111111
12211111111111
12221111111111
12241111111111
12411111111111
12421111111111
12441111111111
14111111111111
14121111111111
14141111111111
14211111111111
14221111111111
14241111111111
14411111111111
14421111111111
14441111111111
21111111111111
21121111111111
21141111111111
21211111111111
21221111111111
21241111111111
21411111111111
21421111111111
21441111111111
22111111111111
22121111111111
22141111111111
22211111111111
22221111111111
22241111111111
22411111111111
22421111111111
22441111111111
24111111111111
24121111111111
24141111111111
24211111111111
24221111111111
24241111111111
24411111111111
24421111111111
24441111111111
41111111111111
41121111111111
41141111111111
41211111111111
41221111111111
41241111111111
41411111111111
41421111111111
41441111111111
42111111111111
42121111111111
42141111111111
42211111111111
42221111111111
42241111111111
42411111111111
42421111111111
42441111111111
44111111111111
44121111111111
44141111111111
44211111111111
44221111111111
44241111111111
44411111111111
44421111111111
44441111111111
after joining
77771111111111
Example 3 first lines
11111111111111
11121111111111
11141111111111
Therea only difference in 3th character
001 xor 010 ==> 011
and there only a different in 3th charcter in 3th line then
011 xor 100 ==> 111 => 7





There's no recursion required; just a "for" loop.
XOR the first and second "lines"; then XOR that result with the 3rd; and so forth.
Maybe not the "best", but "easy" to understand and implement; and you're only dealing with 81 "lines".





Thank You
My Post was a trivial example , but can have somee restrictions
Example:
3,5 and 6 can show only 2 times
or
7 with 3
or
7 with 5
or
7 with 6
show only one time in file
example
75444444444444 is possible
75344444444444 no is possible because 7 is together 5 and 3
35644444444444 no possible 3,5 and 6
35444444444444 is possible
etc
My problem is way to let
smaller number of possible lines





If you need to "filter" your "list of lines" use LINQ.
You can remove items; count items; etc.
e.g.
... = MyList.Where( line => line.contains()  line.StartsWith()  etc. )





Hello to All!
I am performing a small academic research regarding distributed storage systems and some practical problems that are connected with this. One problem is failover and primarybackup replication algorithms. Some DSS use consensus algorithms to elect leader for replication like Raft and Paxos (MultiPaxos). So my question is:
How do these algorithms address replication issues in storage systems? What are advantages and disadvantages to use Paxos or Raft? E.g.:
1. Raft has following advantages:
2. Raft has following limits:
3. Paxos has following advantages:
4. Paxos has following limits:
Can anyone point me on what basic pros and cons are for these two algorithms in using for storage systems?
What is the practical usage of both of them in DSS?
Thanks in advance,
Nick.






Thank you Richard, I know these resources. But mostly information there is about internals of algorithms and how they work. By now I need to see a brief comparison of limits of each for the purpose of building a fault tolerant system using consensus.
So maybe more simple question for people who already familiar with both
protocols: what do like and dislike in each other? I need information not like "it's more complex" or "it's more popular" but better their limits in particular purpose (storage fault tolerance). Why one prefer Paxos or other likes Raft?
Regards,
Nick.





Hi.. im new here...
Im a BSCS freshman and im studying in a couple of weeks.. i just have a little problem... i am having trouble translating this flowchart into an algorithm pseudocode... i dont know how to end the program in the middle of DOWHILE and REPEATUNTIL, it just makes me write endless DOWHILEs... can someone help me?
here is the link of my flowchart..
untitled — Postimage.org[^]
thanks for helping me..
modified 27Aug16 9:00am.





Please use "code" tags to modify your question so the flowchart is readable. That will give you a fixed width font, and you can see the preview to get it right. You won't get any answers to the current mess.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





ive edited my post.. hope you can now understand it..





Crikey! If that's the 'fixed' version, the other one probably killed dolphins, fairies and little babies.
As suggested, rather than trying to hack it together using . characters for spacing, why dont you wrap it in <pre> </pre> blocks? (select it then hit the 'code' button)
You could end up with something as legible as this (even with it's incorrect decision boxes):
++
 START 
++
<+
+v+ 
X = X+1 
+++  YES
 
+v+ 
 X < 3?+
++

 NO

v
Failing that, upload an image to an imagehosting site and then post a link. That's the less preferred option, text is perfectly adequate.





omg thank you... now its better..
modified 27Aug16 8:21am.





You're welcome. That image is much more legible.






OMG now i get it.. thank you so much for the info ;D ;D
modified 27Aug16 21:01pm.





Please Need Urgent Code suggestions for this:
There is a 2D matrix which has 0s and 1s . Your main aim is to tell maximum number of rows that are max amplified . A row is max amplified if all the columns in a row are 1. You have been given K which tells the number of column inversions you HAVE to do . Column inversion means that the whole column of that 2D matrix is inverted . 0 is replaced by 1 and 1 is replaced by 0 . You have to tell that after K inversions how many rows of max amplification will be present . K inversions gave to take place . Not less than that not more . One column can be inverted more than once .





Your homework is set to test what you know, not how good you are at convincing strangers on the Internet to do your work for you.
Try doing it yourself, and you'll probably find it's not as difficult as you think. After all, it should be based on concepts that you've recently covered in your course.
If you really don't know where to start, then talk to your teacher.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
 Homer





This a test about your math knowledge and convert to code. Computer graphic you has do it or you will never get it






Am I reading this right if I say that you want to select the edge if the distance between your vector (the ray) is shorter than the distance epsilon (or some other defined minimal distance) from the line?
If we move to 2d a Voronoi diagram would suffice? Or some version of this:
Add a point to a polyline[^]




