Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array maximum problem (Java)

Started by M-E Mar 30, 2007 at 8:23 AM 9 replies 4.1k views
Original Post
M-E
M-E
I have a problem with one of the exercises of my Java tutorial, i hope someone can give me a good hint and help me with this one. I would appreciate it. The exercise is this:
Quote:
Exercise 2 --- Two Largest Elements Complete the following program so that it computes and writes out the two largest elements in the array. import java.io.* ; class TwoLargest { public static void main ( String[] args ) throws IOException { int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2}; // declare and initialize variables for the two largest // compute the two largest for ( int index= ; index < data.length; index++) { } // write out the two largest System.out.println( ); } }
Now i know how to compute the maximum of an array, like this:
import java.io.* ;

class Largest
{

  public static void main ( String[] args ) throws IOException
  {
    int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};
    
    // declare and initialize variable for the largest
    int largest = data[0];
    
    
    // compute the largest
    for ( int index= 0; index < data.length; index++)
    {
      if(largest < data[index]) {
         largest = data[index];
      }
    }
      
    // write out the largest
    System.out.println("The largest is: " + largest);

  }
}  



However, I don't know how i can compute the second largest element (in the same for-loop as the largest element) like in the exercise. I hope someone can help me.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
M-E
M-E
Quote:
Original post by OrangyTang
Keep track of the second largest found as before, and update it if it's greater than before but less that the largest?

Personally I'd just sort the array and take the last two element, but thats just me. [grin]


Ok, but thats not the exercise [grin]

And about the first part: That's what i thought about too, but that won't work if the two largest elements are the same.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
M-E
M-E
I know how to do it, but it's not (i think it's not) what the exercise wants:

class TwoLargest{  public static void main ( String[] args ) throws IOException  {    int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};        // declare and initialize variable for the largest    int largest = data[0];    int secLargest = 0;    int skip = 0;        // compute the largest    for ( int index= 0; index < data.length; index++)    {      if(largest < data[index]) {         largest = data[index];         skip = index;      }    }        for ( int index= 0; index < data.length; index++)    {      if(secLargest < data[index] && index != skip) {         secLargest = data[index];      }    }          // write out the largest    System.out.println("The largest is: " + largest + " and the second largest is: " + secLargest);  }}  


But this program has a bug too, because when all elements are negative the second largest will be zero. If you initialize secLargest like largest you will get a problem if the first element of the array is the largest.
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
giveblood
giveblood
The second largest works the same way, only you don't even consider it unless its less than the largest.

import java.io.* ;class TwoLargest{  public static void main ( String[] args ) throws IOException  {    int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};    // declare and initialize variable for the largest    int largest = data[0];    int second = data[0];    // compute the largest    for ( int index= 0; index < data.length; index++)    {		if(data[index]>=largest)		{			largest=data[index];		}		else if (data[index]>second)		{			second=data[index];		}	}    // write out the largest    System.out.println("The largest is: " + largest);    System.out.println("The second is: " + second);  }}
-Chris
Stani R
Stani R
I'll just add that I find this exercise to be a waste of your time. Like OrangyTang said, you should be sorting the array, which is what this is aiming at in the end anyway. You could start by implementing selection sort and insertion sort, then move on to more complex algorithms such as quicksort or heapsort. You will get a lot more mileage out of those :D
giveblood
giveblood
I didn't think about if the first value was the largest. You could get around it by making second a very small number, but that's not a good idea. I can't think of a good way to do this without having to pass through the away twice, or adding extra statements that specifically check to see if so and so is the largest
-Chris
M-E
M-E
Yeah, that works for this exercise, thanks. But it still doesn't work for all versions of the problem, because if the second largest element already passed between data[0] and largest it will not compute the second largest.


anyway, thanks all. I will just skip this exercise then...
"Anyone who has never made a mistake has never tried anything new." - Albert Einstein
Stani R
Stani R
I didn't spend too much time on it, but I think the following loop should catch both. If there's a problem with it, it's that it won't detect the case where the first element is the largest. For that, you need to do something else instead of initializing both to the first element right away. That's why you should just sort the array - you won't have to deal with special cases, and can just take the last two elements to answer the problem.

    for ( int index= 0; index < data.length; index++)    {    	if( data[index] > largest) {    		second = largest;    		largest = data[index];    	}    	else if (data[index] > second)    		second = data[index];    		}    }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.