Skip to content

Why do Min & Max show up as 0 in my array?

Why do Min & Max show up as 0 in this array? I tried to figure it out for a while now but I couldn’t wrap my head around it.

public class Main
{
public static void main(String[] args) {
  Scanner input=new Scanner(System.in);
  int n=input.nextInt();
  int []x=new int[n-1];
  random(x);
  
  sizeArray(x);
  
        System.out.println("The min is "+ min(x));
        System.out.println("The max is "+ max(x));
        System.out.println("The Total is "+ getTotal(x));
    }

public static int random(int[]x){
    Random rand=new Random();
    for (int i = 0; i < x.length; i++) {
        x[i]=rand.nextInt(101);
        
    }
        return 0;
    
} 
 public static int sizeArray(int []x){
    for (int i = 0; i < x.length; i++) {
        System.out.println(x[i]);
        
    }
        return 0;
}

Here is the ‘min’.

public static int min(int[]x){
    int min=0;
    for (int i = 0; i < x.length; i++) {
        if(x[i]<min){
            min=x[i];
        
        }
        
    }
        return min;
}

And the ‘max’ I am not sure what the issue is.

public static int max(int[]x){
    int max=0;
    for (int i = 0; i < x.length; i++) {
        if(x[i]>max)
            x[i]=max;
        i++;
        
    }

        return max;
}
public static int getTotal(int[]x){
  int  total=0;
    for (int i = 0; i < x.length; i++) {
        total=total+x[i];
        
    }
        return total;
    }
}```

||SOLVED|| Changed min and max and also added maximum(x); minimum(x); in main while removing ^^ System.out.println(“The min is “+ min(x)); ^^ ^^ System.out.println(“The max is “+ max(x));.^^

import java.util.*;
public class Main
{
public static void main(String[] args) {

Scanner input=new Scanner(System.in);
  int n=input.nextInt();
  int []x=new int[n];
  random(x);
  
  sizeArray(x);
  maximum(x);
        minimum(x);

        System.out.println("The Total is "+ getTotal(x));
    }
public static int random(int[]x){
    Random rand=new Random();
    for (int i = 0; i < x.length; i++) {
        x[i]=rand.nextInt(101);
        
    }
        return 0;
    
}
public static int sizeArray(int []x){
    for (int i = 0; i < x.length; i++) {
        System.out.println(x[i]);
        
    }
        return 0;
}

|Changed max|

public static void maximum(int x[]) {
        int max = x[0];
        for (int i = 1; i < x.length; i++) {
            if (x[i] > max) {
                max = x[i];
            }
        }
        System.out.println("maximum is:" + max);
    }

|And changed min|

    public static void minimum(int x[]) {
        int min = x[0];
        for (int i = 1; i < x.length; i++) {
            if (x[i] < min) {
                min = x[i];
            }
        }
        System.out.println("minimum is:" + min);
    }
public static int getTotal(int[]x){
  int  total=0;
    for (int i = 0; i < x.length; i++) {
        total=total+x[i];
        
    }
        return total;
}
}

Answer

Here you are probably setting every element in the array to 0. And in any case, max is always going to return 0

public static int max(int[]x){
    int max=0;
    for (int i = 0; i < x.length; i++) {
        if(x[i]>max)
            x[i]=max;
        i++;
        
    }

        return max;
}

And here, you are starting with min = 0 (which isn’t necessarily a value in the array) and since all the values are greater than 0, the return value is 0.

public static int min(int[]x){
    int min=0;
    for (int i = 0; i < x.length; i++) {
        if(x[i]<min){
            min=x[i];
        
        }
        
    }
        return min;
}

And honestly, it would have been very easy or you to figure this out if you’d used the debugger to step through your functions. Learning how to use a debugger is an invaluable skill!