# Thread: Print the Largest Number in the Array

1. Member Join Date
Apr 2014
Posts
9
Rep Power
0

## Print the Largest Number in the Array

Guys im a java newbie, Im not able to get the intended result using below code. Could you assist me to rectify the mistake.

Thought process : Sort the array and print the last 2 element of any given array.
Note /|\ Should not use any inbuilt Array.sort()

Java Code:
``` //Write a code to print the 2 largest numbers  from the given Array {2,8,10,5,9}

package arrays;
public class biggest2numbers {
public static void main(String[] args) {

int[] A = {2,8,10,5,9};
//Declaring 2 variable
int Max1,
Max2;

for (int i=0; i>=A.length-1; i++)
{
//initializing 2 variable to hold 1st Highest number in MAX1 and Second Highest Number in Max2
Max1= A[i];
Max2= A[i+1];

//Swap the value of Array index "if" condition is true

if (Max1> Max2)
{
A[i] = Max2;
A[i+1] = Max1;

}
else {
A[i]=Max1;
A[i+1]=Max2;
}

}
//  Array should be sorted in the ascending order and printing the last  2 array element should return the 1st Highest and 2nd Highest elements from the array

System.out.println(" largest Number "+ A[A.length-1]);
System.out.println("Second largest Number "+ A[A.length-2]);
}

}```

Actual Output :

largest Number 9
Second largest Number 5

Problem im facing , code not entering the for loop :(
Last edited by sidda; 04-01-2014 at 03:36 PM.  Reply With Quote

2. Senior Member Join Date
Feb 2014
Posts
447
Rep Power
7

## Re: Print the Largest Number in the Array

Hi,

just a few points I want to mention:
a) If you have a problem then describe it. Just posting some code without giving the error or problem is a bad habbit!
b) for loop is always for (<initialisation>; <condition>; <step>)
So if you want to go through the array, then you want to start with 0 (ok) but then the condition should check if i is inside the array! And that is the case when I is smaller than the number of elements!
c) Use meaningful names. Just A, num1, num2 ... that simply does not say anything about the content of the variables!
d) Your want to get the max number of an array and then you modify the array? Sorry, but really get back to my point a and clearly state, what you want to do.

And a last hint: Play on a piece of paper what should happen. Write down a lit of numbers and then do exactly what you want to do. If I need the highest number of a list of numbers, then I take the first number of all numbers as max value. Then I compare the max value with each other number. If a number is higher than my current max, then I replace max with it. Quite straight forward. So first make sure, that you understood what should go on. Then you can write it down in any language e.g. in Java.

With kind regards,  Reply With Quote

3. ## Re: Print the Largest Number in the Array

get the intended result
Please copy and paste here the program's current output so we can see what it does.

[code]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

4. Just a guy Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: Print the Largest Number in the Array

The code does not seem to match the requirement in that comment anyway. I see a requirement to FIND the highest 2 numbers, yet I see code which apparently tries to sort the array.

Looks a lot like copy/paste/hack to make it work and not so much like code written because of a thought process.  Reply With Quote

5. Member Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Print the Largest Number in the Array

Thanks Gimbal2,

Appreciate your comment, Any suggestion on how to correct the thought Process ??  Reply With Quote

6. Member Join Date
Oct 2013
Posts
15
Rep Power
0

## Re: Print the Largest Number in the Array

public class Sort {
public static void main(String[] args) {
int arrValues[]={2,8,10,5,9};
for (int i = 0; i < arrValues.length; i++)
{
for(int j=i+1;j<arrValues.length;j++)
{
if(arrValues[i]<arrValues[j])
{
int temp=arrValues[i];
arrValues[i]=arrValues[j];
arrValues[j]=temp;
}
}
}
System.out.println("1st Largest Number : "+arrValues);
System.out.println("2nd Largest Number : "+arrValues);

}
}  Reply With Quote

7. Member Join Date
Oct 2013
Posts
15
Rep Power
0

## Re: Print the Largest Number in the Array

Try this one..  Reply With Quote

8. ## Re: Print the Largest Number in the Array

@AnandVijay See this: The Problem with Spoon-feeding  Reply With Quote

9. Member Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Print the Largest Number in the Array

This code fails when subjected to array of negative integers, Array1 = {-8,3,5,2,9,1,-1}

Java Code:
```public static void main(String[] args) {

// intialize array of integer with 5 elements

int [] Array1 = {-8,3,5,2,9,1,-1};

// Thought process : take the first element and compare with the entire  array list
// if its greater than the successive element then swap
// else just increment the counter

for (int i = 0 ; i < Array1.length-1; i ++){

for (int j=0; j<Array1.length-1;j++){

if(Array1[i] < Array1[j]) {
int temp = Array1[j];
Array1[j]=Array1[i];
Array1[i] = temp;

}
}
}

for (int k : Array1){
System.out.print(k +",");
}

System.out.println(" largest Number "+ Array1[Array1.length-1]);
System.out.println("Second largest Number "+ Array1[Array1.length-2]);

}

}```
Actual Output

-8,1,2,3,5,9,-1,
largest Number -1
Second largest Number 9
Last edited by sidda; 04-14-2014 at 07:47 PM.  Reply With Quote

10. Member Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Print the Largest Number in the Array

Could any one suggest whats goes wrong when array consists of negative values?

Note : When we use below snippet, output is displayed correctly however values are printed in Descending order

Java Code:
```	if(Array1[i] > Array1[j]) {
int temp = Array1[j];
Array1[j]=Array1[i];
Array1[i] = temp;

}```
Actual Output :
9,5,3,2,1,-8,-1,
largest Number -1
Second largest Number -8  Reply With Quote

11. ## Re: Print the Largest Number in the Array

The output makes it look like the sort doesn't look at the last element in the array and leaves it where it was.  Reply With Quote

12. Senior Member Join Date
Feb 2014
Posts
447
Rep Power
7

## Re: Print the Largest Number in the Array

Your sort algorithm seems looks wired. You are moving items around like mad, because both loops overlap so sometimes i < j and sometimes j < i.
So sometimes you move the small item to the lower end and sometimes to the higher end.

So I would suggest that you are thinking about the algorithm that you want to implement and then implement the algorithm.

(And Norm is also correct - it seems that you never handle the last item. So maybe you want to change the loops to <= or remove the - 1 so you include the last item, too. Or you just drop the check of the items i and j and compare the items i and i+1. But only do ONE of these three suggestions!)

With kind regards,  Reply With Quote

13. Member Join Date
Apr 2014
Posts
9
Rep Power
0

## Re: Print the Largest Number in the Array

Thank you Norm,

When i changed the array.len condition, its working fine.

Java Code:
```for (int i = 0 ; i < Array1.length; i ++){

for (int j=0; j<Array1.length;j++){

if(Array1[i] < Array1[j]) {
int temp = Array1[j];
Array1[j]=Array1[i];
Array1[i] = temp;

}
}
}```  Reply With Quote

#### Posting Permissions

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