Question

(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
Miller             4000       20.73
Duffy              6000               31.09
Robinson           2500               12.95
Ashtony            1800               9.33
Total              19300

Analyze and design this program. Implement the design. Compile and test the program. Submit your analysis and design document, and your source code.

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

#include <cstdlib>
#include <iostream>

using namespace std;


int main()
{
   string array1[5];
   int votes[5];
   int i;
   float total=0.0;
   for(i=0;i<5;i++)
   {
   cout<<"enter last name"<<endl;
   cin>>array1[i];
                cout<<"enter votes received"<<endl;
                   cin>>votes[i];
      total=total+votes[i];          
   }

cout<<total;

cout<<"Result"<<endl;
cout<<"Candidate"<<" Votes Received % of Total Votes"<<endl;
    for(i=0;i<5;i++)
   {
   cout<<array1[i]<<"           "<<votes[i]<<"                "<<(float)(votes[i]/total)*100.0<<endl;
          
   }
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

Add a comment
Answer #2

#include <iostream>

#include <iomanip>

#include <string>


using namespace std;


const int CANDIDATES = 5;


int sumVotes(int v[], int candidates);

void result(string lN[], int v[], int& t, int candidates);

int win(int v[], int candidates);


int main() 

{

  string lastName[CANDIDATES];

  int votes[CANDIDATES];

  int total;

  string winner;


  for (int i = 0; i < CANDIDATES; i++)

  {

    cout << "Enter the last names of the candidates in a local election ";

    cin >> lastName[i]; 

    cout << endl;

      

    cout << "Enter number of votes the candidate has received ";

    cin >> votes[i]; 

    cout << endl;

  }

  total = sumVotes(votes, CANDIDATES);

  result(lastName, votes, total, CANDIDATES);

  winner = win(votes, CANDIDATES);


  cout << "The Winner of the Election is " << lastName[win(votes, CANDIDATES - 1)];

  

  return 0;

}


int sumVotes(int v[], int candidates)

{

  int totalVotes = 0;

  for(int i = 0; i < candidates; i++)

  {

    totalVotes += v[i];

  }

  return totalVotes;

}


void result(string lN[], int v[], int& t, int candidates)

{

  int i = 0;

  double votePercentage;

  

  cout << "Candidate Votes Received % of Total Votes";

  cout << endl;


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

  {

    cout << lN[i] << "\t\t";

    cout << v[i] << "\t\t";


    cout << fixed << showpoint << setprecision(2);

    votePercentage = static_cast<double>(v[i]) * 100 / static_cast<double>(t);

    cout << votePercentage << "%" << "\t" << endl;

    cout << endl;

  }

  cout << "Total Votes: " << t << endl;

}


int win(int v[], int candidates)

{

  int elected = 0;

  for (int i = 0; i < candidates; i++

  {

    if (v[i] > elected)

    {

      elected = i;

    }     

  }

  return elected;

}



answered by: Jay
Add a comment
Know the answer?
Add Answer to:
(Write a program for C++ )that allows the user to enter the last names of five...
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
  • 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...

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

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

  • c++ Votes received by the candidate. Your program should also output the winner of the election....

    c++ Votes received by the candidate. Your program should also output the winner of the election. A sample output is: The Winner of the Election is Duffy. The input data should come from the file. Use 3 separate arrays to store the names, votes, and percentages before they are displayed. Include functions inputValues to input values for candidate names and votes received and place them in arrays. calcPercents which fills an array with the percentage of votes each candidate got....

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

  • Project 1, Program Design 1. Write a C program replace.c that asks the user to enter...

    Project 1, Program Design 1. Write a C program replace.c that asks the user to enter a three-digit integer and then replace each digit by the sum of that digit plus 6 modulus 10. If the integer entered is less than 100 or greater than 999, output an error message and abort the program. A sample input/output: Enter a three-digit number: 928 Output: 584 2. Write a C program convert.c that displays menus for converting length and calculates the result....

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