Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2008
    Posts
    8
    Rep Power
    0

    Default 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);
    		
    	}
    
    }
    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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default

    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. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #5
    mtv134 is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    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
    visit http://www.thejavacode.com
    Muhammad Safwat

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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. #7
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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.

  8. #8
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    the largest vote will be the last vote and its corresponding canditate will have the same index in the second array
    regards

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default

    @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. #10
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    @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. #11
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    i have one question
    what if the array is not full
    is there any thing like getArrayMax ???

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by amro View Post
    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. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  14. #14
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM
  3. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  4. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

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