Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Computing total votes from a 2d array...

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-12-2008, 10:33 PM PM User | #1 wutsittou New to the CF scene   Join Date: Nov 2008 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts 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
 11-13-2008, 08:25 AM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,597 Thanks: 4 Thanked 2,597 Times in 2,566 Posts 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 ) __________________ PHP Code: ``` header('HTTP/1.1 420 Enhance Your Calm');  ```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:09 AM.