1

Attempting to find the min/max of a randomly generated array in java. My code is working for finding the maximum, however I'm not sure why when I try running it, the minimum is coming up as 0.0 every time.

public static void main(String[] args) {
    double array1[] = new double [10];
    int n = array1.length;
    double max = array1[0];
    double min = array1[1];

    System.out.println("Array: ");

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


            }

        }

 }
1
  • Well, you're generating positive numbers, so it's not that surprising Commented Apr 30, 2019 at 1:39

5 Answers 5

4

From what I see, the reason you getting 0.0 for the min value because it is the default value of the element inside an empty array, so when you try to compare throughout the random generated array which minimum value is only 1, the value inside the min variable will never be updated. You have already assigned the min to be the second element of the array although the elements inside the array haven't been initialized. Try to initialize the array in a different loops which is processed before the comparing loop, or assigned the min variable to be the maximum value of the random generator, then it might run smoothly.

for(int i= 0;i<n;i++)
   array1[i] = Math.floor((Math.random() * 100) + 1);
max = array[0];
min = array[0];
for(int i = 1;i<n;i++)
{
  System.out.print(array1[i]+ " | " );
  //continue ...
}

Or this

min = 100;
max = 1;
for(int i = 0; i<n;i++)
{
   //continue ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

You'll probably want min = array[0]; then you can start the loop from i = 1
I didn't even think about how I was setting the value in an empty array. Thanks for the help man!
0

Either do the array assignments before initializing max and min; or however if you want to avoid that extra loop, just initialize min to Double.MAX_VALUE since assigning it to 0.0 (default initial value of element in a double array) won't satisfy the if condition - any random number greater than or equal to 1 would always be greater than 0.

Comments

0

A fairly standard approach is to initialize min/max variables with "worst case" values:

double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;

which will also solve your problem, since you won't generate any values as low as 0.0, which is what the array holds at the time you access it to assign values to min and max.

Also, within your loop you can simplify to this:

max = Math.max(max, array[i]);
min = Math.min(min, array[i]);

Comments

0
    // using forloop
// max number in arrry
    const arr = [2, 3, 5, 10, 8, 3];
    let max = 0;
    for (i = 0; i < arr.length; i++) {
      if (arr[i] > max) {
        max = arr[i];
      }
    }
    console.log(max);
    
    // max number in arrry
    let min = 100;
    for (i = 0; i < arr.length; i++) {
      if (arr[i] < min) {
        min = arr[i];
      }
    }
    console.log(min);

Comments

0
//find maximum  and min number in array using funtion

const maxArrayNum = (arr) => {
  let max = Math.max(...arr);
  return max;
};

const minArrayNum = (arr) => {
  let min = Math.min(...arr);
  return min;
};

const maxNum = maxArrayNum([3, 4, 2, 1]);
console.log(maxNum);
const minNum = minArrayNum([3, 4, 2, 1]);
console.log(minNum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.