Please be original and include working screenshot. In the Programming Example El
ID: 3661733 • Letter: P
Question
Please be original and include working screenshot.
In the Programming Example Election Results, the class candidateType contains the function calculateTotalVotes. After processing the voting data, this function calculates the total number ofvotes received by a candidate. The function updateVotesByRegion (of the class candidateType) updates only the number ofvotes for a particular region. Modify the definition ofthis function so that it also updates the total number ofvotes received by the candidate. By doing so, the function addVotes in the main program is no longer needed. Modify and run your program with the modified definition of the function updateVotesByRegion. Here are the code provided.
#include <iostream>
#include <string>
#include <iomanip>
#include "candidateType.h"
using namespace std;
void candidateType::setVotes(int region, int votes)
{
votesByRegion[region - 1] = votes;
}
void candidateType::updateVotesByRegion(int region, int votes)
{
votesByRegion[region - 1] = votesByRegion[region - 1] + votes;
}
void candidateType::calculateTotalVotes()
{
totalVotes = 0;
for (int i = 0; i < NO_OF_REGIONS; i++)
totalVotes += votesByRegion[i];
}
int candidateType::getTotalVotes() const
{
return totalVotes;
}
void candidateType::printData() const
{
cout << left
<< setw(10) << firstName << " "
<< setw(10) << lastName << " ";
cout << right;
for (int i = 0; i < NO_OF_REGIONS; i++)
cout << setw(7) << votesByRegion[i] << " ";
cout << setw(7) << totalVotes << endl;
}
candidateType::candidateType()
{
for (int i = 0; i < NO_OF_REGIONS; i++)
votesByRegion[i] = 0;
totalVotes = 0;
}
bool candidateType::operator==(const candidateType& right) const
{
return(firstName == right.firstName
&& lastName == right.lastName);
}
bool candidateType::operator!=(const candidateType& right) const
{
cout << "See Programming Exercise 13." << endl;
return false;
}
bool candidateType::operator<=(const candidateType& right) const
{
cout << "See Programming Exercise 13." << endl;
return false;
}
bool candidateType::operator<(const candidateType& right) const
{
cout << "See Programming Exercise 13." << endl;
return false;
}
bool candidateType::operator>=(const candidateType& right) const
{
cout << "See Programming Exercise 13." << endl;
return false;
}
bool candidateType::operator>(const candidateType& right) const
{
cout << "See Programming Exercise 13." << endl;
return false;
}
const candidateType& candidateType::operator=(const candidateType& right)
{
cout << "See Programming Exercise 13." << endl;
return *this;
}
const candidateType& candidateType::operator=(const personType& right)
{
cout << "See Programming Exercise 13." << endl;
return *this;
}
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
void personType::setName(string first, string last)
{
cout << "See Programming Exercise 13" << endl;
}
string personType::getFirstName() const
{
cout << "See Programming Exercise 13" << endl;
return "";
}
string personType::getLastName() const
{
cout << "See Programming Exercise 13" << endl;
return "";
}
//constructor
personType::personType(string first, string last)
{
cout << "See Programming Exercise 13" << endl;
}
const personType& personType::operator=(const personType& right)
{
cout << "See Programming Exercise 13" << endl;
return *this;
}
//overload the operator ==
bool personType::operator==(const personType& right) const
{
return (firstName == right.firstName
&& lastName == right.lastName); }
bool personType::operator!=(const personType& right) const
{
cout << "See Programming Exercise 13" << endl;
return false;
}
bool personType::operator<=(const personType& right) const
{
cout << "See Programming Exercise 13" << endl;
return false;
}
bool personType::operator<(const personType& right) const
{
cout << "See Programming Exercise 13" << endl;
return false;
}
bool personType::operator>=(const personType& right) const
{
cout << "See Programming Exercise 13" << endl;
return false;
}
bool personType::operator>(const personType& right) const
{
cout << "See Programming Exercise 13" << endl;
return false;
}
istream& operator>>(istream& is, personType& pName)
{
is>>pName.firstName>>pName.lastName;
return is;
}
ostream& operator<<(ostream& os, const personType& pName)
{
cout << "See Programming Exercise 13" << endl;
return os;
}
#include <iostream>
#include <string>
#include <fstream>
#include "candidateType.h"
#include "orderedArrayListType.h"
using namespace std;
const int NO_OF_CANDIDATES = 6;
void fillNames(ifstream& inFile,
orderedArrayListType<candidateType>& cList);
void processVotes(ifstream& inFile,
orderedArrayListType<candidateType>& cList);
void addVotes(orderedArrayListType<candidateType>& cList);
void printHeading();
void printResults(orderedArrayListType<candidateType>& cList);
int main()
{
orderedArrayListType<candidateType> candidateList(NO_OF_CANDIDATES);
ifstream inFile;
inFile.open("candData.txt");
fillNames(inFile, candidateList);
candidateList.selectionSort();
inFile.close();
inFile.open("voteData.txt");
processVotes(inFile, candidateList);
addVotes(candidateList);
printHeading();
printResults(candidateList);
return 0;
}
void fillNames(ifstream& inFile,
orderedArrayListType<candidateType>& cList) { string firstN; string lastN;
candidateType temp;
for (int i = 0; i < NO_OF_CANDIDATES; i++)
{
inFile >> firstN >> lastN;
temp.setName(firstN, lastN);
cList.insertAt(i, temp);
}
}
void processVotes(ifstream& inFile,
orderedArrayListType<candidateType>& cList)
{
cout << "See Programming Exercise 13" << endl;
}
void addVotes(orderedArrayListType<candidateType>& cList)
{
candidateType temp;
for (int i = 0; i < NO_OF_CANDIDATES; i++)
{
cList.retrieveAt(i, temp);
temp.calculateTotalVotes();
cList.replaceAt(i, temp);
}
}
void printHeading()
{
cout << " --------------------Election Results---------"
<< "-----------" << endl << endl;
cout << " Votes" << endl;
cout << " Candidate Name Region1 Region2 Region3 "
<<"Region4 Total" << endl;
cout << "--------------------- ------- ------- "
<< "------- ------- ------" << endl;
}
void printResults(orderedArrayListType<candidateType>& cList)
{
cout << "See Programming Exercise 13" << endl;
}
Explanation / Answer
Only change required is calling the calculateTotalVotes function inside the updateVotesByRegion after updating the votesByRegion i.e
void candidateType::updateVotesByRegion(int region, int votes)
{
votesByRegion[region - 1] = votesByRegion[region - 1] + votes;
calculateTotalVotes();
}
by doing this we don't need to call addVotes everytime we updateVotesByRegion