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

  • Thread starter Thread starter Hiche
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around writing a Java program that reads a specified number of integers from user input and computes their minimum and maximum values. The focus is on coding practices and potential pitfalls in the implementation, particularly regarding the initialization of variables.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Post 1 presents a Java program structure but indicates uncertainty about initializing the minimum variable, questioning if there are alternative methods.
  • Post 2 argues against initializing the maximum variable to a constant, highlighting issues if all user inputs are negative and suggesting that both minimum and maximum should be set to the first input instead.
  • Post 3 proposes a solution where both minimum and maximum are initialized to the first user input, suggesting this might be a better approach.
  • Post 4 agrees with the proposed solution but suggests adding a "continue;" statement to avoid unnecessary comparisons during the first iteration.

Areas of Agreement / Disagreement

Participants express differing views on the initialization of minimum and maximum values, with no consensus reached on the best approach. Some participants agree on initializing both to the first input, while others raise concerns about handling specific cases, such as all negative inputs.

Contextual Notes

There are unresolved considerations regarding the handling of edge cases, such as when all entered integers are negative or when no integers are provided. The discussion does not clarify the implications of these scenarios on the program's functionality.

Who May Find This Useful

This discussion may be useful for individuals learning Java programming, particularly those interested in input handling and variable initialization practices in coding exercises.

Hiche
Messages
82
Reaction score
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
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.
 
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.
 
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K