Question: Please show the output from this code: #include #include #include #include candidateType.h #include orderedArrayListType.h using namespace std; const int noOfCandidates = 6; void fillNames(ifstream& inFile,

Please show the output from this code:

#include #include #include #include "candidateType.h" #include "orderedArrayListType.h"

using namespace std;

const int noOfCandidates = 6;

void fillNames(ifstream& inFile, orderedArrayListType& cList); void processVotes(ifstream& inFile, orderedArrayListType& cList); void addVotes(orderedArrayListType& cList);

void printHeading(); void printResults(orderedArrayListType& cList);

int main() { orderedArrayListType candidateList(noOfCandidates); candidateType temp; ifstream inFile; inFile.open("a:\\candData.txt"); if (!inFile) { cout << "Input file (candData.txt) does not exit. " << "Program terminates!!" << endl; return 1; } fillNames(inFile, candidateList); candidateList.selectionSort(); inFile.close(); inFile.clear(); inFile.open("a:\\voteData.txt"); if (!inFile) { cout << "Input file (voteData.txt) does not exit. " << "Program terminates!!" << endl; return 1; } processVotes(inFile, candidateList); addVotes(candidateList); printHeading(); printResults(candidateList);

return 0; }

void fillNames(ifstream& inFile, orderedArrayListType& cList) { string firstN; string lastN; int i; candidateType temp;

for (i = 0; i < noOfCandidates; i++) { inFile >> firstN >> lastN; temp.setName(firstN, lastN); cList.insertAt(i, temp); } }

void processVotes(ifstream& inFile, orderedArrayListType& cList) { string firstN; string lastN; int region; int votes; int candLocation; candidateType temp; inFile >> firstN >> lastN >> region >> votes; temp.setName(firstN, lastN); temp.setVotes(region, votes); while (inFile) { candLocation = cList.binarySearch(temp);

if (candLocation != -1) { cList.retrieveAt(candLocation, temp); temp.updateVotesByRegion(region, votes); cList.replaceAt(candLocation, temp); } inFile >> firstN >> lastN >> region >> votes; temp.setName(firstN, lastN); temp.setVotes(region, votes); } } void addVotes(orderedArrayListType& cList) { int i; candidateType temp; for (i = 0; i < noOfCandidates; 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"<

void printResults(orderedArrayListType& cList) { int i; candidateType temp; int largestVotes = 0; int sumVotes = 0; int winLoc = 0; for (i = 0; i < noOfCandidates; i++) { cList.retrieveAt(i, temp); temp.printData(); sumVotes += temp.getTotalVotes(); if (largestVotes < temp.getTotalVotes()) { largestVotes = temp.getTotalVotes(); winLoc = i; } } cList.retrieveAt(winLoc, temp); cout << endl << "Winner: "; cout << temp.getFirstName() << " " << temp.getLastName(); cout << ", Votes Received: " << temp.getTotalVotes() << endl << endl; cout << "Total votes polled: " << sumVotes << endl; } arrayListType.h

#ifndef H_arrayListType #define H_arrayListType

#include #include

using namespace std;

template class arrayListType { public: const arrayListType& operator=(const arrayListType&); bool isEmpty() const; bool isFull() const; int listSize() const; int maxListSize() const; void print() const; bool isItemAtEqual(int location, const elemType& item) const; void insertAt(int location, const elemType& insertItem); void insertEnd(const elemType& insertItem); void removeAt(int location); void retrieveAt(int location, elemType& retItem); void replaceAt(int location, const elemType& repItem); void clearList(); int seqSearch(const elemType& item) const; void insert(const elemType& insertItem); void remove(const elemType& removeItem); arrayListType(int size = 100); arrayListType (const arrayListType< elemType >& otherList); //copy constructor ~arrayListType(); protected: elemType *list; //array to hold the list elements int length; //variable to store the length of the list int maxSize; //variable to store the maximum //size of the list };

template bool arrayListType::isEmpty() const { return (length == 0); }

template bool arrayListType::isFull() const { return (length == maxSize); }

template int arrayListType::listSize() const { return length; }

template int arrayListType::maxListSize() const { return maxSize; }

template void arrayListType::print() const { int i;

for (i = 0; i < length; i++) cout << list[i] << " "; cout << endl; }

template bool arrayListType::isItemAtEqual (int location, const elemType& item) const { return(list[location] == item); }

template void arrayListType::insertAt (int location, const elemType& insertItem) { int i;

if (location < 0 || location >= maxSize) cout << "The position of the item to be inserted " << "is out of range." << endl; else if (length >= maxSize) //list is full cout << "Cannot insert in a full list" << endl; else { for (i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down

list[location] = insertItem; //insert the item at the //specified position

length++; //increment the length } } //end insertAt

template void arrayListType::insertEnd(const elemType& insertItem) { if (length >= maxSize) //the list is full cout << "Cannot insert in a full list." << endl; else { list[length] = insertItem; //insert the item at the end length++; //increment length } } //end insertEnd

template void arrayListType::removeAt(int location) { int i;

if (location < 0 || location >= length) cout << "The location of the item to be removed " << "is out of range." << endl; else { for (i = location; i < length - 1; i++) list[i] = list[i+1];

length--; } } //end removeAt

template void arrayListType::retrieveAt (int location, elemType& retItem) { if (location < 0 || location >= length) cout << "The location of the item to be retrieved is " << "out of range." << endl; else retItem = list[location]; } // retrieveAt

template void arrayListType::replaceAt (int location, const elemType& repItem) { if (location < 0 || location >= length) cout << "The location of the item to be replaced is " << "out of range." << endl; else list[location] = repItem;

} //end replaceAt

template void arrayListType::clearList() { length = 0; } // end clearList

template int arrayListType::seqSearch(const elemType& item) const { int loc; bool found = false;

for (loc = 0; loc < length; loc++) if ( list[loc] == item) { found = true; break; }

if (found) return loc; else return -1; } //end seqSearch

template void arrayListType::insert(const elemType& insertItem) { int loc;

if (length == 0) //list is empty list[length++] = insertItem; //insert the item and //increment the length else if (length == maxSize) cout << "Cannot insert in a full list." << endl; else { loc = seqSearch(insertItem);

if (loc == -1) //the item to be inserted //does not exist in the list list[length++] = insertItem; else cout << "the item to be inserted is already in " << "the list. No duplicates are allowed." << endl; } } //end insert

template void arrayListType::remove(const elemType& removeItem) { int loc;

if (length == 0) cout << "Cannot delete from an empty list." << endl; else { loc = seqSearch(removeItem);

if (loc != -1) removeAt(loc); else cout << "The tem to be deleted is not in the list." << endl; }

} //end remove

template arrayListType::arrayListType(int size) { if (size <= 0) { cout << "The array size must be positive. Creating " << "an array of size 100. " << endl;

maxSize = 100; } else maxSize = size;

length = 0;

list = new elemType[maxSize]; assert(list != NULL); }

template arrayListType::~arrayListType() { delete [] list; }

//copy constructor template arrayListType::arrayListType (const arrayListType& otherList) { maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //terminate if unable to allocate //memory space

for (int j = 0; j < length; j++) //copy otherList list [j] = otherList.list[j]; }//end copy constructor

template const arrayListType& arrayListType::operator= (const arrayListType& otherList) { if (this != &otherList) //avoid self-assignment { delete [] list; maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; assert(list != NULL);

for (int i = 0; i < length; i++) list[i] = otherList.list[i]; }

return *this; }

#endif

candidateType.h

#ifndef H_candidateType #define H_candidateType

#include #include "personType.h"

const int noOfRegions = 4;

class candidateType: public personType { public: const candidateType& operator=(const candidateType&); const candidateType& operator=(const personType&); void updateVotesByRegion(int region, int votes); void setVotes(int region, int votes); void calculateTotalVotes(); int getTotalVotes() const; void printData() const; candidateType(); bool operator==(const candidateType& right) const; bool operator!=(const candidateType& right) const; bool operator<=(const candidateType& right) const; bool operator<(const candidateType& right) const; bool operator>=(const candidateType& right) const; bool operator>(const candidateType& right) const;

private: int votesByRegion[noOfRegions]; //array to store the //votes received in //each region int totalVotes; //variable to store the total votes };

#endif

candidateTypeImp.cpp

#include #include #include #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() { int i; totalVotes = 0;

for(i = 0; i < noOfRegions; 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 < noOfRegions; i++) cout << setw(8) << votesByRegion[i] << " "; cout << setw(7) << totalVotes << endl; }

candidateType::candidateType() { for (int i = 0; i < noOfRegions; 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 { return(firstName != right.firstName && lastName != right.lastName); }

bool candidateType::operator<=(const candidateType& right) const { return(lastName <= right.lastName || (lastName == right.lastName && firstName <= right.firstName)); }

bool candidateType::operator<(const candidateType& right) const { return(lastName < right.lastName || (lastName == right.lastName && firstName < right.firstName)); }

bool candidateType::operator>=(const candidateType& right) const { return(lastName >= right.lastName || (lastName == right.lastName && firstName >= right.firstName)); }

bool candidateType::operator>(const candidateType& right) const { return(lastName > right.lastName || (lastName == right.lastName && firstName > right.firstName)); }

const candidateType& candidateType::operator= (const candidateType& right) { if (this != &right) // avoid self-assignment { firstName = right.firstName; lastName = right.lastName;

for (int i = 0; i < noOfRegions; i++) votesByRegion[i] = right.votesByRegion[i];

totalVotes = right.totalVotes; } return *this; }

const candidateType& candidateType::operator= (const personType& right) { firstName = right.getFirstName(); lastName = right.getLastName();

return *this; }

orderedArrayListType.h

#include #include #include #include "candidateType.h" #include "orderedArrayListType.h"

using namespace std;

const int noOfCandidates = 6;

void fillNames(ifstream& inFile, orderedArrayListType& cList); void processVotes(ifstream& inFile, orderedArrayListType& cList); void addVotes(orderedArrayListType& cList);

void printHeading(); void printResults(orderedArrayListType& cList);

int main() { orderedArrayListType candidateList(noOfCandidates); candidateType temp; ifstream inFile; inFile.open("a:\\candData.txt"); if (!inFile) { cout << "Input file (candData.txt) does not exit. " << "Program terminates!!" << endl; return 1; } fillNames(inFile, candidateList); candidateList.selectionSort(); inFile.close(); inFile.clear(); inFile.open("a:\\voteData.txt"); if (!inFile) { cout << "Input file (voteData.txt) does not exit. " << "Program terminates!!" << endl; return 1; } processVotes(inFile, candidateList); addVotes(candidateList); printHeading(); printResults(candidateList);

return 0; }

void fillNames(ifstream& inFile, orderedArrayListType& cList) { string firstN; string lastN; int i; candidateType temp;

for (i = 0; i < noOfCandidates; i++) { inFile >> firstN >> lastN; temp.setName(firstN, lastN); cList.insertAt(i, temp); } }

void processVotes(ifstream& inFile, orderedArrayListType& cList) { string firstN; string lastN; int region; int votes; int candLocation; candidateType temp; inFile >> firstN >> lastN >> region >> votes; temp.setName(firstN, lastN); temp.setVotes(region, votes); while (inFile) { candLocation = cList.binarySearch(temp);

if (candLocation != -1) { cList.retrieveAt(candLocation, temp); temp.updateVotesByRegion(region, votes); cList.replaceAt(candLocation, temp); } inFile >> firstN >> lastN >> region >> votes; temp.setName(firstN, lastN); temp.setVotes(region, votes); } } void addVotes(orderedArrayListType& cList) { int i; candidateType temp; for (i = 0; i < noOfCandidates; 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"<

void printResults(orderedArrayListType& cList) { int i; candidateType temp; int largestVotes = 0; int sumVotes = 0; int winLoc = 0; for (i = 0; i < noOfCandidates; i++) { cList.retrieveAt(i, temp); temp.printData(); sumVotes += temp.getTotalVotes(); if (largestVotes < temp.getTotalVotes()) { largestVotes = temp.getTotalVotes(); winLoc = i; } } cList.retrieveAt(winLoc, temp); cout << endl << "Winner: "; cout << temp.getFirstName() << " " << temp.getLastName(); cout << ", Votes Received: " << temp.getTotalVotes() << endl << endl; cout << "Total votes polled: " << sumVotes << endl; }

personType.h

#ifndef personType_H #define personType_H

#include

using namespace std;

class personType { friend istream& operator>>(istream&, personType&); friend ostream& operator<<(ostream&, const personType&);

public: void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); bool operator==(const personType& right) const; bool operator!=(const personType& right) const; bool operator<=(const personType& right) const; bool operator<(const personType& right) const; bool operator>=(const personType& right) const; bool operator>(const personType& right) const;

protected: string firstName; //variable to store the first name string lastName; //variable to store the last name };

#endif

personTypeImp.cpp

#include #include #include "personType.h"

using namespace std;

void personType::setName(string first, string last) { firstName = first; lastName = last; }

string personType::getFirstName() const { return firstName; }

string personType::getLastName() const { return lastName; }

//constructor personType::personType(string first, string last)

{ firstName = first; lastName = last; }

bool personType::operator==(const personType& right) const { return(firstName == right.firstName && lastName == right.lastName); }

bool personType::operator!=(const personType& right) const { return(firstName != right.firstName && lastName != right.lastName); }

bool personType::operator<=(const personType& right) const { return(lastName <= right.lastName || (lastName == right.lastName && firstName <= right.firstName)); }

bool personType::operator<(const personType& right) const { return(lastName < right.lastName || (lastName == right.lastName && firstName < right.firstName)); }

bool personType::operator>=(const personType& right) const { return(lastName >= right.lastName || (lastName == right.lastName && firstName >= right.firstName)); }

bool personType::operator>(const personType& right) const { return(lastName > right.lastName || (lastName == right.lastName && firstName > right.firstName)); }

istream& operator>>(istream& isObject, personType& pName) { isObject >> pName.firstName >> pName.lastName; return isObject; }

ostream& operator<<(ostream& osObject, const personType& pName) { osObject << pName.firstName << " " << pName.lastName; return osObject; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!