In: Computer Science
C++
Vector
Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes.
Your program should output the winner or winners of the election
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string *names;
double *votes;
double sum=0;
double winner=-1;
string winnerName="";
int total=0;
int size;
cout<<"Enter the number of candiates: ";
cin>>size;
names=new string[size];
votes=new double[size];
//reading votes
for(int i=0;i<size;i++){
cout<<"Enter candidate's name :";
cin>>names[i];
cout<<"Enter votes received :";
cin>>votes[i];
sum+=votes[i];
//finding the winner
if(winner<votes[i]){
winner=votes[i];
winnerName=names[i];
}
}
cout<<endl;
//printing the data
cout << setprecision(2)<<fixed;
cout<<setw(20) << left
<<"Name"<<setw(20) << left
<<"Votes"<<setw(20) << left
<<"Percentage"<<endl;
for(int i=0;i<5;i++){
cout<<setw(20) << left<<names[i]<<setw(20)
<< left
<<votes[i]<<votes[i]/sum*100<<"%"<<endl;
total+=votes[i];
}
cout<<setw(20) << left<<"Total"<<setw(20)
<< left <<total<<endl;
cout<<"The Winner of the Election is
"<<winnerName;
return 0;
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME