Solved

Find largest and smallest number without array

Posted on 2009-07-06
8
1,119 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 250 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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

688 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