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

% of Total Votes

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.

Submit your working source code (10 points)

Necessary comments ( 5 points)

General pseducode ( 5 points)

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

Algorithm

name = []

votes = []

// store the total no of votes

total_votes = 0

// sore the maximum no of votes

max = -infinity

for i = 1 to 5

BEGIN

            str = ask user to enter name

            v = ask user to enter no of votes

            // add values into array

            name.add(str)

            votes.add(v)

            total_votes += v

            // update the max votes

            if v > max

            BEGIN

                        Max = v

            END

END

for i = 1 to 5

BEGIN

            // calculate the percentage of votes of current candidate

            Per = votes[i] / total_votes * 100.0

            print(name[i], votes[i], per)

END

For I = 1 to 5

BEGIN

            // if current candidate is winner

            If votes[i] == max

            BEGIN

                        Print(“Winner : ” + name[i])

            END

END

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
  • (Write a program for C++ )that allows the user to enter the last names of five...

    (Write a program for C++ )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 Johnson            5000               25.91...

  • COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize...

    COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should 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. To solve this problem, you will use one dynamic...

  • No. 2 (35%)-array and record Following data is the number of votes each candidate on an...

    No. 2 (35%)-array and record Following data is the number of votes each candidate on an election. Write a program to output each candidate's name, the number of votes received, and the percentage of the total votes received by the name. The data: (write into a file, named "votes.dat") Candidate Votes Received 5009 4500 6533 2500 1845 Allan Johnson Bea Miller Peter Duffy Robinson Cruoso Mark Ashtony Bondita Rodriguez 3432 The output of this program consists of following information (into...

  • Write a Python program that creates a class which represents a candidate in an election. The...

    Write a Python program that creates a class which represents a candidate in an election. The class must have the candidates first and last name, as well as the number of votes received as attributes. The class must also have a method that allows the user to set and get these attributes. Create a separate test module where instances of the class are created, and the methods are tested. Create instances of the class to represent 5 candidates in the...

  • Using C language, write a program for the following: In a student representative council election, there...

    Using C language, write a program for the following: In a student representative council election, there are ten (10) candidates. A voter is required to vote a candidate of his/her choice only once. The vote is recorded as a number from 1 to 10. The number of voters are unknown beforehand, so the votes are terminated by a value of zero (0). Votes not within and not inclusive of the numbers 1 to 10 are invalid (spoilt) votes. A file,...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • Write a int function that accepts as parameter the name of a candidate and return the...

    Write a int function that accepts as parameter the name of a candidate and return the number of votes received by the candidate. So, if the user enters Duck, the output will be 6000. Of course, if the given name does not exist, display an appropriate message. Need help with the above here is what i have so far: #include <iostream> #include <iomanip> #include <string> #include <vector> #include <map> using namespace std; // Get the details from the user for...

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