WannaBeCoder
WannaBeCoder

Reputation: 53

Issue with finding minimum value of array

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;
    }

}

Upvotes: 1

Views: 229

Answers (3)

Vineet
Vineet

Reputation: 897

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.

Upvotes: 2

Codebender
Codebender

Reputation: 14471

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;
 }

Upvotes: 3

suztomo
suztomo

Reputation: 5202

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

int min = Integer.MAX_VALUE;

Upvotes: 0

Related Questions