Assignment :: TwoLargestElements
Complete the following program so that it computes and writes out the two largest elements in the array.

So far I have this...and it just gets the largest number for both outputs..I need to change it so I get both the largest and second largest. Any help would greatly be appreciated, thanks!

import java.io.* ;
class TwoLargest {
public static void main ( String[] args )
throws IOException {
int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};
int largest = 0, largest2 = 0;
for ( int index = 0; index < data.length; index++)
if(largest < data[index]) {
largest = data [index];
}
for ( int index = 0; index < data.length; index++)
if (largest2 < data[index]) {
largest2 = data [index];
}
System.out.println("The largest number in the array is: " + largest);
System.out.println("The second largest number in the array is: " + largest2);
}
}

Do you have an algorithm that describes your logic in finding the two largest elements?
A list of the steps you think will solve the problem.

Haha, nope not really, I know how to find the largest which works fine (first for loop), but for the second for loop which finds the second largest, it doesn't work. Instead, the second loop also gets the largest. I understand I can use the sort method in order to solve my problem, but there's another approach to this, which I'm missing.

I've also tried changing the second if statement to:

if (largest2 < data[index] && largest2 != largest) {

Maybe you should check if the next largest number being checked in the next loop is less than the current largest number

Tried that, but I think it's an issue of the integer largest2 being declared as 0 in the beginning. So basically, 0 is not equal to 12, and therefore it just prints out 12 for largest2.

Tried that, but I think it's an issue of the integer largest2 being declared as 0 in the beginning. So basically, 0 is not equal to 12, and therefore it just prints out 12 for largest2.

a bit late, but two other possible approaches you could have taken:
after you found the highest number -> remove that number from the array and use the exact same algorithm (you would have to remove all occurences of the highest number)
or (easiest)
you could have sorted your array and taken the last two (unique) values in the array.

if i would do this manually, i would count in acsending order till i reach the second last number. that would be the second largest.

not necessarily (this is only guaranteed to be so if there are only unique values in the array), and this would only work in case the array was already ordered, which we may assume it's not, otherwise it would be a bogus

Why did you initiate largest = 0 and largest2 = 0?
What will happen if every elements of your array is less than 0?
I solved this exercise by doing this:
1) let largest and largest2 = data[0]
2) use for loop, let i = 0 to (data.length-1), if data is greater than largest then largest2 will hold the old value of largest, and assign largest = data
It worked!

uhm, i did not think about this case!
We can check if the first one is the maximum one or not,
If true, just find the maximum one of the array (with i = 1 to data.length)
If false, do what i wrote!
Er, NormR1, how can we solve this (without sorting that array)?
---
songokute