Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code is supposed to let you enter five candidates and the number of votes re

ID: 3619902 • Letter: T

Question

The code is supposed to let you enter five candidates and the number of votes recieved. Then you calculate the percentage and print out the winner. I need help with my percentage code. Keeps coming out wrong.


#include<iostream>
#include<iomanip>
using namespace std;
int votes[20];
int findMax(int votes[20]);

int main()
{
string name[10];
int total, m, i;
float percent[10];

cout<<"Enter the names of candidates and numbers of votes"<<endl;

for(i=0; i<5; i++)
{
cin>>name[i];
cin>>votes[i];
}
total=0;
for (i=0;i<5;i++)
{
total=total+votes[i];
}
for(i=0;i<5;i++)
{
percent[i]=((votes[i]/total)*100);
for(i=0;i<5;i++)
{
cout<<"Candidate: "<<name[i]<<" "<<"Votes Recieved: "<<votes[i]<<" "<<setprecision(2)<<percent[i]<<"% of Total Votes"<<endl;
}
}
cout<<"Total "<<total<<endl;
m=findMax(votes);
cout<<"Winner of the election is "<<name[m]<<endl;
system("pause");
}

int findMax(int votes[])
{
int i, max;
max=votes[0];
for(i=0;i<5;i++)
{
if (votes[i]>max)
{
max=i;
}
}
return max;
}



Explanation / Answer

please rate - thanks you have a few minor errors see the comments and code in red
#include
#include
using namespace std;
int votes[20];
int findMax(int votes[20]);

int main()
{
string name[10];
int total, m, i;
float percent[10];

cout<<"Enter the names of candidates and numbers of votes"<

for(i=0; i<5; i++)
{
cin>>name[i];
cin>>votes[i];
}
total=0;
for (i=0;i<5;i++)
{
total=total+votes[i];
}
for(i=0;i<5;i++)
{
percent[i]=(((float)votes[i]/total)*100.); //you were doing integer arithmetic
}                              //close bracket was in the wrong place
for(i=0;i<5;i++)
{
cout<<"Candidate: "<<<" "<<"Votes Recieved: "<<<" "<<<fixed<<percent[i]<<"% of Total Votes"<