Ranked choice voting is a system of tallying election ballots that is used in many national and local elections throughout the world. Instead of choosing a single candidate, voters must rank the available candidates in the order of their choice. For example, if three candidates are available, a voter might choose #2, #1, and #3 as their choices, with #2 being their first choice, #1 the second, and #3 the third.
The outcome is determined by a runoff, which follows this process:
1) The first choices on all ballots are counted. If one candidate receives more than 50% of the vote, that candidate is elected.
2) If no candidate receives more than 50% of the vote, the lowest scoring candidate is eliminated (or multiple candidates if there is a tie for lowest scoring).
3) The ballots are recounted, using the highest ranking non-eliminated candidate on each ballot.
4) The process is repeated until one candidate receives more than 50% of the vote or there is a tie.
Here is a good video describing the process visually:
Ranked Choice Voting!?! Here's How it Works (Links to an
external site.) 
Assignment
Write a program that performs a series of ranked choice elections based on an input file.
Each election contains 3 candidates. The input file will contain multiple elections, in the following format:
1) At the beginning of a test case, a single integer indicating the number of lines that follow.
2) A series of 3-line ballots, with the voter's first choice being on the first line, second choice on the second, and third on the last line. In this
3) There can be up to 500 ballots in an election and no less than 5.
For example, the following file has 3 ballots:
9 3 1 2 1 2 3 3 2 1
The first ballot selects candidates 3, 1 and 2 in that order. The second ballot selects candidates 1, 2 and 3 in that order. Similarly the third selects candidates 3, 2, and 1.
Requirements
1) For each test case, output the number of ballots read and the winner (something like "Candidate #1 wins").
2) You are free to use any parts of the C++ language we've studied in this course.
3) Dynamic memory is not necessary, but you may find it useful to to allocate the buffer used to hold the ballots dynamically. For example, if you anticipate 300 ballots, allocate an array of 900 lines (3 lines for each ballot).
4) The program must read the input file and tally all votes according to a ranked choice system. No hard-coded inputs or outputs will be accepted.
5) Read the file continuously until EOF and process all elections in the file. After EOF, the program can exit. It is OK to hard code the filename (i.e. not ask the user for input).
Input File
A file containing 3 test cases is posted here --
elections.txt
-- use this as
the input to test your program. Edit: I cant find the download to
the txt file but here are the numbers that are in
elections.txt:
15
1
2
3
3
2
1
2
1
3
1
2
3
2
3
1
15
1
2
3
1
2
3
1
2
3
1
2
3
2
1
3
15
3
2
1
3
2
1
3
1
2
1
2
3
1
3
2
Example Output
Read 5 ballots. Candidate #2 wins. Read 5 ballots. Candidate #1 wins. Read 5 ballots. Candidate #3 wins.
BELOW IS THE IMPLEMENTATION OF ABOVE PROBLEM ,FILE NAME IS ELECTIONS.TXT , PLEASE READ THE FILE WITH SAME NAME FROM THE SAME DIRECTORY OF THE PROJECT.I MADE IT USING CODE BLOCKS COMPILER.
IT TAKES INPUT FILE AND READ LINE BY LINE INTEGERS AND CORRESPONDINGLY CALCULATES THE RESULT,PLEASE GO THROUGH THE CODE ONCE (OR MORE) TO GET A CLEAR IDEA OF WHAT I HAVE DONE.
AND PLEASE SUGGEST ANY IMPROVEMENT AND IF YOU DO NOT UNDERSTAND ANYTHING IN THE CODE PLEASE COMMENT , I WILL SOLVE IT FOR YOU.
AND IF YOU LIKE IT PLEASE UPVOTE,THANK YOU.
FOR THE CUSTOM INPUT PROVIDED THE OUTPUT LOOKS LIKE THIS:

#include<bits/stdc++.h>
using namespace std;
int main(){
ifstream theFile("elections.txt");//file pointer
int x;
while(theFile >> x){// reading integer from file line to
line.
int total=x;// first input number is total number of vote i.e,
number of ballots*3.
map<int,int>arr;// to store frequency of each vote to
candidate accordance to the people's first choice.
vector< vector<int> >ve;// vector to store each ballot
of size 3
total/=3;// as each ballot is of size 3
int ballots=total;
ve.resize(ballots);
for(int i=0;i<ballots;i++){
ve[i].resize(3);
}
int j=0;
while(total--){// taking all ballot input.
bool f=theFile>>x;
ve[j][0]=x;
f=theFile>>x;
ve[j][1]=x;
f=theFile>>x;
ve[j][2]=x;
j++;
}
bool flag=0;
cout<<"Read "<<ballots<<"
ballots."<<"\n";
while(!flag){// flag is set when we elect a candidate
arr.clear();
int sum=0;
for(int i=0;i<ballots;i++){
if(!(ve[i].empty())){
sum+=1;
arr[ve[i][0]]++;
}
}
int maxi=INT_MIN;
int mini=INT_MAX;
map<int,int>::iterator it;
it=arr.begin();
for(;it!=arr.end();it++){// finding maximum and minimum vote in the
people's preferable candidate.
maxi=max(maxi,it->second);
mini=min(mini,it->second);
}
if(maxi>(sum/2.0)){// condition for a candidate to be
elected(more than 50%.)
if(maxi==arr[1]){
cout<<"Candidate #"<<1<<"
wins."<<"\n";
}
else if(maxi==arr[2]){
cout<<"Candidate #"<<2<<"
wins."<<"\n";
}
else{
cout<<"Candidate #"<<3<<"
wins."<<"\n";
}
flag=1;
}
else{// if any candidate has minimum number of votes then they are
eliminated from further process.
int eliminated;
if(mini==arr[1]){
eliminated=1;
for(int i=0;i<ballots;i++){
vector<int>::iterator vit=ve[i].begin();
for(;vit!=ve[i].end();vit++){
if(*vit==eliminated){
break;
}
}
if(vit!=ve[i].end()){
ve[i].erase(vit);
}
}
}
if(mini==arr[2]){
eliminated=2;
for(int i=0;i<ballots;i++){
vector<int>::iterator vit=ve[i].begin();
for(;vit!=ve[i].end();vit++){
if(*vit==eliminated){
break;
}
}
if(vit!=ve[i].end()){
ve[i].erase(vit);
}
}
}
if(mini==arr[3]){
eliminated=3;
for(int i=0;i<ballots;i++){
vector<int>::iterator vit=ve[i].begin();
for(;vit!=ve[i].end();vit++){
if(*vit==eliminated){
break;
}
}
if(vit!=ve[i].end()){
ve[i].erase(vit);
}
}
}
}
}
cout<<endl;
}
}
// code contributed by MIHIR SINGH codechef:(kassutta)
Ranked choice voting is a system of tallying election ballots that is used in many national...
To combat election fraud, your city is instituting a new voting procedure. The ballot has a letter associated with every selection a voter may make. A sample ballot is shown:- (Voter places a tick next to his or her preferred candidate, proposition and measure to indicate his/her vote) 1. Mayoral Candidates A. Pincher, Penny B. Dover, Skip C. Perman, Sue 2. PROP 17 D. YES E. NO 3. MEASURE 1 F. YES G....
Using C language, write a program for the
following:
In a student representative council election, there are ten (10)
candidates. A voter is required to vote a candidate of his/her
choice only once. The vote is recorded as a number from 1 to 10.
The number of voters are unknown beforehand, so the votes are
terminated by a value of zero (0). Votes not within and not
inclusive of the numbers 1 to 10 are invalid (spoilt) votes.
A file,...
PYTHON (A voting machine consists of an election district, together with the names of the candidates and the number of votes cast for each candidate. Define a VotingMachine class, wrote a docstring for the class, and define the following three class methods: 1. An initialization method. The initialization method should: * take a string parameter, electionDistrict, and assign it to the instance attribute elctionDistrict of the voting machine being created. * create an instance attribute named candidates for the voting...
Elections between Hillary and Trump. There are only three voters and all of them strictly prefer Hilary to Trump. We assume that voters' utility only depends on the candidate that wins the election. In the first round, all voters vote simultaneously. If a candidate is voted by all the three voters, this candidate is elected. If none of the two candidates receives all the votes, then a second round takes place. All voters vote again simultaneously, but the winner is...
15.1: Tallying Votes Elections are a formal group-decision making process by which a population chooses a person to hold office, such as the mayor of a city. Another use of elections is to accept or reject a political proposition. Most election results are tallied, or counted, using electronic voting. Most electronic voting machines use a computer to take care of the chore of casting and counting votes. This Tuesday, November 6 (the date your assignment is due) is election day....
It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...
It's almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program's job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...
Assignment 11.2: New Hampshire Primary (10 pts) The New Hampshire Primary is taking place this Tuesday, February 11, 2020. According to Wikipedia, a primary "Primary elections or often just primaries, are the process by which voters can indicate their preference for their party's candidate, or a candidate in general, in an upcoming general election... with the goal of narrowing the field of candidates" The New Hampshire primary is the first of the primary elections, which will be held to select...
This lab continues to work with C++ arrays and introduces vectors 1) First, most code that works on arrays can solve a problem using a single loop. One problem where this is not true is if you need to check if there are any duplicates in the array. ·In a file called duplicates.cpp write a function hasDups(string strs[], int numStrs) that returns true if there are any duplicates in the names array ·Write main() to simulate reading in a single ranked...
Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...