1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Maximum/Minimum finder

  1. Nov 11, 2011 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations

    ..

    3. The attempt at a solution

    Code (Text):
    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.
     
  2. jcsd
  3. Nov 11, 2011 #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.
     
  4. Nov 11, 2011 #3
    Code (Text):
                    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.
     
  5. Nov 11, 2011 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Maximum/Minimum finder
  1. Maximum power (Replies: 70)

Loading...