54.8k views
0 votes
Modify the attached file 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
Then your program should display,
A Fourth Place
B Third Place
C Third Place
D First Place
F First Place
G Second Place
Here is the code:
#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;
}

1 Answer

4 votes

Final answer:

The winners of the Election are: D - First Place, F - First Place

The candidates with the second highest number of votes are: G - Second Place

The candidates with the third highest number of votes are: B - Third Place, C - Third Place

The candidates with the fourth highest number of votes are: A - Fourth Place

Step-by-step explanation:

To modify the code to handle multiple candidates with the same number of votes, we need to implement a ranking system that considers all candidates and their vote counts. Here are the steps to achieve this:

  1. Read the candidates' names and votes from the file.
  2. Calculate the total number of votes.
  3. Sort the candidates based on their votes in descending order.
  4. Assign ranks to the candidates based on their positions in the sorted list.
  5. If multiple candidates have the same number of votes, they will share the same rank.
  6. Display the ranking of the candidates along with their names and vote counts.

Here is the modified code:#include <array>

User Atiking
by
8.6k points