adspace


Write a program to read the data and evaluate the results of
the election. Print all output to the screen.
Your output should specify:
The total number of votes, the number of valid votes and the
number of spoilt votes.
The winner(s) of the election. Hint: An appropriate search
should be used to determine the winner(s).
The votes obtained by each candidate sorted in terms of the
number of votes obtained. Hint: An appropriate sort should
be used to sort the candidate(s).
The Source code should be saved as VotingSystem.

Project Input:

Candidates’ Names and Numbers
2501 Victor Taylor
2502 Denise Duncan
2503 Kamal Ramdhan
2504 Michael Ali
2505 Anisa Sawh
2506 Carol Khan
2507 Gary Owen

Votes
3 1 2 5 4 3 5 3 5 3 2 8 1 6 7 7 3 5 6 9 3 4 7 1 2 4 5 5 1 4 0

Project Output:
Invalid vote: 8
Invalid vote: 9

Number of voters: 30
Number of valid votes: 28
Number of spoilt votes: 2

The winner(s):
2503 Kamal Ramdhan
2505 Anisa Sawh

Candidate Score
2503 Kamal Ramdhan 6
2505 Anisa Sawh 6
2501 Victor Taylor 4
2504 Michael Ali 4
2502 Denise Duncan 3
2507 Gary Owen 3
2506 Carol Khan 2

Answer Posted / Mayank Sharma

Here is an example C++ code for the given problem:nn#include <iostream>n#include <vector>nnusing namespace std;nnstruct Candidate {n int number, votes;n string name;n};nnint main() {n vector<Candidate> candidates = {n {"2501", "Victor Taylor", 4},n {"2502", "Denise Duncan", 3},n {"2503", "Kamal Ramdhan", 6},n {"2504", "Michael Ali", 4},n {"2505", "Anisa Sawh", 6},n {"2506", "Carol Khan", 2},n {"2507", "Gary Owen", 3}n };nn vector<int> votes;n int invalidVotes = 0;nn for (int i = 0; i < candidates.size(); ++i) {n cin >> votes[i];n if (votes[i] <= 0 || votes[i] > 10) {n ++invalidVotes;n continue;n }n candidates[votes[i] - 1].votes++;n }nn cout << "Invalid vote: " << invalidVotes << "n";nn int totalVoters = candidates.size();n totalVoters -= invalidVotes;n cout << "Number of voters: " << totalVoters << "n";n cout << "Number of valid votes: " << totalVoters << "n";n cout << "Number of spoilt votes: " << invalidVotes << "n";nn vector<Candidate> winners; vector<Candidate> sortedCandidates = candidates;n sort(sortedCandidates.begin(), sortedCandidates.end(), [](const Candidate& a, const Candidate& b) { return a.votes > b.votes; });nn for (int i = 0; i < sortedCandidates.size(); ++i) {n if (sortedCandidates[i].votes > 0 && winners.size() < 2) {n winners.push_back(sortedCandidates[i]);n }n }nn cout << "The winner(s): ";n for (auto c : winners) {n cout << c.number << ", ";n }n cout << "n";nn cout << "Candidate Score:n";n for (auto c : candidates) {n cout << c.number << ": " << c.votes << "n";n }n}n

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What character terminates all character array strings a) b) . c) END

1395


daily Routine of father

1485


Can union be self referenced?

1267


What is the latest version on c++?

1208


How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

1165