Java Reference
In-Depth Information
That is, each line in the file voteData.txt consists of the candidate's name, region
number, and the votes received by the candidate in this region. There is one entry
per line. For example, the input file containing the voting data looks like:
Mia 2 34
Mickey 1 56
Donald 2 56
Mia 1 78
Danny 4 29
Ashley 4 78
.
.
.
The first line indicates that Mia received 34 votes from region 2 .
Two files, one containing the candidates' names and the other containing the
voting data, as described previously.
Input:
Output: The election results in a tabular form, as described previously, and the winner.
Looking at the output, it is clear that the program must organize the voting data by
regions. The program must also calculate the total votes received by each candidate
and the total votes polled for the election. Furthermore, the names of the candidates
must appear in alphabetical order.
Because the data type of a candidate's name (which is a string) and the data type of the
number of votes (which is an integer) are different, we need separate arrays—one to hold
the candidates' names and the other to hold the voting data. The array to hold the names
of the candidates is a one-dimensional array, and each element of this array is a string.
Instead of using one two-dimensional array to hold the voting data, we will use a two-
dimensional array to hold the next four columns of the output, that is, the regional
voting data, and we will use a one-dimensional array to hold the total votes received by
each candidate. These three arrays are parallel arrays (see Figure 14-16).
PROBLEM
ANALYSIS AND
ALGORITHM
DESIGN
votesByRegion
candidatesName
totalVotes
[0]
[1]
[3]
[2]
[0]
[0]
[0]
[1]
[1]
[1]
[2]
[2]
[2]
[3]
[3]
[3]
[4]
[4]
[4]
[5]
[5]
[5]
FIGURE 14-16 Parallel arrays candidatesName , votesByRegion , and totalVotes
Search WWH ::




Custom Search