Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in c++ In lab 08, you were asked to write a program that allows the user t

ID: 3804124 • Letter: W

Question

Write in c++

In lab 08, you were asked to write a program that allows the user to enter the last names of live candidates in a local election and the number of votes received by each candidate. The program then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program also output the winner of the election. A sample output for the program is: For this lab, redo lab assignment 08 using dynamic arrays. You must ask the user for the number of candidates and then create the appropriate arrays to hold the data.

Explanation / Answer

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

int main()
{
int n;
cout << "Enter number of candidates: ";
cin >> n;
int *votes = new int[n];
string *candidate = new string[n];

int total = 0;
int max = -1;
int winner = -1;
  
cout << "Enter candidates information" << endl;
for(int i = 0; i < n ; i++)
{
cin >> candidate[i] >> votes[i];
if (votes[i] > max)
{
max = votes[i];
winner = i;
}
total += votes[i];
}

cout << "Candidate Votes Received % of Total Votes"<< endl;
for(int i = 0; i < n; i++)
{
double votePercent = ((double)votes[i])/total*100;
cout << candidate[i] << " " << votes[i] << " " << fixed << setprecision(2) << votePercent << endl;
}
cout << "Total " << total<<endl;

cout << "Winner of elections is "<< candidate[winner] << " with " << votes[winner] << " votes." << endl;
delete votes;
return 0;
}

/*

Sample execution
$ g++ elections.cpp
$ ./a.out
Enter number of candidates: 5
Enter candidates information
Johnson 5000
Miller 4000
Duffy 6000
Robinson 2500
Ashtony 1800
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
Winner of elections is Duffy with 6000 votes.

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote