Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find largest and smallest number without array

Posted on 2009-07-06
8
Medium Priority
?
1,124 Views
Last Modified: 2012-05-07
I would like to implement a program like follows:

Prompt the user to enter a bunch of numbers till a sentinel is enter.
Only output the max and min once the sentinel is enter.

What sentinel should I use, if all numbers entered are +ve and -ve integer.

public class MinMax{

public void main(String args[]){

int counter=0;

while(true){
    int firstNum;
    int num = readInt("Enter number:");
    if(counter==0){ firstNum=num;}
    counter++;
    max=min=firstnum;
    if(num==SENTINEL){
     if(num<min) println("Min:"+min)  ;
     if(num<max) println("Max:"+min)  ;
     break;  
    }//end if
}//end while
   
}//end main

}//end class
0
Comment
Question by:peter_chong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24783501
You haven't defined min/max

int max = Integer.MIN_VALUE;
max = Math.max(max, num);
int max = Integer.MIN_VALUE; // (before loop)
...
max = Math.max(max, num);

Open in new window

0
 
LVL 20

Expert Comment

by:a_b
ID: 24783508
Can you ask the user how many number he wants to enter and then take as input that many numbers? Otherwise you might have to do some extra processing to get the numbers.
0
 

Author Comment

by:peter_chong
ID: 24783850
To CEHJ,
Actually, this is an exercise that I try to sharpen my programming skill without using much reusable library and preferably write by my own functions.

To a_b,
Can you ask the user how many number he wants to enter and then take as input that many numbers?
Can not in this case.
Since this is an exercise that I try to sharpen my programming skill without using much reusable library and preferably write by my own functions.

Sorry Guys. :-)


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24783860
>>
Actually, this is an exercise that I try to sharpen my programming skill without using much reusable library and preferably write by my own functions.
>>

Well there's very little difference actually in doing what i suggested and doing the below, but either way, you need to define the variable:
if (max < num) {
    max = num;
}

Open in new window

0
 

Author Comment

by:peter_chong
ID: 24784288
Hi CEHJ,
How about the integer sentinel, if I allow all integers to be enter?

Any other alternative? (refer my code above)

thanks.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 24784415
>> if(num==SENTINEL){

Will take care of that. Just make sure that line comes before any ordering is done
0
 

Author Comment

by:peter_chong
ID: 24786980
CEHJ:
Yes, you are right.

I honors 250 marks to you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24787080
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question