|
|
|
|
|
|
|
void WritePerCandidate(
/* in */ const VoteTable votes, // Total votes
/* in */ const String10 name[], // Candidate names
/* inout */ ofstream& reportFile ) // Output file
// Sums the votes per person and writes the totals to the
// report file
// Precondition:
// votes[0..NUM_PRECINCTS-1][0..NUM_CANDIDATES] are assigned
// && name[0..NUM_CANDIDATES-1] are assigned
// Postcondition:
// For each person i, name[i] has been output,
// followed by the sum
// votes[0][i] + votes[1][i] + + votes[NUM_PRECINCTS-1][i]
{
int precinct; // Loop counter
int candidate; // Loop counter
int total; // Total votes for a candidate
for (candidate = 0; candidate < NUM_CANDIDATES; candidate++)
{
// Invariant (prior to test):
// Votes for candidates 0 through (candidate-1) have
// been summed and output
// && 0 <= candidate <= NUM_CANDIDATES
total = 0;
// Compute column sum
for (precinct = 0; precinct < NUM_PRECINCTS; precinct++)
// Invariant (prior to test):
// total == votes[0][candidate] + +
// votes[precinct-1][candidate]
// && 0 <= precinct <= NUM_PRECINCTS
total = total + votes[precinct][candidate];
reportFile << Total votes for
<< setw(10) << name[candidate] << :
<< setw(3) << total << endl;
}
reportFile << endl;
} |
|
|
|
|
|