Results 1 to 15 of 15
- 08-05-2008, 06:54 PM #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:
Here is the input file: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); } }
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 Sleepy 40 Sneezy 60 Bashful 25 Doc 45
So I need some assistance so that the code figures out that Sneezy recieved the most votes and will print it out like so: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.
- 08-05-2008, 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.
- 08-06-2008, 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
- 08-06-2008, 11:56 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 } }
- 08-06-2008, 01:41 PM #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 candidatevisit http://www.thejavacode.com
Muhammad Safwat
- 08-11-2008, 03:42 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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, 01:17 PM #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 04:47 PM.
- 10-31-2010, 01:22 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
- 10-31-2010, 04: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
- 10-31-2010, 05:02 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
@Norm
done, but you knew what ,there will be time where this codes will merge with Java codeuse code tags to preserve the formatting.
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 bedUse {}s
- 10-31-2010, 05:10 PM #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 ???
- 11-02-2010, 05:45 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-02-2010, 05:47 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you've a question please start a new thread in the correct sub-forum.
- 11-02-2010, 05:38 PM #14
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
- 11-03-2010, 06:49 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But the OP has no interest at all.
Similar Threads
-
Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11-08-2010, 08: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: 06-10-2008, 03:53 AM -
Finding largest and smallest integer
By mlhazan in forum New To JavaReplies: 2Last Post: 01-12-2008, 10:30 PM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM -
Finding largest no
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks