Home > OS >  How to find min and max values for a randomized array, max works but for some reason the min doesn&#
How to find min and max values for a randomized array, max works but for some reason the min doesn&#

Time:08-13

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int [] intNumbers = new int [10];
    
    int intSmallest = intNumbers[0];
    int intLargest = intNumbers[0];
    
    for (int i = 0; i< intNumbers.length; i = i  1) {
       intNumbers[i] = (int) (Math.random()*100);
       
       //if (intNumbers[i] < intSmallest) intSmallest = intNumbers[i]; 
    }

    jLabel5.setText(Arrays.toString(intNumbers));
     
    for (int i = 0; i< intNumbers.length; i  ) {
        if (intNumbers[i] > intLargest) {
            intLargest = intNumbers[i];
        }
        if (intNumbers[i] < intSmallest) {
            intSmallest = intNumbers[i];
        }
    }
  
    System.out.println(intLargest);
    System.out.println(intSmallest);
}

For some reason the max works, but the min doesn't, even though they are pretty much the same code.

CodePudding user response:

You are initializing smallest and largest to zero. Move these initializations and declarations after the loop.

for (int i = 0; i< intNumbers.length; i = i  1) {
   intNumbers[i] = (int) (Math.random()*100);               
}
int intSmallest = intNumbers[0];
int intLargest = intNumbers[0];    

Next, I would suggest Math.max and Math.min and a for-each loop for the second loop. Like,

for (int val : intNumbers) {
    intLargest = Math.max(intLargest, val);   
    intSmallest = Math.min(intSmallest, val);
}

CodePudding user response:

Need to re initialize intSmallest and intLargest to intNumbers[0] after you set the intNumbers to random values to get set the exact first random number to your anwer variable.

Something like this:

int [] intNumbers = new int [10];//all array elements are 0
    
    int intSmallest = intNumbers[0];///okay here intNumbers[0] is 0
    int intLargest = intNumbers[0];
    
    for (int i = 0; i< intNumbers.length; i = i  1) {
      // here intNumbers[0] gets a new value which can be less than 0
       intNumbers[i] = (int) (Math.random()*100);
       
       //if (intNumbers[i] < intSmallest) intSmallest = intNumbers[i]; 
            
        
    }
     intSmallest = intNumbers[0];// seed the smallest and biggest 
     intLargest = intNumbers[0];//you could also move the decleretion here... 
    jLabel5.setText(Arrays.toString(intNumbers));
     
    for (int i = 0; i< intNumbers.length; i  ) {
        
        if (intNumbers[i] > intLargest) {
            intLargest = intNumbers[i];
        }
        if (intNumbers[i] < intSmallest) {
            intSmallest = intNumbers[i];
        }
    }
    
  
    System.out.println(intLargest);
    System.out.println(intSmallest);
}

CodePudding user response:

The following code works. One thing I found is the initialization of intSmallest and intLargest. In the beginning it is not clear, which values will be generated. Thus, set intSmallest to max value of interval, and intLargest to min value of interval. (If max and min value of interval are not clear, then set both to the first generated value, just after generating first value).

public class Main
{
    public static void main(String[] args) {
        int [] intNumbers = new int [10];
    
    int intSmallest = 100;
    int intLargest = 0;
    
    for (int i = 0; i< intNumbers.length; i = i  1) {
       intNumbers[i] = (int) (Math.random()*100);
    }

    // edit: better as described above
    intSmallest=intNumbers[0];
    intLargest=intNumbers[0];
    // end of edit

     
    for (int i = 0; i< intNumbers.length; i  ) {
        System.out.println("number " i " is " intNumbers[i]);
        if (intNumbers[i] > intLargest) {
            intLargest = intNumbers[i];
        }
        if (intNumbers[i] < intSmallest) {
            intSmallest = intNumbers[i];
        }
    }
    
  
    System.out.println("Largest is " intLargest);
    System.out.println("Smallest is " intSmallest);

    }
}

The output:

number 0 is 16
number 1 is 90
number 2 is 41
number 3 is 71
number 4 is 57
number 5 is 5
number 6 is 11
number 7 is 72
number 8 is 32
number 9 is 17
Largest is 90
Smallest is 5

I would also like to point out that such kind of problems can often simply be solved by printing values in between various steps, like within the loop (instead of only printing final result). Then you can see where "something" goes wrong.

CodePudding user response:

WHY

intSmallest is assigned to 0. Unless intNumbers contains value less than or equals to 0 after the loop, intSmallest will always be 0.

HOW

method 1:

move intSmallest initializations after the loop.

    for (int i = 0; i< intNumbers.length; i = i  1) {
       intNumbers[i] = (int) (Math.random()*100);
       
       //if (intNumbers[i] < intSmallest) intSmallest = intNumbers[i]; 
    }

    int intSmallest = intNumbers[0];

method 2:

sort intNumbers first, the min value is intNumbers[0] and the max value is intNumbers[intNumbers.length - 1] .

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int [] intNumbers = new int [10];
    
    for (int i = 0; i< intNumbers.length; i = i  1) {
       intNumbers[i] = (int) (Math.random()*100);
    }

    jLabel5.setText(Arrays.toString(intNumbers));
     
    Arrays.sort(intNumbers);

    System.out.println(intNumbers[intNumbers.length - 1]); // intLargest
    System.out.println(intNumbers[0]); // intSmallest
}
  • Related