Question
c++
Votes received by the candidate. Your program shou
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//function to display the array data
void displayResults(string name[],int votes[],float percentages[],int size)
{
    int total=0;
    cout<<"Candidate\tVotes Received\t% of Total Votes\n\n";
    for(int i=0;i<size;i++)
    {
        total+=votes[i];
        cout<<name[i]<<"\t\t"<<votes[i]<<"\t\t"<<percentages[i]<<endl;
    }
    cout<<"Total\t"<<total<<"\t"<<total/size;
}

//function to detrmin winner and returns the winner name
string determineWinner(string name[],float percentages[],int size)
{
    string mname="";
    double max=0;
    //loop through array and find the one with max votes
    for(int i=0;i<size;i++)
    {
        if(max<percentages[i])
        {
            max=percentages[i];
            mname=name[i];
        }
    }
    return mname;
}

//function to calculate % of votes
void calcPercents(int votes[],float percentages[],int size)
{
    int total=0;
    for(int i=0;i<size;i++)
    {
        total+=votes[i];
    }
    for(int i=0;i<size;i++)
    {
        percentages[i]=(votes[i]*100)/total;
    }
}

//function which reads votes data from file and stores them into array
int inputValues(string name[],int votes[])
{
    int size=0;
    ifstream myfile ("votes.txt");
    if (myfile.is_open())
    {
        while ( myfile>>name[size] )
        {
            myfile>>votes[size];
            size++;
        }
    myfile.close();
}
else
cout << "Unable to open file";
    return size;
}

//main function which triggers the above funcitons
int main()
{
    const int size=10;
   string name[size];
   int votes[size];
   float percentages[size];
   int length=inputValues(name,votes);
   calcPercents(votes,percentages,length);

   displayResults(name,votes,percentages,length);
   cout<<"\n\nThe Winner of the election is "<<determineWinner(name,percentages,length)<<endl;
   return 0;
}

Sample Output:

Candidate       Votes Received  % of Total Votes                                                                                                                

                                                                                                                                                                

A       5000    28                                                                                                                                              

B       4000    22                                                                                                                                              

C       6000    34                                                                                                                                              

D       2500    14                                                                                                                                              

Total   17500   4375                                                                                                                                            

                                                                                                                                                                

The Winner of the election is C

Add a comment
Know the answer?
Add Answer to:
c++ Votes received by the candidate. Your program should also output the winner of the election....
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
  • 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...

  • (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...

  • 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,...

  • 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...

  • (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...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • C Program Assignment: 2-Add comments to your program to full document it by describing the most...

    C Program Assignment: 2-Add comments to your program to full document it by describing the most important processes. 3-Your variables names should be very friendly. This means that the names should have meaning related to what they are storing. Example: Instead of naming the variable that stores the hours worked for an employee in a variable called ‘x’ you should name it HoursWorked. 5-Submit your .c program (note that I only need your source code and not all files that...

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
ADVERTISEMENT