954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Max and Min of more than 2 numbers

I am trying to make a program that finds the max and min of more than 2 inputs and i am trying to use the Math.Min and Math.Max method but i cant seem to get them too work since they only accept 2 parameters.

this is what i tried to use

public static int max(int a, int b, int c);
int max = Math.max(a) Math.max(b) Math.max(c);


can anyone help me try to find the max and min of more than 2 values please. thanks.

aodpreacher
Light Poster
26 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

You can write a method of your own that, say for example when passed an array of numbers returns the max and the same for min.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

I just started programming in Java and i dont think i would know how to program a method to make it detect the min/max in an array. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input?

aodpreacher
Light Poster
26 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Yes, Math.max and Math.min in a loop saving the result through each iteration. Once the loop is done, you have the max and/or min.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
I just started programming in Java and i dont think i would know how to program a method to make it detect the min/max in an array. is there any method to just use the Math.min/Math.max for trying to see which one is the largest/smallest from the users input?

Yes you may new to java, but are you new to numbers and Math too ?
I am not against using Math.min/max but that's a lame excuse. Can you not compare two given numbers for equality/greater than/lesser than condition are you new to that too ?

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Besides, if you simply want to "take the easy way out", place your numbers in an array (which I assume they are already, anyway), and use Arrays.sort() and then pull the first and last numbers.

;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
Yes, Math.max and Math.min in a loop saving the result through each iteration. Once the loop is done, you have the max and/or min.

How would i make it in a loop?

aodpreacher
Light Poster
26 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
for (.....) {
  max = Math.max(max, ...);
  min ....
}
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

thank you so much. :)

aodpreacher
Light Poster
26 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
How would i make it in a loop?

@masijade : He he, I knew that would be his next question.

@aodpreacher : Well if you feel your question has been answered, you can mark this thread solved and give masijade his deserved and me (somehwhat) undeserved credit ;)

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

try this one

import java.util.Arrays;

class max
{
public static void main(String args[])
{
int [] num = {40, 20, 10 ,5, 74, 15, 20,30,95,21};
Arrays.sort(num);
System.out.println("min value"+num[0]);
System.out.println("max value"+num[9]);
}
}

lalchand87
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Read first number in the array. Set this to be both the highest and lowest initially so we have something to compare against

Then check each subsequent number in the array
Is it higher than the highest number so fat?
- If so then its the new highest number
Is it lower than the lowest number so far?
- If so then its the new lowest number
Loop until you reach the end of the array

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

try this one

import java.util.Arrays;

class max { public static void main(String args[]) { int [] num = {40, 20, 10 ,5, 74, 15, 20,30,95,21}; Arrays.sort(num); System.out.println("min value"+num[0]); System.out.println("max value"+num[9]); } }

See reply 6.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

wouldn't a simple line like

return Math.max( x, Math.max( y, z ) );

work eaiser than an array since math rules state that you have to work from the inside out . Just a question, I to am new to this and am on here looking for ideas and answers for my own homework...

tmoney7566
Newbie Poster
17 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

No, that wouldn't work. How would it work for a set of numbers of arbitrary size (in other words how would it work when you don't know how many numbers you'll have, since in your example, it only works if you have 3 numbers)? Hard coding it like that would only work if you knew how many numbers you would have. And even then, it is unreadable and overly complex. The suggestions that a few people in here gave will work, but consider that the most efficient solution is probably to keep two variables: one being the lowestValue and one being the highestValue. Then, "look at" each number in your set of numbers once (go through your numbers once) and if it's greater than the highestValue set highestValue to the number you're looking at. And do the same for the lowestValue. If you study Java at all, you will be able to write this method. Nobody is going to hand you the code. But if this is hard to understand I'll give you the psuedocode so you can try to code it on your own:

declare highestVariable and set it to 0
declare lowestVariable and set it to something really high

for each of your numbers, compare the first number to highestVariable. If it's higher than highestVariable, set highestVariable to the current number. Do the equivalent for lowestVariable. Then move onto the second number. Do this for every number in your set of numbers.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
declare highestVariable and set it to 0 declare lowestVariable and set it to something really high

@BestJewSinceJC : This is not a good idea. Say for example in the first case all the nnumbers in the array are negative. what then ? Your approach would certainly yield the wrong answer. Also in the second case, would you be able to define a really high value ? Also what if all the numbers are greater than that ?

@OP : For this one should set the min and max to the very first numbers from the array and then compare with all the other numbers, modifying the min wherever a lower number is found and modifying the max wherever a greater number is found. In the end min and max have the answers to your question.

PS : I am surprised this thread should be lasting more than 4-5 posts. I think we all have given the OP sufficient material for thought (not to mention masijade gave him the entire solution) and further posts should be submitted after the OP provides proof of effort from his side.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Read first number in the array. Set this to be both the highest and lowest initially so we have something to compare against

Then check each subsequent number in the array Is it higher than the highest number so fat? - If so then its the new highest number Is it lower than the lowest number so far? - If so then its the new lowest number Loop until you reach the end of the array

my programming PhD always taught me to make my own methods,
you can fit them to what you want, and that's really a good practice for programming.

try to make that algorithm, min/max is one of the easiest when you begin to work it out.

eng.M4AH
Newbie Poster
8 posts since Dec 2008
Reputation Points: 17
Solved Threads: 1
 
not to mention masijade gave him the entire solution

Well, theform of it anyway. Cut-n-paste code I do not do. ;-)

In either case, I have the feeling that his instructor will want to see a loop, two "save" variables, and two if statements, rather than Math.max, Math.min, or the easiest solution Arrays.sort(). ;-)

Of course, he didn't really state how the instructor worded the exercise, the instructor may even want to see a recursive method. Who knows? ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

i think you must try this
method for find max and the same method for min
private int findMax(int a, int b, int c)
{int max=math.max(Math.max(a,b),c)
return max;
}

doleh
Newbie Poster
9 posts since Jul 2008
Reputation Points: 8
Solved Threads: 1
 
i think you must try this method for find max and the same method for min private int findMax(int a, int b, int c) {int max=math.max(Math.max(a,b),c) return max; }

Have you even taken care to understand what his problem domain specifies ? He clearly mentions that there can bemore than 2 nos so it means there can be more than 3 nos too. How does your program work when there are more than 3 numbers ? Will you be able to explain that ? This approach has already been suggested and somebody has already mentioned that this hardcoded approach won't work.
While suggesting solutions remember to read the problem domain correctly, understand it and also don't forget to read the solutions offered by others.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You