1

I am having a slight issue when finding the minimum value of my random array. I keep getting 0 as a result for the smallest number. Largest number would appear to be fine. I also receive the wrong number for the index of both as well.

Current Code:

public class RandomArray {

public static void main(String[] args) 
{
    int indexHigh = 0;
    int indexLow = 0;
    int max = 0;
    int min = 0;

    int[] randArray = new int[10];

    for(int i = 0; i < randArray.length; i++)
    {
        randArray[i] = RandomMethod.randomInt(1,1000);
        System.out.print(randArray[i] + " ");
        System.out.println(" ");

        //Max & Min
         if (randArray[i] > max)
          max = randArray[i];
         indexHigh = i;
            if (randArray[i] < min)
                min = randArray[i];
         indexLow =  i;
    }

            System.out.println("The highest minimum for the December is: " + min + " at index: " + indexLow);
            System.out.println("The highest maximum for the December is: " + max + " at index: " + indexHigh);


    }

}

Supporting Class(Instructions stated that it needed to be in another class. Only the method is necessary):

import java.security.SecureRandom;
import java.util.*;

public class RandomMethod {

    //implement random class
    static Random randomNumbers = new Random();

    public static void main(String[] args) 
    {

        //#7 -- Generates a number between 10 & 20 ( 100x )

        /*
        int counter = 0;
        while(counter <= 100)
        {
            System.out.println(randomInt(10,20));
            counter++;
        }
        */

        //# 8

        //int[] randArray = new int[randomInt(1,1000)];


    }


    // Random Int method where value is greater than x & less than y
    public static int randomInt(int x, int y)
    {
        int randomValue = randomNumbers.nextInt(y - x) + x;
        return randomValue;
    }

}
1
  • min starts at zero and all the elements in the array are greater than 1. All numbers will thus be larger than min and your min will never be updated. You should set the starting value of min to either the first element of the array, or some very large value that you know is larger than all the elements in the array. Commented Jul 28, 2015 at 2:42

3 Answers 3

3

I keep getting 0 as a result for the smallest number

That's because you have initialized min = 0. And your randInt() will never generate a number < 0. So your min will never change.

To Fix, have the initial min to be higher than the highest your randInt will generate.

int min = 1001; // Or to be safer, use Integer.MAX_VALUE

I also receive the wrong number for the index of both as well.

That's because your if statement doesn't include indexHigh = i; statement.

Use,

 if (randArray[i] > max) {
  max = randArray[i];
  indexHigh = i;
 }
    if (randArray[i] < min) {
        min = randArray[i];
        indexLow =  i;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Kudos, I always seem to fall for the same mistakes on every project.
2

You min is initialized to 0 already. So it's only looking for the smallest value below zero. Initialize min to a higher number, higher than what the expected input will be, or use int min = Integer.MAX_VALUE;.

For index printing, it's a matter of braces {} for the if statements. What happens right now is that on every loop, indexHigh & indexLow are reset with the value of i.

     //Max & Min
     if (randArray[i] > max){
       max = randArray[i];
       indexHigh = i;
     }
     if (randArray[i] < min){
       min = randArray[i];
       indexLow =  i;
     }

This way, those two variables only take the value of i upon actually encountering a higher/lower value.

Comments

0

The variable to check he minimum value should be initialized with big number. Try

int min = Integer.MAX_VALUE;

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.