help with looping structure
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: help with looping structure

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Posts
    1

    Exclamation 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;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    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.

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: help with looping structure

    Quote Originally Posted by GCDEF View Post
    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 round-up only once the total of percentages probably is less than 100.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: help with looping structure

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: help with looping structure

    Quote Originally Posted by laserlight View Post
    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: help with looping structure

    Quote Originally Posted by itsmeandnobodyelse View Post
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: help with looping structure

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: help with looping structure

    Quote Originally Posted by laserlight View Post
    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: help with looping structure

    Quote Originally Posted by itsmeandnobodyelse View Post
    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/en-us/libr...(v=VS.80).aspx
    http://www.cppreference.com/operator_precedence.html

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: help with looping structure

    Thanks for the valuable information.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: help with looping structure

    Quote 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.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Feb 2008
    Posts
    22

    Re: help with looping structure

    Quote Originally Posted by chasethesun View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center