# Need Help Creating Code to Find Min and Max Numbers

• (2 Pages)
• 1
• 2

## 16 Replies - 2168 Views - Last Post: 12 October 2012 - 06:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295378&amp;s=7ad95bcde16cc9931e21e9f35b670e03&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

# Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 02:19 PM

I need help creating a code using only the command console to input numbers. The code has to give the maximum and minimum value between 5 numbers. I did the code using 3 numbers, but now with 5 I'm a bit stuck. I have no clue how to use the Math.max and Math.min functions, and definitely need help with it. Afterwards I need to make essentially the same code except that it will sort the numbers from smallest to largest. I'll put in my code that I used for only 3 numbers, and you'll see why it will put extremely hard to use the same code with 5 numbers. If someone could show me how to do it using the Math.max and Math.min functions that would be great, otherwise I'm going to give myself a headache with endless lines of code.

public class MinMax
{
public static void main(String args[])
{

//Worked with Zach on this and SortNumbers

double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]);

if(x < 1 || x > 10)
{
System.out.println("Your first number is not between 1 and 10.");
}

else if(y < 1 || y > 10)
{
System.out.println("Your second number is not between 1 and 10.");
}

else if(z < 1 || z > 10)
{
System.out.println("Your third number is not between 1 and 10.");
}

else if(x >= y && y >= z)
{
System.out.println(x + " " + y + " " + z);
System.out.println(x + " is the largest number and " + z + " is the smallest number.");
}

else if(x >= y && z >= y)
{
System.out.println(x + " " + y + " " + z);
System.out.println(x + " is the largest number and " + y + " is the smallest number.");
}

else if(y >= x && x >= z)
{
System.out.println(x + " " + y + " " + z);
System.out.println(y + " is the largest number and " + z + " is the smallest number.");
}

else if(y >= z && z >= x)
{
System.out.println(x + " " + y + " " + z);
System.out.println(y + " is the largest number and " + x + " is the smallest number.");
}

else if(z >= y && x >= y)
{
System.out.println(x + " " + y + " " + z);
System.out.println(z + " is the largest number and " + y + " is the smallest number.");
}

else if(z >= x && y >= x)
{
System.out.println(x + " " + y + " " + z);
System.out.println(z + " is the largest number and " + x + " is the smallest number.");
}
}
}

Is This A Good Question/Topic? 0

## Replies To: Need Help Creating Code to Find Min and Max Numbers

### #2 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 02:36 PM

You need to use arrays - if you are not familiar with them, here's a link to get you started http://www.javatutor...ava-arrays.html

Once you have the numbers in an array, you use a for loop to go through every element and it should be easy to get the min and max at that point.

### #3 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 02:48 PM

I'm trying to understand those arrays but I'm having a little trouble with it. I'm not sure how I'd make my command line arguments into arrays, and then where to go from there.

### #4 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,853
• Joined: 19-March 11

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 03:07 PM

Your command line args come in as an array of String. You can iterate over that in a loop, and parse each token in the args array to a numeric value. If you want, you can put those values into an array of doubles (or ints, or whatever type you're expecting)
However, you really don't need to do that to just get the max and the min. Here's a hint to simplify your life: at any given point, you've seen exactly one max value and one min value.

### #5 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 03:41 PM

I'm sorry, I'm still lost. I'm not a programmer by any means and this Java class is the only one I need to take for my major. I'm pretty bad at programming. I feel like seeing an example of what you're talking about by using a bit of my code that I posted will give me some more insight. I know you're not going to do my work for me, but right now I'm still lost.

### #6 Kakerergodt

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 03:44 PM

As is being said array and loops are the way to go, you might skip arrays if you're gonna learn that later and you know exactly how many numbers you are getting, but a loop is a must. Two temporary variables for storing the largest and smallest number is also a good way to go (to expand on jon.kiparsky's hint).

This post has been edited by Kakerergodt: 12 October 2012 - 05:00 PM

### #7 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 04:17 PM

So this might not be quite what you want me to do, but it would give me ten loops, which is something I can work with. My code doesn't have to be super efficient since we're just graded on making it work, but I also don't want something like 300 lines of code.

Could I do something like this:

for(a >= b && a >= c && a >= d && a >= e)
{
System.out.println(a + " is the max");
}

for(b >= a && b >= c && b >= d && b >= e)
{
System.out.println(b + " is the max");
}

etc.

Then for the minimum, I could have something like:

for(a <= b && a <= c && a <= d && a <= e)
{
System.out.println(a + " is the min");
}

etc.

Will this work for giving a minimum and a maximum? I know it's not exactly the way the hints were given, but it does work, no?

### #8 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 04:30 PM

Those were supposed to be while loops, sorry. I just tried it and nothing was printed out. I only tried it with one loop just to test it though. The numbers I enter in the command console need to be between 1 and 10 as well. Here's what I have:

public class MinMax5
{
public static void main(String args[])
{

double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double c = Double.parseDouble(args[2]);
double d = Double.parseDouble(args[3]);
double e = Double.parseDouble(args[4]);

if(a < 1 || a > 10)
{
System.out.println("Your first number is not between 1 and 10.");
}

else if(b < 1 || b > 10)
{
System.out.println("Your second number is not between 1 and 10.");
}

else if(c < 1 || c > 10)
{
System.out.println("Your third number is not between 1 and 10.");
}

else if(d < 1 || d > 10)
{
System.out.println("Your fourth number is not between 1 and 10.");
}

else if(e < 1 || e > 10)
{
System.out.println("Your fifth number is not between 1 and 10.");
}

else
{
while(a >= b && a >= c && a >= d && a >= e);
{
System.out.println(a + " is the max");
}
}
}
}

### #9 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 04:43 PM

So I realized I need something to make the while loop stop, but I don't know what. It's not printing anything out anyways so I don't know where I'm wrong.

### #10 Kakerergodt

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 04:59 PM

I'm sorry I was a bit premature in saying that you could skip using the array, but still use a loop. The loop isn't much use without the array (or some form of list/iteration).

You've got two choices now, either look into array and make it work with a loop. Or skip both and do something like this:
<Pseudocode>
double maxValue = a; //assume the first is the biggest
double minValue = a; //assume the first is the smalles

- check whether the next value is greater than the current maxValue,
if so, update maxValue.
- Do the same for minValue.
- repeat for b,c,d,e

(The last two steps would fit nicely in a loop, since you want to do it for all the values you have, without a loop you'll have to repeat it as many times as you have numbers.)

Edit:
If you want to use a loop a for-loop would be the most logical to use.
<Pseudocode>
double[] array = {a, b, c, d, e}; //Add all elements to array
double maxValue = array[0]; //assume the first is the biggest
double minValue = array[0]; //assume the first is the smalles

for( all values )
{
- check whether the next value is greater than the current maxValue,
if so, update maxValue.
- Do the same for minValue.
}
System.out.println(maxValue + " is max. " + minValue + " is min.");

This post has been edited by Kakerergodt: 12 October 2012 - 05:06 PM

### #11 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 05:24 PM

I cannot figure out how to make this thing work in a loop to save me. I figured it out with your last comment Kakerergodt! Thank you. This is due at midnight so maybe after that you could post an example of it with a loop? I'm sure it would help me on future assignments that need to use them, especially since I'm still extremely new at this. My next problem will be displaying the numbers in order from smallest to largest, which may require arrays and loops, which then I might definitely be stuck. I'll post my code that only needs to give me the max and min first, so you can see that I got it done.

### #12 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 05:33 PM

Works like a charm, despite being inefficient and a nightmare to a lot of advanced coders.

public class MinMax5
{
public static void main(String args[])
{

double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double c = Double.parseDouble(args[2]);
double d = Double.parseDouble(args[3]);
double e = Double.parseDouble(args[4]);
double maxValue = a;
double minValue = a;

if(a < 1 || a > 10 || b < 1 || b > 10 || c < 1 || c > 10 || d < 1 || d > 10 || e < 1 || e > 10)
{
System.out.println("Please enter numbers that are only between 1 and 10.");
}

//start max value calculations
else if (a >= b && a >= c && a >= d && a >= e)
{
maxValue = a;
System.out.println(maxValue + " is the max.");
}

else if (b >= a && b >= c && b >= d && b >= e)
{
maxValue = b;
System.out.println(maxValue + " is the max.");
}

else if (c >= a && c >= b && c >= d && c >= e)
{
maxValue = c;
System.out.println(maxValue + " is the max.");
}

else if (d >= a && d >= b && d >= c && d >= e)
{
maxValue = d;
System.out.println(maxValue + " is the max.");
}

else if (e >= a && e >= b && e >= c && e >= d)
{
maxValue = e;
System.out.println(maxValue + " is the max.");
}
//end max value calculations

//start min value calculations
if (a <= b && a <= c && a <= d && a <= e)
{
minValue = a;
System.out.println(minValue + " is the min.");
}

else if (b <= a && b <= c && b <= d && b <= e)
{
minValue = b;
System.out.println(minValue + " is the min.");
}

else if (c <= a && c <= b && c <= d && c <= e)
{
minValue = c;
System.out.println(minValue + " is the min.");
}

else if (d <= a && d <= b && d <= c && b <= e)
{
minValue = d;
System.out.println(minValue + " is the min.");
}

else if (e <= a && e <= b && e <= c && e <= d)
{
minValue = e;
System.out.println(minValue + " is the min.");
}
//end min value calculations
}
}

### #13 Kakerergodt

Reputation: 89
• Posts: 201
• Joined: 01-May 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 05:42 PM

You compare all values to all values which gives you an unneccesary amount of operations.
If you first set maxValue to a and then compare maxValue to b, then there's no use in comparing c,d,e to a or b, because you know maxValue holds the largest no matter what.

double maxValue = a; //assume the first is the biggest
double minValue = a; //assume the first is the smalles
if(maxValue < B)/> maxValue = b;
if(maxValue < c) maxValue = c;
if(maxValue < d) maxValue = d;
if(maxValue < e) maxValue = e;
System.out.println(maxValue + " is max.");

Edit: You'll probably figure out how to do the same with minValue.

This post has been edited by Kakerergodt: 12 October 2012 - 05:44 PM

### #14 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 05:54 PM

I tried it that to clean up the code a little bit but it didn't work properly. I checked over it to make sure it was correct but couldn't seem to get it. I need to focus on my new code, SortNumbers now though. It's supposed to be a modification to MinMax5, but it prints the original input and then prints the numbers in order from smallest to largest.

### #15 dobrien323

Reputation: 1
• Posts: 10
• Joined: 12-October 12

## Re: Need Help Creating Code to Find Min and Max Numbers

Posted 12 October 2012 - 06:30 PM

Do you have an example of how to cast an argument into an array? I haven't seen anything online, and it seems that I need arrays to do Sort functions for this new code on sorting from smallest to largest. I've only seen examples of casting pre-determined numbers into arrays. We can only use the arguments in the initial line of the command console for our code.

For example, I would enter:
java SortNumbers5 7 3 4 2 9

Then, if the code worked properly, it would put them in order.