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:
You should implement the 3 functions for each phrase:
Please fill in the following empty function
#include<iostream>
using namespace std;
const int MAX_SIZE = 50;
const int MAX_STRING_LENGTH = 20;
// structure definition of Participant
struct Participant
{
int id;
char name[MAX_STRING_LENGTH];
bool hasVoted; // true if voter has voted, false otherwise
int numVotes; // number of votes received (if the participant is a candidate
};
// structure definition of Election
struct Election
{
Participant participants[MAX_SIZE];
int numCandidates;
int numVoters;
// the candidates will occupy the first numCandidates elements in the participants array,
// while the other voters will occupy the next numVoters elements in the participants array
};
/* Print the list of candidates
* Parameter:
* - election: the Election struct object
*/
void printCandidates(const Election &election)
{
cout << "The list of candidates: " << endl;
for (int i=0; i<election.numCandidates; i++)
cout << "Candidate id: " << election.participants[i].id << "\tName:" << election.participants[i].name << endl;
}
/* Setup the election
* - initialize all the member variables of the Election object
* Parameters:
* - election: a Election struct object
* Note:
* - id for voter/candidate has to be unique
* - initialize all the member variables of the Participant elements
*/
void setup(Election &election)
{
// TODO: Complete the implementation
}
/* Casting votes to some candidates by a specific voter given the voter id
* Parameters:
* - election: the Election struct object
* - voterId: the voter's id
* - numVotes: the number of votes the voter wants to cast
* Note:
* validates the following
* - voter id has to be existed
* - candidate id has to be existed
*/
void voting(/* TODO: add the formal parameters */)
{
// TODO: Complete the implementation
}
/* Output the number of votes for each candidate
* and announce the winner by finding who has the maximum number of votes
* Parameters:
* - election: the Election struct object
* Note: if there is more than 1 candidate has the same number of maximum votes,
* announce it as a tie election.
*/
void resultTallying(const Election &election)
{
// TODO: Complete the implementation
}
// Main function for the election system
int main()
{
// Create an election
Election election;
cout << "===============================" << endl;
cout << " Welcome to the voting system! " << endl;
cout << "===============================" << endl;
cout << "Preparing ... " << endl;
do {
cout << "Enter how many candidates will run for the election: \n";
cin >> election.numCandidates;
cout << "Enter how many more voters: \n";
cin >> election.numVoters;
} while (((election.numCandidates + election.numVoters) > MAX_SIZE) || (election.numCandidates <= 0));
cout << "\n";
// the setup phase
setup(election);
int currentId;
int numVotes = 0;
// the voting phase
cout << "Voting starts ..." << endl;
printCandidates(election);
char cmd;
//for (int i = 0; i < (election.numCandidates + election.numVoters); i++)
do {
cout << "Enter the voter id to start voting:\n";
cin >> currentId;
do {
cout << "Enter the number of votes to be cast (<" << election.numCandidates+1 << ") :\n";
cin >> numVotes;
} while (numVotes > election.numCandidates);
voting(election, currentId, numVotes);
cout << "Continue with the next voter? (y/n) ";
cin >> cmd;
} while ((cmd == 'y') || (cmd == 'Y'));
cout << "Tallying votes ..." << endl;
//the result-tallying phase
resultTallying(election);
cout << "End of the election!" << endl;
return 0;
}#include<iostream>
using namespace std;
const int MAX_SIZE = 50;
const int MAX_STRING_LENGTH = 20;
// structure definition of Participant
struct Participant
{
int id;
char name[MAX_STRING_LENGTH];
bool hasVoted; // true if voter has voted, false otherwise
int numVotes; // number of votes received (if the participant is a
candidate
};
// structure definition of Election
struct Election
{
Participant participants[MAX_SIZE];
int numCandidates;
int numVoters;
// the candidates will occupy the first numCandidates elements in
the participants array,
// while the other voters will occupy the next numVoters elements
in the participants array
};
/* Print the list of candidates
* Parameter:
* - election: the Election struct object
*/
void printCandidates(const Election &election)
{
cout << "The list of candidates: " << endl;
for (int i=0; i<election.numCandidates; i++)
cout << "Candidate id: " << election.participants[i].id
<< "\tName:" << election.participants[i].name <<
endl;
}
/* Setup the election
* - initialize all the member variables of the Election
object
* Parameters:
* - election: a Election struct object
* Note:
* - id for voter/candidate has to be unique
* - initialize all the member variables of the Participant
elements
*/
void setup(Election &election)
{
string name;
for(int i=0;i<election.numCandidates;i++)
{
cout<<"Enter Id :";
cin>>election.participants[i].id;
cin.ignore();
cout<<"Enter Name :";
getline(cin,name);
int j;
for (j = 0; j < name.length(); j++)
{
election.participants[i].name[j] = name[j];
}
election.participants[i].name[j] = '\0';
}
}
/* Casting votes to some candidates by a specific voter given
the voter id
* Parameters:
* - election: the Election struct object
* - voterId: the voter's id
* - numVotes: the number of votes the voter wants to cast
* Note:
* validates the following
* - voter id has to be existed
* - candidate id has to be existed
*/
void voting(Election election,int id,int noOfVotes)
{
// TODO: Complete the implementation
for(int
i=0;i<election.numCandidates+election.numVoters;i++)
{
if(election.participants[i].id==id)
{
election.participants[i].numVotes+=noOfVotes;
election.participants[i].hasVoted=true;
}
}
}
/* Output the number of votes for each candidate
* and announce the winner by finding who has the maximum number of
votes
* Parameters:
* - election: the Election struct object
* Note: if there is more than 1 candidate has the same number of
maximum votes,
* announce it as a tie election.
*/
void resultTallying(const Election &election)
{
// TODO: Complete the implementation
int indx=0,cnt=0;
int max=election.participants[0].numVotes;
for(int i=0;i<election.numCandidates;i++)
{
if(max<election.participants[i].numVotes)
{
max=election.participants[i].numVotes;
indx=i;
}
cout<<"No of Votes for
"<<election.participants[i].name<<":"<<election.participants[i].numVotes<<endl;
}
for(int i=0;i<election.numCandidates;i++)
{
if(election.participants[i].numVotes==max)
cnt++;
}
if(cnt>0)
{
cout<<"Its a tie
Election"<<endl;
}
else
{
cout<<election.participants[indx].name<<" is the
winner."<<endl;
}
}
// Main function for the election system
int main()
{
// Create an election
Election election;
cout << "===============================" <<
endl;
cout << " Welcome to the voting system! " <<
endl;
cout << "===============================" <<
endl;
cout << "Preparing ... " << endl;
do {
cout << "Enter how many candidates will run for the election:
\n";
cin >> election.numCandidates;
cout << "Enter how many more voters: \n";
cin >> election.numVoters;
} while (((election.numCandidates + election.numVoters) >
MAX_SIZE) || (election.numCandidates <= 0));
cout << "\n";
// the setup phase
setup(election);
int currentId;
int numVotes = 0;
// the voting phase
cout << "Voting starts ..." << endl;
printCandidates(election);
char cmd;
//for (int i = 0; i < (election.numCandidates +
election.numVoters); i++)
do {
cout << "Enter the voter id to start voting:\n";
cin >> currentId;
do {
cout << "Enter the number of votes to be cast (<" <<
election.numCandidates+1 << ") :\n";
cin >> numVotes;
} while (numVotes > election.numCandidates);
voting(election, currentId, numVotes);
cout << "Continue with the next voter? (y/n) ";
cin >> cmd;
} while ((cmd == 'y') || (cmd == 'Y'));
cout << "Tallying votes ..." << endl;
//the result-tallying phase
resultTallying(election);
cout << "End of the election!" << endl;
return 0;
}
____________________
Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...
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....
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
Write a int function that accepts as parameter the name of a candidate and return the number of votes received by the candidate. So, if the user enters Duck, the output will be 6000. Of course, if the given name does not exist, display an appropriate message. Need help with the above here is what i have so far: #include <iostream> #include <iomanip> #include <string> #include <vector> #include <map> using namespace std; // Get the details from the user for...
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...