If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

help with looping structure

Okay, hello, my code works all most according to the standards, but i can't seem to figure out how to get the total out of the loop and insert it into another loop. Basically, I can't get the percentage of the votes from each candidate because the total stays as 0 in the loop. I've been at this for days & can't figure it out. Please help asap, anyways, here is the code:

Re: help with looping structure

If total is always zero in the loop, use the debugger to look at the values in votes.

You should use parantheses at the right side cause multiplication and division have the same priority and the compiler could perform either operation firstly. Also you should care for rounding cause integer division always rounds down to the lesser integer.

Nevertheless the above might give percentages which don't add up to 100%. That is because multiple roundings must not necesseraly balance equally, e. g. if you round down 4 times and round-up only once the total of percentages probably is less than 100.

Re: help with looping structure

Originally Posted by laserlight

Those operators group left to right, so the expression votes[j] * 100 / total is equivalent to (votes[j] * 100) / total.

Even if you are right for a specific compiler, it is better readable and mathematical more accurate to using parantheses. See also the code where I add 50 for rounding purposes and where the parantheses are mandatory.

Re: help with looping structure

Originally Posted by itsmeandnobodyelse

Even if you are right for a specific compiler, it is better readable and mathematical more accurate to using parantheses. See also the code where I add 50 for rounding purposes and where the parantheses are mandatory.

As laserlight said, they're evaluated left to right. I'm not a big believer in superfluous parenthesis, but they won't do any harm I guess.

As to the rounding, his result is a double, so casting something to a float would probably be closer to what he wants.

Re: help with looping structure

Actually, I am right for all standard conforming compilers, but yes, the parentheses can aid readability here. There is no difference in accuracy of the computation.

Are you sure that it is C/C++ standard? I lastly debugged code with overloaded operator+ and I mean to have seen that a+b+c was evaluated as a+(b+c).

I talked of mathematical accuracy.

In math the term "(a * b) / c" is equal to "a * (b / c)" and even equal to "(a / c) * b"

where the latter was the approach the OP has used. So setting parantheses not only would add readability but would make clear that the correct order of computation is crucial for computing the right result.

Re: help with looping structure

Are you sure that it is C/C++ standard? I lastly debugged code with overloaded operator+ and I mean to have seen that a+b+c was evaluated as a+(b+c).

I talked of mathematical accuracy.

In math the term "(a * b) / c" is equal to "a * (b / c)" and even equal to "(a / c) * b"

where the latter was the approach the OP has used. So setting parantheses not only would add readability but would make clear that the correct order of computation is crucial for computing the right result.

Re: help with looping structure

Re: help with looping structure

Originally Posted by chasethesun

Okay, hello, my code works all most according to the standards, but i can't seem to figure out how to get the total out of the loop and insert it into another loop. Basically, I can't get the percentage of the votes from each candidate because the total stays as 0 in the loop. I've been at this for days & can't figure it out. Please help asap, anyways, here is the code:

Either make the "votes", and "total" doubles, or typecast them as "double" while computing "percent".