-2

I am making an array that generates random numbers from 1-100. Then, at the end, I will output the max and min numbers from the list. However, I don't know exactly how to find / call upon the max and min numbers I've tried using the math method functions like Math.min() but I don't think it works for arrays. Here's my code (the underscores is where I thought I'd call in the max and min numbers but I don't know how to).

public class MaxMin {

public static void main(String[] args) {

    int max = 0;
    int min = 0;
    int hold = 0;
    System.out.println("#" + "\t\t" + "Number");
    int[][] numberArray= new int [10][1];
    for(int i=0;i<10;i++)
    {
        System.out.print((i+1)+"\t\t");
        for(int j=0;j<1;j++)
        {
            numberArray[i][j] = (int)(Math.random()*100)+1;
            System.out.print(numberArray[i][j] + "\t\t");
        }
        System.out.println();
    }
    System.out.println("Max: " + "\t\t" + ______);
    System.out.println("Min: " + "\t\t" + ______);

}

}

gino
  • 41
  • 5
  • 2
    Does this answer your question? [Finding min element in an array](https://stackoverflow.com/questions/28569181/finding-min-element-in-an-array) – code11 Mar 17 '21 at 13:56

2 Answers2

2

When you generate the random number you should compare it to both your min and max variables, and if it is greater than the maximum or smaller than the minimum you should update the value of those variables, like so:

for (int j=0;j<n;j++)
{
    numberArray[i][j] = (int)(Math.random()*100)+1;
    if (numberArray[i][j] > max)
        max = numberArray[i][j];
    if (numberArray[i][j] < min)
        min = numberArray[i][j];
    System.out.print(numberArray[i][j] + "\t\t");
}

That way you avoid iterating through your array more than once. Also, consider how you initialize the max and min variables. You could generate the random number outside the loop and assign the first value to both max and min, because if you initialize your min as zero and you only generate positive integers, for example, min will never change. You should initialize max to your minimum value (in this case 0) and min to your maximum value at the beginning of your code.

andand
  • 17,134
  • 11
  • 53
  • 79
Tomás Ferrer
  • 55
  • 1
  • 6
1

If it has only one column, then you can create another temporary array and just sort array using

Arrays.sort(arrayname);

/ by

for (int i = 0; i < arrayname.length; i++)   
{  
    for (int j = i + 1; j < arrayname.length; j++)   
    {  
        int tmp = 0;  
        if (arrayname[i] > arrayname[j])   
        {  
            tmp = arr[i];  
            arrayname[i] = arrayname[j];  
            arrayname[j] = tmp;  
        }  
    }  
}

then

  • arrayname[0] is min number and
  • arrayname[arrayname.length -1] is max number

or if have multiple columns then

array[m][n]

for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
        for(int k = 0; k < m - 1 - j; k++){ 
            if(array[k][i] > array[k + 1][i]){
                int temp = array[k][i];
                array[k][i] = array[k + 1][i];
                array[k + 1][i] = temp;
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Sorting isn't the best way to do this. It's at best an O(n log(n)) operation, and the sort shown above is actually O(n^2). You can find the min and max through an exhaustive search of the elements on O(n) time. – andand Mar 17 '21 at 15:30