# Finding the largest number in an array

• 08-05-2008, 06:54 PM
starchildren3317
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:

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);                         } }```
Here is the input file:

Code:

```Grumpy 50 Sleepy 40 Sneezy 60 Bashful 25 Doc 45```
Here is how it looks right now (I still have to do a little format cleanup to make it look pretty):

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```
So I need some assistance so that the code figures out that Sneezy recieved the most votes and will print it out like so:

The winner of the Election is: Sneezy

Thanks for any advice, tips, and help that you can give me.
• 08-05-2008, 08:02 PM
Norm
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.
• 08-06-2008, 10:17 AM
Niveditha
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.
• 08-06-2008, 11:56 AM
Eranga
Something like this,

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         }     }```
• 08-06-2008, 01:41 PM
mtv134
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 candidate
• 08-11-2008, 03:42 AM
Eranga
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.
• 10-31-2010, 02:17 PM
amro
use bubble sort and link the vote to its corresponding candidate like this

Quote:

for(int i=1; i<vote.length; i++)
for(int j=0; j<vote.length-1; 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;
}

• 10-31-2010, 02:22 PM
amro
the largest vote will be the last vote and its corresponding canditate will have the same index in the second array
regards
• 10-31-2010, 05:42 PM
Norm
@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
• 10-31-2010, 06:02 PM
amro
@Norm
Quote:

use code tags to preserve the formatting.
done, but you knew what ,there will be time where this codes will merge with Java code
Quote:

Use {}s
yes some time extra of them is necessary but also misleading in case one of them is missing in another part especially 30 minutes before going to bed
• 10-31-2010, 06:10 PM
amro
i have one question
what if the array is not full
is there any thing like getArrayMax ???
• 11-02-2010, 06:45 AM
Eranga
Quote:

Originally Posted by amro
use bubble sort and link the vote to its corresponding candidate like this

This is a very old thread, almost two years pass. No point of interacting in such, unless OP has interest on it.
• 11-02-2010, 06:47 AM
Eranga
If you've a question please start a new thread in the correct sub-forum.
• 11-02-2010, 06:38 PM
amro
Quote:

Originally Posted by Eranga
This is a very old thread, almost two years pass. No point of interacting in such, unless OP has interest on it.

its just incomplete threads are very annoying fore those coming from search engines and wast of bandwidth as well

i use to open new post when its really acute
• 11-03-2010, 07:49 AM
Eranga
But the OP has no interest at all.