Electronic Voting: Application of Unique/Distinct Values
Learn to create an electronic voting program using distinct and unique values.
Electronic voting
The election took place in a parliament with nine candidates competing with each other. Make a program to check who wins the election. Read the data from the Votes.txt
file (as given in the playground).
The file format is the following:
9 Total number of candidates, like here 9 means 9 candidates competing
1. Name 1
2. Name 2
3. .
4. .
9. Name 9
50 Total votes
1 2 3 2 7 9 1 4 ... Total 50 votes, where each number represents a vote v cast for the vth candidate.
Sample Output
Candidate Name Votes
1. Bernie 14
2. Donald 6
3. Nawaz 5
4. Benazir 6
5. Vladimir 5
6. Justin 5
7. Jonathan 2
8. Nehru 6
9. Bilawal 11
The compiled result
Bernie : ************** 14 votes
Donald : ****** 6 votes
Nawaz : ***** 5 votes
Benazir : ****** 6 votes
Vladimir : ***** 5 votes
Justin : ***** 5 votes
Jonathan : ** 2 votes
Nehru : ****** 6 votes
Bilawal : *********** 11 votes
Bernie won the election.
--------------------------------------
The compiled sorted result
Bernie : ************** 14 votes
Bilawal : *********** 11 votes
Donald : ****** 6 votes
Benazir : ****** 6 votes
Nehru : ****** 6 votes
Nawaz : ***** 5 votes
Vladimir : ***** 5 votes
Justin : ***** 5 votes
Jonathan : ** 2 votes
Bernie won the election.
Implementation details
Memory requirement:
We need the following variables in our program:
int voters; // No of voters
int noc = 9; // No of candidates
int vote_cast[voters]; // To store every vote read from file
int candidates_votes[noc] = {0}; // To store the vote-count of every candidate
// candidates_votes[1] holds the votes of candidate 1
// candidates_votes[2] holds the votes of candidate 2
// ....
// candidates_votes[9] holds the votes of candidate 9
string candidate_names[noc]; // to store the name of every candidate
Below is the flow of the program.
Step 1: Reading the voting file
Reading involves loading inside vote_cast[]
along with the size voters
and reading the number of candidates noc
and their names candidates_name[]
, hence the following prototype:
void read(int &voters, int vote_cast[], int &no_of_candidates, string candidate_names[]);
Instruction: Write your implementation of the above function in the editor at the end of the lesson.
Step 2: Computing the results
For compiling, we must iterate through all the votes, i.e., vote_cast[]
and increment the count of every candidate respectively. For this we should implement the following function:
void computeResult(int candidates_votes[], int vote_cast[], int voters);
Instruction: Write your implementation of the above function in the editor at the end of the lesson.
Step 3: Displaying the results
To display every candidate’s name and votes, we should make the following function:
void displayResult(int candidates_votes[], string candidate_names[], int noc);
Instruction: Write your implementation of the above function in the editor at the end of the lesson.
Step 4: Plotting the results
Display the entire result with a bar graph horizontally (made up of stars such that every candidate’s name must be followed by the *
s followed by the exact count.
void resultPlot(int candidates_votes[], string candidate_names[], int noc);
Instruction: Write your implementation of the above function in the editor at the end of the lesson.
Step 5: Sorting the result (using the attained votes)
In this step, we would like to sort by attained votes and based on that, sort the names as well.
- Hint: The comparison needs to be done based on votes, and swapping needs to be done on both
candidate_name[]
andcandidates_votes[]
.
Instruction: Write your implementation of the above functions here, and change the Voting.txt
file in whatever way you want.
#include<iostream>#include<fstream>#include<string>#include<iomanip>using namespace std;#define capacity 100void read(int &voters, int vote_cast[ ], int &no_of_candidates, string candidate_names[]);void computeResult(int candidates_votes[ ], int vote_cast[ ], int voters);void displayResult(int parties[ ], string candidate_names[ ], int noc);void resultPlot(int candidates_votes[ ], string candidate_names[ ], int noc);int sortCandidates(int candidates_votes[], string candidate_names[], int noc);int main(){// Write your code herereturn 0;}void read(int &voters, int vote_cast[ ], int &noc, string candidate_names[]){// Write your code here}void computeResult(int candidates_votes[ ], int vote_cast[ ], int voters){// Write your code here}void displayResult(int candidates_votes[ ], string candidate_names[ ], int noc){// Write your code here}void resultPlot(int candidates_votes[ ], string candidate_names[ ], int noc){// Write your code here}int sortCandidates(int candidates_votes[], string candidate_names[], int noc){// Write your code here}
Write down the main flow of the program and complete the main()
function first. Step by step, make all the functions and test them one by one.