Programming Assignment 8
Programming Assignment 8
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
void printHeading();
int main()
{
//Declare variables; Step 1
vector<string> candidatesName(NO_OF_CANDIDATES);
int votesByRegion[NO_OF_CANDIDATES][NO_OF_REGIONS];
vector<int> totalVotes(NO_OF_CANDIDATES);
ifstream infile;
infile.open("candData.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Input file (candData.txt) does "
<< "not exit." << endl;
return 1;
}
getCandidatesName(infile, candidatesName,
NO_OF_CANDIDATES); //Step 4
sortCandidatesName(candidatesName,
NO_OF_CANDIDATES); //Step 5
infile.close(); //Step 6
infile.clear(); //Step 6
infile.open("voteData.txt"); //Step 7
if (!infile) //Step 8
{
cout << "Input file (voteData.txt) does "
<< "not exist." << endl;
return 1;
}
initialize(votesByRegion, totalVotes,
NO_OF_CANDIDATES); //Step 9
processVotes(infile, candidatesName,
votesByRegion, NO_OF_CANDIDATES); //Step 10
addRegionsVote(votesByRegion, totalVotes,
NO_OF_CANDIDATES); //Step 11
printHeading(); //Step 12
printResults(candidatesName,votesByRegion,
totalVotes, NO_OF_CANDIDATES); //Step 13
system("pause");
return 0;
}
do
{
cNames[location] = cNames[location - 1];
location--;
}
while (location > 0 && cNames[location - 1] > temp);
cNames[location] = temp;
}
}
if (cNames[mid] == name)
found = true;
else if (cNames[mid] > name)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
while (inp)
{
loc = binSearch(cNames, noOfRows, candName);
if (loc != -1)
vbRegion[loc][region - 1] = vbRegion[loc][region - 1]
+ noOfVotes;
inp >> candName >> region >> noOfVotes;
}
}
void printHeading()
{
cout << " --------------Election Results----------"
<< "----" << endl << endl;
cout << "Candidate Votes" << endl;
cout << "Name Region1 Region2 Region3 "
<< "Region4 Total" << endl;
cout << "--------- ------- ------- ------- "
<< "------- ------" << endl;
}
cout << endl << endl << "Winner: " << cNames[winLoc]
<< ", Votes Received: " << tVotes[winLoc]
<< endl << endl;
cout << "Total votes polled: " << sumVotes << endl;
}