Question

Write a program that allows the user to enter the last names of five candidates in...

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                Percent of Total Votes
Jack                              52                                          26
Jill                                 41                                         20.5
Bob                             13                                          6.5
Bill                              74                                          37
Sam                            20                                          10
Total                                200

The election winner is Bill.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<iomanip>

#include<vector>

using namespace std;

int main()

{

    vector<string> name;

    vector<int> votes;

   

    // store the total no of votes

    int total_votes = 0;

   

    // store the maximum no of votes

    int max = INT_MIN;

   

    int i;

   

    for( i = 0 ; i < 5 ; i++ )

    {

        string str;

       

        cout<<"Enter name : ";

        cin>>str;

       

        // add name to vector

        name.push_back(str);

       

        int x;

       

        cout<<"Enter votes recieved : ";

        cin>>x;

       

        // add votes to vector

        votes.push_back(x);

       

        total_votes += x;

       

        if( x > max )

            max = x;

    }

    cout<<setw(20)<<"Candidate"<<" ";

    cout<<setw(20)<<"Votes Received"<<" ";

    cout<<setw(20)<<"% of Total Votes"<<endl;

     

    for( i = 0 ; i < votes.size() ; i++ )

    {

        // calculate percentage of votes recieved

        double per = (double)votes[i] / (double)total_votes * 100.0;

     

        cout<<setw(20)<<name[i]<<" ";

       cout<<setw(20)<<votes[i]<<" ";

        cout<<setw(20)<<per<<endl;

    }

    cout<<"Total : "<<total_votes;

    cout<<"\nWinners are : ";

  

    for( i = 0 ; i < votes.size() ; i++ )

        if( votes[i] == max )

            cout<<name[i]<<" ";

   

    return 0;

}


Sample Output

Add a comment
Know the answer?
Add Answer to:
Write a program that allows the user to enter the last names of five candidates in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT