Answer to Question #181184 in C++ for luna

Question #181184

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.

sample output:

Candidate         Votes Received       % of Total Votes

Johnson        5000           25.91

Miller                      4000                          20.73

Total                         9000


The winner of the election is Johnson.


1
Expert's answer
2021-04-15T03:27:51-0400
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    int max = 5, votes[max], max_vote = 0, total = 0, width = 20;
    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<<"Candidate"<<setw(width)<<"Votes Received"<<setw(width)<<"%of Total Votes"<<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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS