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. Please have output show first, second, third and fourth place winners asked in instructions below. The candidate names are attacked. Please provide screen shot of multiple candidates with the same number of votes.  Suppose the candidates are A, B, C, D, E, F, G and H and their votes are: A 100

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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. Please have output show first, second, third and fourth place winners asked in instructions below. The candidate names are attacked. Please provide screen shot of multiple candidates with the same number of votes.  Suppose the candidates are A, B, C, D, E, F, G and H 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

votes.txt
A = Johnson
B = Miller
C = Robinson
D = Duffy
E = Asthon
F = Sampson
G = Adams
H = Williams

My code to be modified according to instruction

#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;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education