I have done this program over and over and no matter what I try the percentage p
ID: 3554360 • Letter: I
Question
I have done this program over and over and no matter what I try the percentage part will not work for me at all. Below is what I have. Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is:
Candidate Votes Received % of Total Votes
Johnson____5000___________25.91
Miller______4000___________20.73
Duffy______6000___________31.09
Robinson___2500___________12.95
Ashtony____1800___________9.33
Total______19300
The Winner of the Election is Duffy.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int findMax (int votes[]);
void main(){
int votes[20];
string name[10];
int total=0;
float percent[10];
int m;
cout <<"Enter the names of the candidates in the local election and the number of votes" <<endl;
for (int i=0;i<5;i++)
{ cin>>name[i];
cin>>votes[i]; }
for (int i=0;i<5;i++){
total= total + votes[i]; }
for (int i=0;i<5;i++) {
percent[i]=(votes[i]/total*100); }
cout <<"Candidate" <<' '<<"Votes Recieved"<<' '<< " % of total votes"<< endl;
cout<< setprecision(2);
for(int i=0;i<5;i++){
cout<<name[i]<<' '<<' '<<' '<<votes[i]<<' '<<' '<<percent[i]<<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,loc;
max= votes[0];
for (i=1;i<5;i++)
{ if(votes[i]>max)
max=votes[i];
loc=i;}
return loc;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int findMax (int votes[]);
void main(){
int votes[20];
string name[10];
int total=0;
float percent[10];
int m;
cout <<"Enter the names of the candidates in the local election and the number of votes" <<endl;
for (int i=0;i<5;i++)
{ cin>>name[i];
cin>>votes[i]; }
for (int i=0;i<5;i++){
total= total + votes[i]; }
for (int i=0;i<5;i++) {
percent[i]=((votes[i]/total)*100); }
cout <<"Candidate" <<' '<<"Votes Recieved"<<' '<< " % of total votes"<< endl;
cout<< setprecision(2);
for(int i=0;i<5;i++){
cout<<name[i]<<' '<<' '<<' '<<votes[i]<<' '<<' '<<percent[i]<<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,loc;
max= votes[0];
for (i=1;i<5;i++)
{ if(votes[i]>max)
max=votes[i];
loc=i;}
return loc;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.