# Thread: Finding the largest number in an array

1. Member
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

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]);
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++)

System.out.println();
System.out.println("The total number of votes: " + total);

}

}```
Here is the input file:

Java 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):

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```
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.

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.

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.

4. 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
}
}```

5. Member
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 candidate

6. 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.

7. Member
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.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;
}
Last edited by amro; 10-31-2010 at 05:47 PM.

8. Member
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

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

10. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
@Norm
use code tags to preserve the formatting.
done, but you knew what ,there will be time where this codes will merge with Java code
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

11. Member
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 ???

12. 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.

13. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
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

#### Posting Permissions

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