Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Computing total votes from a 2d array...

1. ## Computing total votes from a 2d array...

I am trying to make a program that will add the total votes for a precinct from each candidate. Also I want to make it list the total votes casted for each canidadte. The following code i have so far is just showing the table. If you look below the code there is the output similar to what I am trying to do. I am just not sure how to the total votes for each precinct and for the canidates.

Code:
```   public static void main(String[] args)
{
int[][] vote = {{192, 48, 206, 37},
{147, 90, 312, 21},
{186, 12, 121, 38},
{114, 21, 408, 39},
{267, 13, 382, 29}};

int precinct;
int candidate;

// Could use a 1-D array for precinct and candidate data,
// but no need for such arrays
int precinctTotal;
int candidateTotal;

System.out.println("Here are the mayoral election results "
+ "by precinct and by candidate.\n");

// Print all of the data as a table
System.out.println("Precinct  Candidate 1\tCandidate 2\t"
+ "Candidate 3\tCandidate 4");
for (precinct = 0; precinct < NUM_PRECINCTS; precinct++)
{
System.out.print("   " + (precinct + 1) + "   \t      ");
for (candidate = 0; candidate < NUM_CANDIDATES; candidate++)
System.out.print(vote[precinct][candidate] + "    \t    ");
System.out.println();
}

// Next, print the totals for each precinct
System.out.println("\nThe results for each precinct:\n");

// Now, print the totals for each candidate
System.out.println("\nThe results for each candidate:\n");

}
}```
Here is what i am trying to do from the 2d array.

The results for each precinct:
Precinct 1: 483 votes
Precinct 2: 570 votes
Precinct 3: 357 votes
Precinct 4: 582 votes
Precinct 5: 691 votes

The results for each candidate:
Candidate 1: 906 votes
Candidate 2: 184 votes
Candidate 3: 1429 votes
Candidate 4: 164 votes

2. Like so?
Code:
```    public static void main(String[] args)
{
int[][] vote = {{192, 48, 206, 37},
{147, 90, 312, 21},
{186, 12, 121, 38},
{114, 21, 408, 39},
{267, 13, 382, 29}};

int[] precinctTotal = new int[vote.length];
int[] candidateTotal = new int[vote[0].length];

System.out.println("Here are the mayoral election results "
+ "by precinct and by candidate.\n");
System.out.println("Precinct  Candidate 1\tCandidate 2\t"
+ "Candidate 3\tCandidate 4");

for (int p = 0; p < vote.length; p++)
{
System.out.print((p + 1));
for (int c = 0; c < vote[p].length; c++)
{
precinctTotal[p] += vote[p][c];
candidateTotal[c] += vote[p][c];
System.out.print("\t" + vote[p][c]);
}
System.out.println();
}

System.out.println("\nThe results for each precinct:");
for (int i = 0; i < precinctTotal.length; i++)
{
System.out.println("Precinct " + (i + 1) + ": " + precinctTotal[i] + " votes");
}

System.out.println("\nThe results for each candidate:");
for (int j = 0; j < candidateTotal.length; j++)
{
System.out.println("Candidate " + (j + 1) + ": " + candidateTotal[j] + " votes");
}
}```
Output:
Code:
```Here are the mayoral election results by precinct and by candidate.

Precinct  Candidate 1	Candidate 2	Candidate 3	Candidate 4
1	192	48	206	37
2	147	90	312	21
3	186	12	121	38
4	114	21	408	39
5	267	13	382	29

The results for each precinct:
Precinct 1: 483 votes
Precinct 2: 570 votes
Precinct 3: 357 votes
Precinct 4: 582 votes
Precinct 5: 691 votes

The results for each candidate:
Candidate 1: 906 votes
Candidate 2: 184 votes
Candidate 3: 1429 votes
Candidate 4: 164 votes```
The only real difference is that I stored each one of the results into another array, otherwise its pretty much identical. There are several ways you can approach this problem though. My recommendation would be to seek an object collection and make use of a custom object for precincts and candidates. It will be tricky though. A matrix of these would be a good choice.

Oh yes, make sure you're try catching this as well. I just based my array sizes off of the votes array, but that will fail if the votes array doesn't have any items. Same goes for you're contants, they could fail if they step out of bounds on you're votes array. This is part of what makes these problems tricky (and why object usage is so much easier )

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•