Homework Help Question & Answers

Ranked choice voting is a system of tallying election ballots that is used in many national...

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.) Ranked Choice Voting!?! Here's How it Works

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.txtPreview the document -- 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.
We were unable to transcribe this image
We were unable to transcribe this image
0 0
Add a comment
Answer #1

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:

main.cpp (HomeworkLib) - Code::Blocks 17.12 File ID: HomeworkLib\HomeworkLib\bin\Debug HomeworkLib.exe Read 5 ballots. <glaCandidate #2 wins. - DX Re

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

Add a comment
Know the answer?
Add Answer to:
Ranked choice voting is a system of tallying election ballots that is used in many national...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.