C Learning Objectives The objective of this lab is to gain e
C++
Learning Objectives:
The objective of this lab is
to gain experience working with an
STL container, in particular the STL list.
Note: STL <algorithm>
sort()
cannot be used
with
list
(
list
provides its own
sort()
operation).
Assignment
:
Write a program that allows the user to enter the last names of five
candidates in a local election and the votes received by each candidate. The program
should then output each candidate’s name, the votes received by that candidate, and th
e
percentage of the total votes received by the candidate. Your program should also output
the winner of the election. A sample output is
Candidate
Votes Received
% of Total Votes
Johnson
5000
25.91
Miller
4000
20.72
Duffy
6000
31.09
Ro
binson
2500
12.95
Anthony
1800
9.33
Total
19300
The Winner of the Election is Duffy.
First design a
Vote structure
to hold candidate, votes received and percentage of total
votes. Then use
a STL list of Vote structure to implement the pro
gram
. Whenever
possible, you should
use STL functions and/or iterators to accomplish the necessary
operations
. Otherwise, you can not get credit for your implementation. Design the
program top
-
down, with only function calls in main, and function calls w
ithin functions.
Solution
#include <iostream>
 #include <list>
 #include <cmath>
 using namespace std;
#define TOTAL_CANDIDATES 5
 struct Vote{
    string candidateName;
    int votesReceived;
    float percentageOfTotalVotes;
    Vote( string name, int votesR ){
        candidateName = name;
        votesReceived = votesR;
    }
 };
list< Vote > candidateList;
void getInput(){
    string candidateName;
    int votesReceived;
    for(int cnd = 0; cnd < TOTAL_CANDIDATES; cnd++ )
    {
        cout << \"Enter candidate name: \";
        cin >> candidateName;
        cout << \"Enter votes received by the candidate: \";
        cin >> votesReceived;
       Vote newVote = Vote( candidateName, votesReceived );
        candidateList.push_back( newVote );
    }
 }
inline float roundItUpToTwoDecimal( float num ){
    float result = num*100*100;
    return round(result)/100;
 }
int updatePercentageOfVotesAndReturnTotalVotes(){
    //first calculating total votes, need to iterate over our list
    int totalVotes = 0;
    for( list<Vote>::iterator it = candidateList.begin(); it != candidateList.end(); it++ ){
        totalVotes = totalVotes + (*it).votesReceived;
    }
    //now update percentage of votes for each candidate
    for( list<Vote>::iterator it = candidateList.begin(); it != candidateList.end(); it++ ){
        float percentageOfVotes = (((*it).votesReceived)*1.0)/totalVotes;
        //rounding it up to have two decimal spaces
        (*it).percentageOfTotalVotes = roundItUpToTwoDecimal( percentageOfVotes );
    }
    return totalVotes;
 }
void printOutput( int totalVotes ){
    cout << \"Candidate\ Votes Received\ \\% of Total Votes\ \";
    string winner = \"None\";
    int winnerVotes = -100;
   for( list<Vote>::iterator it = candidateList.begin(); it != candidateList.end(); it++ ){
        cout << (*it).candidateName << \"\ \" << (*it).votesReceived << \"\ \";
        cout << (*it).percentageOfTotalVotes << endl;
        if( (*it).votesReceived > winnerVotes ){
            winner = (*it).candidateName;
            winnerVotes = (*it).votesReceived;
        }
    }
    cout << \"Total\ \" << totalVotes << endl;  
    cout << \"The Winner of the Election is \" << winner << \".\"<< endl;
    return;
 }
int main(){
    getInput(); //got the input, that was required
    int totalVotes = updatePercentageOfVotesAndReturnTotalVotes(); //to update percentage of total votes for each candidate
    printOutput( totalVotes ); //print the output, as required
    return 0;
 }




