
December 19th, 2010, 03:41 PM
#1
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:
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string name[10];
int votes[20];
int total = 0;
double percent[10];
int m, i, j, sum = 0, max = 0;
cout << "Please enter 5 candidates followed by the votes they recieved here:\n";
for(i = 0; i < 5; i++)
{
cin >> name[i];
cin >> votes[i];
total = total + votes[i];
}
for (j = 0; j < 5; j++)
percent[j] = votes[j]/ total * 100;
cout << left << setw(10) << "Canditates" << right << setw(15) << "Votes" << setw(12) << "Percent" << endl;
cout << setprecision(2);
for(i=0; i <5; i++)
cout << left << setw(10) << name[i] << right << setw(15) << votes[i] << setw(12) << percent[i] << endl;
cout << left << setw(10) << "Total:" << right << setw(15) << total << endl;
m = 0;
for (i = 0; i < 5; i++)
{
if (votes[i] > m)
{
m = votes[i];
max = i;
}
}
cout << "The winner of the election is: " << name[max] << endl;
return 0;
}

December 19th, 2010, 06:51 PM
#2
Re: help with looping structure
percent[j] = votes[j]/ total * 100;
That will always be zero. Try
percent[j] = votes[j] * 100 / total;
If total is always zero in the loop, use the debugger to look at the values in votes.

December 20th, 2010, 05:16 AM
#3
Re: help with looping structure
Originally Posted by GCDEF
percent[j] = votes[j]/ total * 100;
That will always be zero. Try
percent[j] = votes[j] * 100 / total;
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.
Code:
percent[j] = (votes[j] * 100 + 50) / total; // the +50 would do the proper rounding
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 roundup only once the total of percentages probably is less than 100.

December 20th, 2010, 05:28 AM
#4
Re: help with looping structure
Originally Posted by itsmeandnobodyelse
You should use parantheses at the right side cause multiplication and division have the same priority and the compiler could perform either operation firstly.
Those operators group left to right, so the expression votes[j] * 100 / total is equivalent to (votes[j] * 100) / total.

December 20th, 2010, 05:55 AM
#5
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.

December 20th, 2010, 06:00 AM
#6
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.
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.

December 20th, 2010, 06:17 AM
#7
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.

December 20th, 2010, 07:50 AM
#8
Re: help with looping structure
Originally Posted by laserlight
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.
but they won't do any harm I guess
Not much harm, probably.

December 20th, 2010, 08:10 AM
#9
Re: help with looping structure
Originally Posted by itsmeandnobodyelse
Are you sure that it is C/C++ standard?
Yes. Besides, if it was not, the result of an expression such as (9  5  2) would be implementation defined.
Originally Posted by itsmeandnobodyelse
I lastly debugged code with overloaded operator+ and I mean to have seen that a+b+c was evaluated as a+(b+c).
That is strange. Perhaps for the sake of optimisation for operator+ this grouping rule might be violated, but that seems rather unlikely where an overloaded operator+ is concerned.

December 20th, 2010, 08:15 AM
#10
Re: help with looping structure
Originally Posted by itsmeandnobodyelse
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.
Not much harm, probably.
http://msdn.microsoft.com/enus/libr...(v=VS.80).aspx
http://www.cppreference.com/operator_precedence.html

December 20th, 2010, 11:34 AM
#11
Re: help with looping structure
Thanks for the valuable information.

December 20th, 2010, 05:21 PM
#12
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".
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
