Question

Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

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 representing the voting status of the participant (i.e true for "has voted", false for "has not yet voted".
    • numVotes -- an integer variable for storing the number of votes a candidate has received. (Note: the numVotes of a voter should be zero).
  • Election structure, which has the following members:
    • participants -- an array of Participant objects (array size: MAX_SIZE).
    • numCandidates -- the number of candidates.
    • numVoters -- the number of voters.
    Note: the candidates will occupy the first numCandidates elements in the voters array, while the other voters will occupy the next numVoters elements in the voters array.

You should implement the 3 functions for each phrase:

  • setup() -- the setup phase which it register the details of the candidates, who are eligible to vote as well, and the other people who will be eligible to vote.
  • voting() -- the voting phase which only eligible voters should be able to cast the votes to eligible candidates.
    • The voter should have already be registered in the election (existed).
    • The voter should have not yet voted.
    • The candidate should have already be registered in the election (existed).
    • Increment the number of votes for the corresponding candidate.
    • Update the voting status (i.e. hasVoted member).
  • resultTallying() -- the result-tallying phase which output the final count for each candidate and announce the winner.
    • Print the number of votes for each candidate.
    • Find and output the one with the maximum number of votes, who is the winner.
  • Note: You don't need to worry about the input validation too much. Just try to implement a voting systems that you can add some candidates and voters, allow voters to vote, and announce the result of the election. That will be great!

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;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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;
}

____________________

Add a comment
Know the answer?
Add Answer to:
Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

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
  • To combat election fraud, your city is instituting a new voting procedure. The ballot has a...

    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>...

    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,...

  • 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...

ADVERTISEMENT
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT