In: Computer Science
DATA STRUCTURES USING C++ 2ND EDITION
Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by that candidate. The program should then output each candidates name, votes received by that candidate, 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 as follow
Johnson 5000 25.91
miller 4000 20.72
duffy 6000 31.09
robinson 2500 12.95
sam 1800 9.33
total 19300
the winner of the election is Duffy
#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 you
If you like my answer please rate and help me it is very Imp for me