Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By AnandVijay
  • 1 Post By Norm
  • 1 Post By kneitzel

Thread: Print the Largest Number in the Array

  1. #1
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default 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,

    Konrad

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: Print the Largest Number in the Array

    Thanks Gimbal2,

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

  6. #6
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default 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[0]);
    System.out.println("2nd Largest Number : "+arrValues[1]);

    }
    }
    sidda likes this.

  7. #7
    AnandVijay is offline Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0

    Default Re: Print the Largest Number in the Array

    Try this one..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Print the Largest Number in the Array

    @AnandVijay See this: The Problem with Spoon-feeding
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default 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.

  10. #10
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default 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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.
    sidda likes this.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default 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,

    Konrad
    sidda likes this.

  13. #13
    sidda is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

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

Similar Threads

  1. Finding Second largest number
    By Gsangha in forum New To Java
    Replies: 5
    Last Post: 12-29-2011, 04:49 PM
  2. Replies: 26
    Last Post: 11-14-2011, 03:22 PM
  3. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 06:08 PM
  4. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  5. Print the second Largest in an array
    By singularity in forum New To Java
    Replies: 23
    Last Post: 05-05-2010, 12:33 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
  •