Question

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

4. MEASURE 2

    H. YES

    I    NO

After submitting the ballot, every voter receives a receipt that has a unique ID number and a record of the voting selections. For example, a voter who submits a vote for Sue, Perman, Yes on Proposition 17, No on Measure 1 and Yes on Measure 2 might receive a receipt with:-

ID 4925    :    CDGH

The next day, the city posts all votes on its web page sorted by ID Number. This allows a voter to confirm their submission and allows anyone to count the vote totals for themselves. A sample list of for the sample ballot is shown below:-

ID   VOTES
4925   CDGH
4926   AEGH
4927   CDGI
4928   BEGI
4929   ADFH
Construct a C++ program that reads the posted voting list from a file named Vote.txtand outputs the percent of votes cast for each ballot item. File contains a list of Id numbers and a string containing votes (column B from the table).

Task:-

Define a class named Nofraud that stores an individuals voting record. The class should have a constructor that takes as input a string of votes for example "CDGH", a voter Id, an accessor function(s) that return the person's Id and vote for a specific question. Store each Voter instance in an array or vector.

Your program should iterate over the array to compute and output the percent of votes cast for each candidate, proposition and measure. It should then prompt the user to enter a voter Id, iterate over the list again to find the object with that ID and print his or her votes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE : First create a file named as "Votes.txt"(copy and paste the data below) in the same directory as that of the program.

ID   VOTES
4925   CDGH
4926   AEGH
4927   CDGI
4928   BEGI
4929   ADFH

Then run the program and see the result.

CODE :

#include<bits/stdc++.h>
using namespace std;
const int MAX = 5;
class Nofraud {
   int id;
   string votes;
   public :
   Nofraud(int id, string votes) {
       this -> id = id;
       this -> votes = votes;
   }
   char getvote(int question) {
       return votes[question];
   }
   int getid() {
       return id;
   }
};

int main() {
   vector<Nofraud> obj;
   ifstream inFile;
    inFile.open("Votes.txt");
   string useless;
   inFile >> useless >> useless;
   int id;
   string votes;
   for (int i = 0; i < MAX; ++i) {
       inFile >> id >> votes;
       obj.push_back(Nofraud(id, votes));
   }
   inFile.close();
   int mayor[3];
   int prop17[2];
   int measure1[2];
   int measure2[2];
   for (int i = 0; i < 4; ++i) {
       mayor[i] = 0;
   }
   for (int i = 0; i < 2; ++i) {
       prop17[i] = 0;
       measure1[i] = 0;
       measure2[i] = 0;
   }
   for (int i = 0; i < 5; ++i) {
       for (int j = 0; j < 4; ++j) {
           char answer = obj[i].getvote(j);
           if (j == 0) {
               mayor[answer - 'A']++;
           } else if (j == 1) {
               prop17[answer - 'D']++;
           } else if (j == 2) {
               measure1[answer - 'F']++;
           } else if (j == 3) {
               measure2[answer - 'H']++;
           }
       }
   }
  
   int totalsum_mayor = 0;
   int totalsum_prop17 = 0;
   int totalsum_measure1 = 0;
   int totalsum_measure2 = 0;
   for (int i = 0; i < 3; ++i) {
       totalsum_mayor += mayor[i];
   }
   for (int i = 0; i < 2; ++i) {
       totalsum_prop17 += prop17[i];
       totalsum_measure1 += measure1[i];
       totalsum_measure2 += measure2[i];
   }
   cout << "\n\nFOR MAYOR CANDIDATES: \n";
   for (int i = 0; i < 3; ++i) {
       cout << char(i + 'A') << " got " << ((float) mayor[i] / totalsum_mayor) * 100 << " percentage of votes\n";
   }
   cout << "\n\nFOR PROP17: \n";
   for (int i = 0; i < 2; ++i) {
       cout << char(i + 'D') << " got " << ((float) prop17[i] / totalsum_prop17) * 100 << " percentage of votes\n";
   }
  
   cout << "\n\nFOR MEASURE1: \n";
   for (int i = 0; i < 2; ++i) {
       cout << char(i + 'F') << " got " << ((float) measure1[i] / totalsum_measure1) * 100 << " percentage of votes\n";
   }
  
   cout << "\n\nFOR MEASURE2: \n";
   for (int i = 0; i < 2; ++i) {
       cout << char(i + 'H') << " got " << ((float) measure2[i] / totalsum_measure2) * 100 << " percentage of votes\n";
   }
   int tid;
   cout << "\nEnter a ID to display the votes: ";
   cin >> tid;
   for (int i = 0; i < MAX; ++i) {
       if (tid == obj[i].getid()) {
           cout << tid << " voted for the following: \n";
           for (int j = 0; j < 4; ++j) {
               cout << obj[i].getvote(j);
           }
           break;
       }
   }
}

INPUT/ OUTPUT :

Votes.txt - Notepad File Edit Format View Help ID VOTES 4925 CDGH 4926 AEGH 4927 CDGI 4928 BEGI 4929 ADFH -OX

FOR MAYOR CANDIDATES: A got 48 percentage of votes got 28 percentage of votes C got 48 percentage of votes FOR PROP17: got 6e

Add a comment
Know the answer?
Add Answer to:
To combat election fraud, your city is instituting a new voting procedure. The ballot has a...
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
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