Trusted by Students Everywhere
Why Choose Us?
0% AI Guarantee

Human-written only.

24/7 Support

Anytime, anywhere.

Plagiarism Free

100% Original.

Expert Tutors

Masters & PhDs.

100% Confidential

Your privacy matters.

On-Time Delivery

Never miss a deadline.

Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate

Computer Science Sep 23, 2020

Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, the 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:

Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Anthony 1800 9.33
Total 19300

The Winner of the Election is Duffy.

Expert Solution

Following is the Detailed explanation and solution of the Election Program. Basically, there are three functions other than main(). They are called from main. Also note that calculate_winner() is basically a majority finder. It does not know what to do in case of a tie.

I have attached both the code and the executable for your benefit.

Hope it helps.

/*
Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, the 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:

Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Anthony 1800 9.33
Total 19300
The Winner of the Election is Duffy.

*/

#include
#include //Used for the setprecision function
#define N 5
using namespace std;

char names[N][20];
int votes[N];
int totalVotes; //A counter to keep a record of total number of votes cast for all candidates
int winnerPos; //The position of the winner
int winnerVotes;//the number of votes the winner has received

void get_input(){
int i;
totalVotes = 0;
for (i=0;i cout << "Enter Last Name of Candidate" << endl;
cin >> names[i]; //store the name of the candidate
cout << "Enter Number of Votes received " << endl;
cin >> votes[i]; //store the number of votes that this candidate received
totalVotes += votes[i]; //update the totalVotes counter
}

}

void calculate_winner(){
int i;
winnerPos = 0;//arbitrarily assume the first person winner
winnerVotes = votes[0]; //so the winner has votes contained in the corresponding votes[0] location

//Now compare the votes[0] with votes[1], votes[2],..,votes[N] to find the real winner
for (i=1;i if ( winnerVotes < votes[i] ) {//There is someone with more votes
winnerPos = i; //so this candidate is the winner
winnerVotes = votes[i]; //update the winnerVotes
}
}
}
void show_output(){
int i;
cout <<"Candidate"<<"tt"<<"Votes Received"< for ( i = 0; i < N; i++ ){
cout.width(9);
cout << left< }
cout.width(9);
cout <<"Total "< cout <<"The Winner of the Election is "<

}

int main(){
get_input();
calculate_winner();
show_output();

return 0;
}

Please use this google drive link to download the answer file.       

https://drive.google.com/file/d/1e3ecfNIwF2k8k7v6mBdo7tCEDAiMi7HG/view?usp=sharing

Note: If you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process. 

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link 

Archived Solution
Unlocked Solution

You have full access to this solution. To save a copy with all formatting and attachments, use the button below.

Already a member? Sign In
Important Note: This solution is from our archive and has been purchased by others. Submitting it as-is may trigger plagiarism detection. Use it for reference only.

For ready-to-submit work, please order a fresh solution below.

Or get 100% fresh solution
Get Custom Quote
Secure Payment