Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Find the two largest number

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Find the two largest number

    this exercise was from the book "Java how to program 9th edition".

    you ask the user to input 10 numbers and your program should find the largest number and the second largest number out of the 10. the logic in my code makes sense to me but it does not come up with the two largest numbers.

     
    import java.util.Scanner;
    public class Largest {
    	public static void main(String [] args){
    		Scanner input = new Scanner(System.in);
     
    		int counter = 0;
    		int currentNum;
    		int largestNum = 0;
    		int secLargestNum = 0;
     
    		while (counter < 10){
    			currentNum = input.nextInt();
    			if (currentNum > secLargestNum && currentNum > largestNum){
    				largestNum = currentNum;
     
    				if (currentNum > secLargestNum && currentNum < largestNum){
    					secLargestNum = currentNum;
    				}
    			}
    			counter++;
    			System.out.printf("%d\n", largestNum);
    			System.out.printf("%d\n", secLargestNum);
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    Can you explain your logic?
    Especially how you determine the second largest.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    well for the first if statement "if (currentNum > secLargestNum && currentNum > largestNum)", i'm saying that if the user input a number that is greater than the current second largest number and its greater than the current largest number then it will be the new largest number.

    if (currentNum > secLargestNum && currentNum < largestNum)
    is saying that if the user inputs a number that is greater than the current second largest number but its smaller than the current largest number then it will be the new second largest number.

    i also spotted an error in my nesting of the the second if statement in the first one. that was unintentionally.


     
    import java.util.Scanner;
    public class Largest {
    	public static void main(String [] args){
    		Scanner input = new Scanner(System.in);
     
    		int counter = 0;
    		int currentNum;
    		int largestNum = 0;
    		int secLargestNum = 0;
     
    		while (counter < 10){
    			currentNum = input.nextInt();
    			if (currentNum > secLargestNum && currentNum > largestNum){
    				largestNum = currentNum;
    			}
    			if (currentNum > secLargestNum && currentNum < largestNum){
    				secLargestNum = currentNum;
    			}
    			counter++;
    			System.out.printf("%d\n", largestNum);
    			System.out.printf("%d\n", secLargestNum);
    		}
     
    	}
     
    }
    Last edited by vendettabf; December 21st, 2011 at 07:48 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    if the user input a number that is greater than the current second largest number and its greater than the current largest number then it will be the new largest number.
    Why test against both?

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    i dont know... it does seem necessary now that you mention it. the problem still persists tho xD.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    i dont know.
    That makes it very hard to write a program if you do not know what steps the program is to take to solve the problem.

    I often use a piece of paper and pencil to work out program logic BEFORE I try writing the code.

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    well after reading your comment, though i did not actually took and pen and paper out, i studied my code and noticed that my approach was flawed. when a number is given that is larger than the largest number, the largest number would be replaced with the number, but then the second largest isnt no longer valid. the second largest number must obtain the original largest number if the largest number is replaced. after noticing this, i redid my code:

     
    import java.util.Scanner;
    public class Largest {
    	public static void main(String [] args){
    		Scanner input = new Scanner(System.in);
     
    		int counter = 0;
    		int currentNum;
    		int largestNum = 0;
    		int secLargestNum = 0;
     
    		while (counter < 10){
    			currentNum = input.nextInt();
    			if (currentNum > largestNum){
    				secLargestNum = largestNum;
    				largestNum = currentNum;
    			}
    			if (currentNum < largestNum && currentNum > secLargestNum){
    				secLargestNum = currentNum;
    			}
     
    			counter++;
    			System.out.printf("%d\n", largestNum);
    			System.out.printf("%d\n", secLargestNum);
    		}
     
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    if (currentNum < largestNum
    that looks like it should be an else

    What if two largest?

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    well if it was just that alone it would be an else, but since there is a second argument i didn't make it an else. if there is a better way to write that syntax, please do tell.

    i dont know what you mean by "what if two largest?"

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    An else would be a tad more efficient in that if the first if is true, the second one can not be true.

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Find the two largest number

    Would this work?

     
    int[] numbers = new int[10];
    // That was an array.  It stores a bunch of a type of data (It has to be all of the same data type.  Can't store both ints and doubles (unless you make the data type Object that is))
    // It starts at index 0 and goes to the length, in this case 10, -1, so from index 0 to 9.  It has a variable called length that keeps track of its size.  
    // It seemed easier to do this with arrays than to possibly have ten different int variables (one for each index).
    // You generally put the [] after the datatype, though I think you can have it after the variable name, though I think the downside of doing it the second way would be, I think if I heard right, 
    // that if you had two int arrays or something and had it like int array[], array2;  It would make the first an array and the second a regular int whereas, I believe if I've heard right,
    // that int[] array, array2, would make both array and array2 arrays.   
    // the 10 in the box tells it the size.  
    // Also, I believe you have to import java.util in order to use arrays I think.
    // Also, an array has a fixed size, you can't add or remove indexes from it (i.e. you can't suddenly try to make that array be size 11 or size 9.).
     
     
    for (int i =0; i < numbers.length; i++)
    {
    numbers[i] = input.nextInt();
    }
     
     
    int current;
    int largest;
     
     
    current = numbers[0];
    largest = current;
     
    for (int i = 1; i < numbers.length; i++)
    {
    current = numbers[i];
    if (current > largest) 
    largest = current;
     
    }
     
    System.out.println("The largest number is: " + largest);
     
    int secondLargest;
    int newCurrent;
     
    if(numbers[0] == largest)
    {
    current = numbers[1];
    secondLargest = current;
     
    for (int i = 2; i < numbers.length; i++)
    {
    current = numbers[i];
    if (current > secondLargest)
    secondLargest = current;
    }
     
    }
     
    else
    {
    current = numbers[0];
    secondLargest = current;
     
    for (int i = 1; i < numbers.length; i++)
    {
    current = numbers[i];
    if (current > secondLargest && current < largest)
    secondLargest = current;
    }
     
    }
    System.out.println("Second Largest number is: " + secondLargest);
    Last edited by javapenguin; December 21st, 2011 at 10:56 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Find the two largest number

    Looks like a very inefficient way to do it with three loops.

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Find the two largest number

    Quote Originally Posted by Norm View Post
    Looks like a very inefficient way to do it with three loops.
    As well as a great example of spoonfeeding.
    http://www.javaprogrammingforums.com...n-feeding.html
    for which javapenguin, I am quite positive you've been warned about in the past (and seriously - "Also, I believe you have to import java.util in order to use arrays I think." - did you try it? Time and again, you post pseudo advice without truly knowing the answer, and the answer is at your fingertips)
    Last edited by copeg; December 22nd, 2011 at 10:49 AM.

  14. #14
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    what i did was as follows:

    if statement to test if current number is greater than largest number, and if it is current largest number is stored as previous largest number and current number becomes largest number

    nested if statement that tests whether previous largest number is greater than current second largest number and if it is previous largest number becomes second largest number

    else if statement that belongs to original if statement structure that asks if current number is larger than second largest number and if is then current number becomes second largest number

    so basically i'm just getting round problem by creating a new variable so that the diplaced largest number can become the current second largest number

    i'm new to the forum and hope my suggestions don't fall foul of how we are supposed to go about things

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find the two largest number

    I think i got that wrong - as far as i can tell all you need is to deal with the matter of making the previous largest number the second largest number

    i seem to have sorted it out with just an if/else if structure

  16. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Find the two largest number

    Yes but what if the new number it encounters is larger than second largest but smaller than largest? Merely setting the second largest to the previous largest every time and not dealing with the case of a new second largest but not a new largest could result in errors sometimes.

    i.e.

    5 3 4 ........

    Also, shouldn't you be initializing both largest and secondLargest to the lowest possible int value? (i.e. if you initialize them originally to 0 and the user only enters in negatives, then your largest, though never entered by the user, will be 0. Also, since 0 would be the original largest as well as second largest, then the secondLargest value would also be 0 as largest would never find a larger value than 0, so secondLargest would never be set to the value of the previous largest and also none of the negative values entered by the user would be larger than 0.)

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. find the sum of even number not exceed four million
    By i4ba1 in forum Algorithms & Recursion
    Replies: 10
    Last Post: June 29th, 2011, 08:08 AM
  3. [SOLVED] My smallest and largest integers will not change.
    By toiletsauce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2011, 07:50 PM
  4. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM
  5. finding the largest object help
    By nickypass in forum Object Oriented Programming
    Replies: 4
    Last Post: October 16th, 2010, 05:48 PM