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