# Find the two largest number

• December 21st, 2011, 05:47 PM
vendettabf
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. :mad:

Code Java:

```  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); }   }   }```
• December 21st, 2011, 06:34 PM
Norm
Re: Find the two largest number
Especially how you determine the second largest.
• December 21st, 2011, 06:46 PM
vendettabf
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.

Code Java:

```  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); }   }   }```
• December 21st, 2011, 06:49 PM
Norm
Re: Find the two largest number
Quote:

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?
• December 21st, 2011, 06:57 PM
vendettabf
Re: Find the two largest number
i dont know... it does seem necessary now that you mention it. the problem still persists tho xD.
• December 21st, 2011, 07:03 PM
Norm
Re: Find the two largest number
Quote:

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.
• December 21st, 2011, 07:37 PM
vendettabf
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:

Code Java:

```  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); }   }   }```
• December 21st, 2011, 07:40 PM
Norm
Re: Find the two largest number
Quote:

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

What if two largest?
• December 21st, 2011, 07:47 PM
vendettabf
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?"
• December 21st, 2011, 08:00 PM
Norm
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.
• December 21st, 2011, 09:44 PM
javapenguin
Re: Find the two largest number
Would this work?

Code java:

```  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);```
• December 22nd, 2011, 05:38 AM
Norm
Re: Find the two largest number
Looks like a very inefficient way to do it with three loops.
• December 22nd, 2011, 09:46 AM
copeg
Re: Find the two largest number
Quote:

Originally Posted by Norm
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)
• December 22nd, 2011, 01:55 PM
johnpipes
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
• December 22nd, 2011, 05:27 PM
johnpipes
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
• December 29th, 2011, 12:23 PM
javapenguin
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.)