Results 1 to 15 of 15
 08052008, 06:54 PM #1Member
 Join Date
 Jul 2008
 Posts
 8
 Rep Power
 0
Finding the largest number in an array
I am writing code for an assignment. There is one last part that I can not figure out. I need to be able to find the largest item in an array. I have a parallel array and need to find the largest number in one array list and have it show who the largest number belongs to in the other array list.
Here is my code thus far:
Java Code:import java.io.*; import java.util.Scanner; public class CH09EX07 { /** * @param args */ public static void main(String[] args) throws FileNotFoundException { Scanner inFile = new Scanner (new FileReader ("chapt09\\CH09EX07data.txt")); PrintWriter outFile = new PrintWriter("chapt09//CH09EX07.out"); String[] candidate = new String[5]; //parallel array int[] votes = new int[5]; double percentOfCandidates = 0; int numOfCandidates = 0; while (inFile.hasNext() && numOfCandidates < 5){ //loop enters the items from the input file into the two array lists. //it then prints both lists and shows the percent of votes that each candidate received from the total. candidate[numOfCandidates] = inFile.next(); System.out.printf("%2s", candidate[numOfCandidates]); votes[numOfCandidates] = inFile.nextInt(); System.out.printf("%13d", votes[numOfCandidates]); percentOfCandidates = (votes[numOfCandidates]); System.out.printf("%20.2f %n", percentOfCandidates / 220 * 100); numOfCandidates++; } int total = 0; //calculates the total number of votes. int counter; for (counter = 0; counter < votes.length; counter++) total = total + votes[counter]; System.out.println(); System.out.println("The total number of votes: " + total); } }
Java Code:Grumpy 50 Sleepy 40 Sneezy 60 Bashful 25 Doc 45
Java Code:Grumpy 50 22.73 Sleepy 40 18.18 Sneezy 60 27.27 Bashful 25 11.36 Doc 45 20.45 The total number of votes: 220
The winner of the Election is: Sneezy
Thanks for any advice, tips, and help that you can give me.
 08052008, 08:02 PM #2
To find the max value in an array, you need to save the current max value in a variable for comparions. Start that at a value smaller than the smallest possible value, say 1. Then in a loop get the values one at a time and compare that value with the saved one. If the new one is larger than that saved one (it should be the first time) then save that new value in the current max value variable and also save the current index of the array you are going thru in the loop.
 08062008, 10:17 AM #3
Hi,
There are many ways of doing it.
That was one of the way what norm specified.
We can even get the biggest number in 1 array in a variable say big and then compare it with the elements of second array.
But this way would require more number of lines so better to go by the way norm suggested.To finish sooner, take your own time....
Nivedithaaaa
 08062008, 11:56 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Something like this,
Java Code:int maximum = t[0]; // first value of the array int index = 0; for (int i=1; i<t.length; i++) { if (t[i] > maximum) { maximum = t[i]; // maximum index = i; // comparing index } }
 08062008, 01:41 PM #5Member
 Join Date
 Aug 2008
 Posts
 22
 Rep Power
 0
use a 2 dimensional array with the width of 2
the first field is the votes the second is the candidate number
after filling the array use Arrays.sort on the array and get the last element from the returned array
that will be the highest votes with the index of the candidatevisit http://www.thejavacode.com
Muhammad Safwat
 08112008, 03:42 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Yes, it's the best. But here the issue is our thread starter has two parallel arrays. May be that's form his implementation approach in his application.
 10312010, 02:17 PM #7Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
use bubble sort and link the vote to its corresponding candidate like this
for(int i=1; i<vote.length; i++)
for(int j=0; j<vote.length1; j++)
if(vote[j]<vote[j+1]){
temp_vote=vote[j];
vote[j]=vote[j+1];
vote[j+1]=temp_vote;
temp_candidate=candidate[j];
candidate[j]=candidate[j+1];
candidate[j+1]=temp_candidate;
}Last edited by amro; 10312010 at 05:47 PM.
 10312010, 02:22 PM #8Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
the largest vote will be the last vote and its corresponding canditate will have the same index in the second array
regards
 10312010, 05:42 PM #9
@amro
Some comments on the code you posted:
use code tags to preserve the formatting. Info here:Java Forums  BB Code List
Use {}s with the for loops to be sure nesting is correct and easily seen
 10312010, 06:02 PM #10Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
@Norm
use code tags to preserve the formatting.
Use {}s
 10312010, 06:10 PM #11Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
i have one question
what if the array is not full
is there any thing like getArrayMax ???
 11022010, 06:45 AM #12
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 11022010, 06:47 AM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
If you've a question please start a new thread in the correct subforum.
 11022010, 06:38 PM #14Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
 11032010, 07:49 AM #15
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
But the OP has no interest at all.
Similar Threads

Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11082010, 09:25 PM 
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06102008, 03:53 AM 
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01122008, 11:30 PM 
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12122007, 01:47 PM 
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11292007, 01:49 PM
Bookmarks