Solved

Find largest and smallest number without array

Posted on 2009-07-06
8
1,110 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
  • 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
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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can anyone please tell me why i can not find ID  investorCoolGrid inside the jsp page? 1 23
throw exception 21 67
jsp login check 12 53
Java 8 to Java 6 8 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 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