Please modify the C++ data structure program below to detect and display multiple candidates with the same number of votes: multiple winners, multiple candidates with the second highest number of votes, multiple candidates with the third highest number of votes, and so on. Suppose the candidates are A, B, C, D, E, F and G and their votes are: A 100 B 200 C 200 D 400 E 100 F 400 G 300 H 300 Then your program should display, A Fourth Place B Third Place C Third Place D First Place E Fourth Place F First Place G Second Place H Second Place with the amount of votes they received, for example: A Fourth Place with 100 votes, B Third Place with 200 votes, C Third Place with 200 and etc. Text file names and program below. Thank you. votes.txt A = Johnson B = Miller C = Robinson D = Duffy E = Asthon F = Sampson G = Adams H = Williams ________________________________________________________________________________ #include #include #include #include #include #include #include #include using namespace std; const string filename = "votes.txt"; const int maxCandidates = 8; struct Candidate { string name = ""; size_t votes = 0; double percent = 0.0; }; int main() { ifstream inps(filename); assert(inps); array candidates; size_t nCandidates = 0; while (inps.good() && nCandidates < maxCandidates) { string name; inps >> name; size_t votes; inps >> votes; candidates[nCandidates].name = name; candidates[nCandidates].votes = votes; nCandidates++; } if (nCandidates > 0) { size_t namefieldwidth = 12; size_t maxvotes = 0; size_t maxidx = 0; size_t totalvotes = 0; for (size_t idx = 0; idx < nCandidates; idx++) { totalvotes += candidates[idx].votes; if (candidates[idx].votes > maxvotes) { maxvotes = candidates[idx].votes; maxidx = idx; } } size_t namefieldwith = 16; size_t votesfieldwidth = 16; size_t percentfieldwidth = 16; if (totalvotes > 0) { for (size_t idx = 0; idx < nCandidates; idx++) candidates[idx].percent = 100.0 * candidates[idx].votes / static_cast(totalvotes); } cout << fixed << setprecision(2); cout << left << setw(namefieldwidth) << "Candidate" << ' ' << right << setw(votesfieldwidth) << "Votes Received" << ' ' << right << setw(percentfieldwidth) << setfill(' ') << "% of Total Votes " << endl << endl; for (size_t idx = 0; idx < nCandidates; ++idx) cout << left << setw(namefieldwidth) << candidates[idx].name << ' ' << right << setw(votesfieldwidth) << candidates[idx].votes << ' ' << right << setw(percentfieldwidth) << setfill(' ') << candidates[idx].percent << endl; size_t totalvotesfieldwidth = namefieldwidth + votesfieldwidth + 1; cout << setw(totalvotesfieldwidth) << totalvotes << endl << endl; cout << "The winner of the Election is " << candidates[maxidx].name << endl; } return 0; } This is my second time with this question. This statement below was added to the code on the first attemp and the image uploaded was the output. That is incorrect. Could you please modify the code according to the instructions please. Thank you /** Implementation/Code change to display multiple winners This way more than one candidate with same number of maximum votes can be displayed **/ //Loops through every candidate for (size_t idx = 0; idx < nCandidates; idx++) { //If the candidate has max votes if (candidates[idx].votes == maxvotes) { //Display the candidate name cout << candidates[idx].name << endl; } } } return 0; }
Please modify the C++ data structure program below to detect and display multiple candidates with the same number of votes: multiple winners, multiple candidates with the second highest number of votes, multiple candidates with the third highest number of votes, and so on.
Suppose the candidates are A, B, C, D, E, F and G and their votes are:
A 100
B 200
C 200
D 400
E 100
F 400
G 300
H 300
Then your program should display,
A Fourth Place
B Third Place
C Third Place
D First Place
E Fourth Place
F First Place
G Second Place
H Second Place
with the amount of votes they received, for example: A Fourth Place with 100 votes, B Third Place with 200 votes, C Third Place with 200 and etc. Text file names and program below. Thank you.
votes.txt |
A = Johnson |
B = Miller |
C = Robinson |
D = Duffy |
E = Asthon |
F = Sampson |
G = Adams |
H = Williams |
________________________________________________________________________________
#include <array>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const string filename = "votes.txt";
const int maxCandidates = 8;
struct Candidate
{
string name = "";
size_t votes = 0;
double percent = 0.0;
};
int main()
{
ifstream inps(filename);
assert(inps);
array<Candidate, maxCandidates> candidates;
size_t nCandidates = 0;
while (inps.good() && nCandidates < maxCandidates)
{
string name;
inps >> name;
size_t votes;
inps >> votes;
candidates[nCandidates].name = name;
candidates[nCandidates].votes = votes;
nCandidates++;
}
if (nCandidates > 0)
{
size_t namefieldwidth = 12;
size_t maxvotes = 0;
size_t maxidx = 0;
size_t totalvotes = 0;
for (size_t idx = 0; idx < nCandidates; idx++)
{
totalvotes += candidates[idx].votes;
if (candidates[idx].votes > maxvotes)
{
maxvotes = candidates[idx].votes;
maxidx = idx;
}
}
size_t namefieldwith = 16;
size_t votesfieldwidth = 16;
size_t percentfieldwidth = 16;
if (totalvotes > 0)
{
for (size_t idx = 0; idx < nCandidates; idx++)
candidates[idx].percent = 100.0 * candidates[idx].votes / static_cast<double>(totalvotes);
}
cout << fixed << setprecision(2);
cout << left << setw(namefieldwidth) << "Candidate" << ' ' << right << setw(votesfieldwidth) << "Votes Received" << ' ' << right << setw(percentfieldwidth) << setfill(' ') << "% of Total Votes " << endl << endl;
for (size_t idx = 0; idx < nCandidates; ++idx)
cout << left << setw(namefieldwidth) << candidates[idx].name << ' ' << right << setw(votesfieldwidth) << candidates[idx].votes << ' ' << right << setw(percentfieldwidth) << setfill(' ') << candidates[idx].percent << endl;
size_t totalvotesfieldwidth = namefieldwidth + votesfieldwidth + 1;
cout << setw(totalvotesfieldwidth) << totalvotes << endl << endl;
cout << "The winner of the Election is " << candidates[maxidx].name << endl;
}
return 0;
}
/**
Implementation/Code change to display multiple winners
This way more than one candidate with same number of maximum votes can be displayed
**/
//Loops through every candidate
for (size_t idx = 0; idx < nCandidates; idx++)
{
//If the candidate has max votes
if (candidates[idx].votes == maxvotes)
{
//Display the candidate name
cout << candidates[idx].name << endl;
}
}
}
return 0;
}
![**Transcript of Visual Studio Debug Console Output**
This image displays the output of a Visual Studio Debug Console during the execution of a console application. The displayed results appear as follows:
- **Election Results Overview:**
- **Candidate:** Johnson
- **Votes Received:** 0
- **Percentage of Total Votes:** 0.00%
- **Announcement:**
- The winners of the election are: Johnson
- **Console Information:**
- The console application `ConsoleApplication1.exe` exited with code 0, indicating successful execution.
- A note explains how to automatically close the console when debugging stops. It suggests enabling this feature via the path: `Tools -> Options -> Debugging -> Automatically close the console`.
- **Prompt for User:**
- The console instructs the user to press any key to close the window.
This typical console output format is used to debug and visualize the results of programming operations, often seen in software development environments like Visual Studio. The current example shows the workings of a mock election result calculation.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F802555ea-2663-4669-897b-7df9c2cf7457%2F16511ddf-3e4b-4794-96b5-dc7f59637867%2Ff1p63g_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)