Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Find the Second Largest and Smallest  RSS feed

 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, everyone! I'm trying to run code that will find the smallest and largest integer. I've completed that, but I also need to find the second largest/smallest. I can't use arrays, because it has not been covered yet. I feel like such an idiot though, because I've been trying to figure it out for numerous hours. Basically, I just know control statements and methods. Anyway, here is what I have so far:


Thanks.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Chase.

First off, step away from your keyboard (well, after you finish reading this message, anyway). Then, get yourself a pencil and paper, and some dice. On that paper, draw two squares, one labeled, "Greatest," the other labeled, "Second Greatest." In each, write zero. Now, start rolling your dice and track the greatest and second greatest values you see. Pick some fixed, small number of rolls, like five. As you do this, ask yourself, "What am I doing after each roll to determine if I need to change what's in one square, the other square, or both?" Pretty quickly, you'll realize what your mental logic is. Once you do that, write it down as a set of instructions in English (or some other human language of your choice). From there, see if you can use those written instructions as comments in a Java program you derive from them.

In general, when you are trying to write a program to do a task, writing the code should come last, after you have figured out how to solve the problem. Give my approach a try. It may sound silly, but I bet you'll discover it works.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Welcome to the Ranch, Chase.

First off, step away from your keyboard (well, after you finish reading this message, anyway). Then, get yourself a pencil and paper, and some dice. On that paper, draw two squares, one labeled, "Greatest," the other labeled, "Second Greatest." In each, write zero. Now, start rolling your dice and track the greatest and second greatest values you see. Pick some fixed, small number of rolls, like five. As you do this, ask yourself, "What am I doing after each roll to determine if I need to change what's in one square, the other square, or both?" Pretty quickly, you'll realize what your mental logic is. Once you do that, write it down as a set of instructions in English (or some other human language of your choice). From there, see if you can use those written instructions as comments in a Java program you derive from them.

In general, when you are trying to write a program to do a task, writing the code should come last, after you have figured out how to solve the problem. Give my approach a try. It may sound silly, but I bet you'll discover it works.
Thank you! That really does help. Here's where I am now. Am I on the right track?
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to work...Kinda. Here's output from one of the attempts.
Enter a Value (or 0 to End): 5898999
Enter a Value (or 0 to End): 909
Enter a Value (or 0 to End): -9999
Enter a Value (or 0 to End): -44464565
Enter a Value (or 0 to End): 1
Enter a Value (or 0 to End): 7800
Enter a Value (or 0 to End): 0
The smallest number is: -44464565
The largest number is: 5898999
The second smallest number is: 1
The second largest number is: 7800

It skipped right over the -9999 and picked "1" as the second smallest.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this

Enter a Value (or 0 to End): 10
Enter a Value (or 0 to End): 20
Enter a Value (or 0 to End): 30
Enter a Value (or 0 to End): 0
The smallest number is: 10
The largest number is: 30
The second smallest number is: 20
The second largest number is: -2147483648
 
Chase Harris
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I got this

Enter a Value (or 0 to End): 10
Enter a Value (or 0 to End): 20
Enter a Value (or 0 to End): 30
Enter a Value (or 0 to End): 0
The smallest number is: 10
The largest number is: 30
The second smallest number is: 20
The second largest number is: -2147483648
I've made some more changes. Can you please try it again?

 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good!
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Looks good!
Yay! Thanks for looking!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!