1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int sumList(const int list[], int size);
int indexOfMax(const int list[], int size);
// This function gets the results from the file and returns them in the corresponding
void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count);
void printResults(const string candidates[], const int ballots[], int count, int totalBallots);
const int NUM_CANDIDATES = 10; // The number of candidates
int main()
{
ifstream inFile;
string names[NUM_CANDIDATES]; // The names of the candidates
int votes[NUM_CANDIDATES]; // The number of votes received by each candidate
int totalVotes; // The total number of votes received by all candidates
int max; // number of candidates processed
inFile.open("votingresults.txt");
inFile.open("votingresults.txt");
if (!inFile)
{
cout << "File not found!" << endl << endl;
system("pause");
return 1;
}
///////////////////////////////
// Start of your code
// Get the candidates' names and number of votes each received
// Assume the user enters valid input (just call the getElectionData() function)
getElectionData(inFile, names, votes, max);
// Calculate the total number of votes received by all candidates
// (call the sumList() function)
totalVotes = sumList(votes ,max);
// Print each candidate's name, the number of votes she received,
// and the percent of votes the candidate received
// (just call the printResults() function)
printResults(names, votes, max, totalVotes);
inFile.close();
// End of your code in the main function
// Don't forget to complete the functions below
///////////////////////////////
return 0;
}
//
// Returns the sum of the numbers in the given array
//
// WRITE THE BODY OF THIS FUNCTION
//
int sumList(int list[], int size)
{
int i, sum = 0;
for(i = 0; i<size; i++)
{
sum = list[i] + sum;
}
return sum;
}
//
// Returns the index of the largest value in the given array (not the largest
// value, but the index of it)
//
// WRITE THE BODY OF THIS FUNCTION
//
int indexOfMax(int list[], int size)
{
int i;
int Max = 0, maxIndex= 0;
for(i = 0; i < size; i++) // loop 10 times
{
if(Max < list[i]) //if Max is lkess then the index it will store the new index as max
{
Max = list[i];
maxIndex = i;
}
}
return maxIndex;
}
// Gets the names of the candidates and the votes they got from the file and returns
// them in the corresponding arrays. It also returns the number of rows in the file that
// were processed
//
// WRITE THE BODY OF THIS FUNCTION
//
void getElectionData(ifstream & iFile, string candidates[], int ballots[], int & count)
{
int i = 0;
while(iFile >> candidates[i] >> ballots[i])
{
count = count + ballots[i];
i++;
}
}
// Receives the candidates names and the quantity of votes they got, the number of elements
// in the arrays to be processed, and the total number of votes so they can be displayed as
// shown in the file with the problem statement
// Must call the indexOfMax function() to find the array index of the candidate with the
// largest number of votes
//
// WRITE THE BODY OF THIS FUNCTION
//
void printResults(const string candidates[], const int ballots[], int count, int totalBallots)
{
int k;
cout << "Candidate" << setw(20) << "Num Votes" << setw(30) << "% of votes" << endl;
for(int i = 0; i<count; i++)
{
cout << candidates[i] << setw(20) << ballots[i] << setw(20) << totalBallots/count /*Goes here */<< endl;
}
k = indexOfMax(ballots, totalBallots);
cout << "The winner is: " << candidates[k] << endl << endl;
}
|