Must be C++ , please read carefully Write a program that allows the user to ente
ID: 668344 • Letter: M
Question
Must be C++ , please read carefully
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
Smith 5000 27.32
Cena 2000 10.93
Hill 6000 32.79
Richardson 3500 19.13
Washington 1800 9.84
total 18300
The winner of the election is Hill
Explanation / Answer
#include<iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
string candidateNames[5];
int votes[5],maxIndex = 0,total = 0;
for(int i=0;i<5;i++)
{
cout << "Enter the last name of candidate " << i+1 << " ";
cin >> candidateNames[i];
cout << "Enter the number of votes recieved by candidate " << i+1 << " ";
cin >> votes[i];
if(votes[i]>=votes[maxIndex])
maxIndex = i;
total = total + votes[i];
cout << endl;
}
cout << "Candidates Votes Recieved % of total votes ";
for(int i=0;i<5;i++)
{
cout << candidateNames[i] << " " << votes[i] << " " << votes[i]*1.0/total <<endl;
}
cout << "total " << total;
cout << " The winner of the election is " << candidateNames[maxIndex];
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.