redsky
redsky

Reputation: 33

Min and max not working correctly when using single low digit numbers

I'm setting up my array from a user input and then trying to find the min and max.But it only seems to work perfect for finding the max and not the min.

import java.util.Scanner;
public class GradeBookKeeper 
{
  public static void main(String arg[])
{
  int numItems;
  int[] items;
  double sum=0;
  Scanner scan= new Scanner(System.in);
  System.out.println("Enter the number of students: ");
  numItems=scan.nextInt();
  items=new int [numItems];
  int largest=items[0],smallest=items[0];
  for (int i = 0 ; i < items.length; i++ )
  {
      System.out.println("Enter the grade for student "+(i+1)+":");
      items[i] = scan.nextInt();
      sum = sum + items[i];
      if(largest < items[i])
      {
          largest = items[i];
      } 
      else 
      {
          smallest = items[i];
      } 
  }
  System.out.printf("The average is "+"%.2f\n",sum/items.length);
  System.out.println("The minimum is: "+smallest);
  System.out.println("The maximum is: "+largest);
  scan.close();
}
}

//output

Enter the number of students: 4

Enter the grade for student 1:

2

Enter the grade for student 2:

1

Enter the grade for student 3:

2

Enter the grade for student 4:

3

The average is 2.00

The minimum is: 2

The maximum is: 3

Upvotes: 0

Views: 233

Answers (3)

Liam Wilson
Liam Wilson

Reputation: 127

I am only here for an alternative version. And the way it works is sorting the array(lowest to highest), and then just getting lowest as the smallest, and highest as the largest number.

import java.util.Arrays;
import java.util.Scanner;

public class GradeBookKeeper
{
 public static void main(String[] arg)
  {
    Scanner scan= new Scanner(System.in);

    int[] items;
    double sum=0;
    int numItems,largest,smallest;

    System.out.print("Enter the number of students:  ");
    numItems=scan.nextInt();
    items=new int [numItems];
    largest=smallest=items[0];

    for(int i=0;i<items.length;i++)
    {
        System.out.print("Enter the grade for student "+(i+1)+": ");
        items[i] = scan.nextInt();
        sum+=items[i];

    }
    for (int i = 0; i < items.length; i++) {
        Arrays.sort(items);
        largest=items[items.length-1];
        smallest=items[0];
    }


    System.out.printf("The average is "+"%.2f\n",sum/items.length);
    System.out.println("The minimum is: "+smallest);
    System.out.println("The maximum is: "+largest);
    scan.close();
  }
}

Upvotes: 0

Chris Gong
Chris Gong

Reputation: 8229

The problem is that you're only setting smallest when you have not found a new largest element while traversing the items array. So instead of an else, change it to another if statement to see if the item you are iterating on is less than your current smallest item like so,

if(largest < items[i])
{
    largest = items[i];
} 
if(smallest > items[i])
{
    smallest = items[i];
} 

Also, you're initializing smallest and largest to the first element in items which will default to 0 since the items array is empty at that point. Try initializing largest to the smallest possible grade and smallest to the largest possible grade like so (currently using the min and max integer values),

int largest=Integer.MIN_VALUE,smallest=INTEGER.MAX_VALUE;

The reason for this is that you're never going to find a grade smaller than 0, and since that's what you had before, the smallest grade would have always been 0.

Upvotes: 3

VEDANSH SOMANI
VEDANSH SOMANI

Reputation: 1

if (largest < items[i])
largest = items[i]
else if (smallest > items[i])
smallest = items[i]

Also smallest's first value should be one of the items as none of the grades can be smaller than 0

This should work, I hope that you understand

Upvotes: -1

Related Questions