Solved

Find largest and smallest number without array

Posted on 2009-07-06
8
1,103 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
MySqlDump not dumping triggers 1 43
Cisco ASA: Java web start no go, asdm launcher no go 3 37
running on tomcat not jboss eap 7.0 3 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

861 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