Use Pointer
Write a program that allows the user to enter the last names of a number of 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:
Number of Candidates: 5
Candidate Votes recieve %of total vote
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>
using namespace std;
int main(){
int max = 5, votes[max], max_vote = 0, total = 0, width = 10;
float percentage;
string names[max];
for(int i = 0; i < max; i++){
cout<<"Enter the name of candidate "<<i + 1<<endl;
cin>>names[i];
cout<<"Enter the number of votes that "<<names[i]<<" received.\n";
cin>>votes[i];
total += votes[i];
if(votes[i] > votes[max_vote]){
max_vote = i;
}
}
cout<<setw(width)<<left<<"Name"<<setw(width)<<"Votes"<<setw(width)<<"% "<<endl;
for(int i = 0; i < max; i++){
percentage = (float)votes[i]/(float)total * 100;
cout<<setw(width)<<left<<names[i]<<setw(width)<<votes[i]<<setw(width)<<percentage<<endl;
}
cout<<setw(width)<<"Total"<<setw(width)<<total<<endl;
cout<<"\nThe winner of the election is "<<names[max_vote];
return 0;
}
Comments
Leave a comment