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); 
 }
}

Recommended Answers

All 30 Replies

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.

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) {

but still had no luck...

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

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.

I don't think your 2nd loop works like that

I don't think your 2nd loop works like that

Not sure on how to solve this, haha, do you have any clue?

How would you do it if you did it manually?
What would be your logic for finding the second largest value?
You have never explained your logic.

How would you do it if you did it manually?
What would be your logic for finding the second largest value?
You have never explained your logic.

I would look at the array and see that 8 is the second largest.........lol?

If 12 is the largest element in the array, then the second largest has to be 8...

&& largest2 != largest

The problem here is that you use the last value of largest2 instead of its next value

The problem here is that you use the last value of largest2 instead of its next value

Tried making another if statement, but same output...

Tried making another if statement, but same output...

Of course it will still do that cause

The problem here is that you use the last value of largest2 instead of its next value

same can be applied on another if statement

Of course it will still do that cause

same can be applied on another if statement

I'm not sure how to make it not the last value....do you know how to fix it? I'm very confused right now.

got it!! just had it change this part:

if (largest2 < largest) {  
    largest2 = largest;
  }

Thanks for ev1 who tried to help and gave feedback!

good for you :)

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.

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

int largest = 0, largest2 = 0;

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!

----
Songokute

What if the first value is the largest one?

since he assigns both fields to data[0] first, it would work, not?

What if the first value is the largest one?

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

When would largest2 get set?

Sorry i do not understand what you mean!

My posting was in response to stultuske.

:)
i did ask u: how can we find the 2 first largest one without sorting?
It's not difficulty to solve this issue but i don't know the best algorithm!

I don't know the best algorithm either.

How will you do if you have to solve this?

I'm lucky there. I don't have to solve it.

:)
You are humorous! Thanks for showing me many things that i don't realize by myself!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.