Find Maximum and Minimum of N Integers | MinMax Program for Java

  • Thread starter Hiche
  • Start date
In summary, the program reads a number N from the command line and asks the user to enter N integers. It then computes the minimum and maximum of these numbers and prints them out. One potential issue is initializing minimum and maximum to 0, which may cause problems if all the numbers entered are negative. A suggested solution is to set minimum and maximum to the first element on the first iteration and then continue with the normal comparisons on subsequent iterations.
  • #1
Hiche
84
0

Homework Statement



Write a program that reads a number N from the command line and asks the user to enter N integers. The program should compute their minimum and maximum, and prints them out.

Homework Equations



..

The Attempt at a Solution



Code:
public class MinMax 
{
	public static void main(String[] args)
	{
		int N = Integer.parseInt(args[0]);
		int userInput = 0;
		int maximum = 0;
		int minimum = ?;
		
		for (int i = 1; i <= N; i++)
		{
			StdOut.print("Enter integer " + i + ": ");
			userInput = StdIn.readInt();
			if (userInput > maximum)
				maximum = Math.max(maximum, userInput);
			if (userInput < minimum)
				minimum = Math.min(minimum, userInput);
		}
		StdOut.println(maximum + " " + minimum);
		
		
	}
}

The question mark is where I need help. If I input a large number, it works. But is it the only way? We are limited to using the standard input command StdIn. This is an easy question I know, but I was just looking for an alternative.
 
Physics news on Phys.org
  • #2
I don't think you should initialize either minimum or maximum to constants.

There is a problem with

int maximum = 0;

if all the numbers entered by the user are negative.

Instead, the first iteration could have different behavior than the others. On the first interation, minimum and maximum are both set to the first element.
 
  • #3
Code:
                for (int i = 1; i <= N; i++)
		{
			System.out.print("Enter integer " + i + ": ");
			userInput = StdIn.readInt();
			if (i == 1)
			{
				maximum = userInput;
				minimum = userInput;
			}
			if (userInput > maximum)
				maximum = Math.max(maximum, userInput);
			if (userInput < minimum)
				minimum = Math.min(minimum, userInput);
		}

Something like this? I'm guessing there is a better way.
 
  • #4
Yes, that should be okay, although it might be better to put "continue;" at the end of that first if block, since there is no need for comparisons on the first iteration.
 
  • #5



One alternative could be to initialize the minimum variable to the first user input, assuming that the user will always input at least one integer. This way, the minimum will be updated as the user inputs more integers, and the if statement for checking the minimum can be removed. Another alternative could be to use Integer.MAX_VALUE for the initial value of minimum, as it is the largest possible integer value and will be updated as the user inputs smaller integers.
 

FAQ: Find Maximum and Minimum of N Integers | MinMax Program for Java

What is a maximum/minimum finder?

A maximum/minimum finder is a tool or algorithm used to determine the highest or lowest value within a given set of data or parameters. It can be used in various fields such as mathematics, statistics, and computer science.

How does a maximum/minimum finder work?

A maximum/minimum finder works by comparing each data point within a given set and identifying the highest or lowest value based on a specific criteria or function. This can be done manually or through the use of computer programs and algorithms.

What is the importance of a maximum/minimum finder?

A maximum/minimum finder is important in data analysis and decision-making processes as it allows for the identification of extreme values within a data set. This can help in identifying outliers, understanding patterns, and making informed decisions based on the data.

What are the different types of maximum/minimum finders?

There are various types of maximum/minimum finders, including graphical methods, calculus-based methods, and optimization algorithms. Each type has its own advantages and disadvantages, and the choice of method depends on the specific problem at hand.

Can a maximum/minimum finder be used in real-world applications?

Yes, a maximum/minimum finder can be used in real-world applications, such as financial analysis, engineering design, and data mining. It can help in identifying the best or worst-case scenarios, optimizing processes, and making data-driven decisions.

Similar threads

Replies
7
Views
2K
Replies
3
Views
1K
Replies
4
Views
8K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top